autoPitch Pitch_subtractLinearFit (Pitch me, int unit) { try { autoPitch thee = Pitch_interpolate (me); /* * Find the first and last voiced frame. */ long imin = thy nx + 1, imax = 0; for (long i = 1; i <= my nx; i ++) if (Pitch_isVoiced_i (thee.peek(), i)) { imin = i; break; } for (long i = imin + 1; i <= my nx; i ++) if (! Pitch_isVoiced_i (thee.peek(), i)) { imax = i - 1; break; } long n = imax - imin + 1; if (n < 3) return thee; /* * Compute average pitch and time. */ double sum = 0.0; for (long i = imin; i <= imax; i ++) { sum += Sampled_getValueAtSample (thee.peek(), i, Pitch_LEVEL_FREQUENCY, unit); } double fmean = sum / n; double tmean = thy x1 + (0.5 * (imin + imax) - 1) * thy dx; /* * Compute slope. */ double numerator = 0.0, denominator = 0.0; for (long i = imin; i <= imax; i ++) { double t = thy x1 + (i - 1) * thy dx - tmean; double f = Sampled_getValueAtSample (thee.peek(), i, Pitch_LEVEL_FREQUENCY, unit) - fmean; numerator += f * t; denominator += t * t; } double slope = numerator / denominator; /* * Modify frequencies. */ for (long i = imin; i <= imax; i ++) { Pitch_Frame myFrame = & my frame [i], thyFrame = & thy frame [i]; double t = thy x1 + (i - 1) * thy dx - tmean, myFreq = FREQUENCY (myFrame); double f = Sampled_getValueAtSample (thee.peek(), i, Pitch_LEVEL_FREQUENCY, unit); f -= slope * t; if (NOT_VOICED (myFreq)) FREQUENCY (thyFrame) = 0.0; else FREQUENCY (thyFrame) = Function_convertSpecialToStandardUnit (me, f, Pitch_LEVEL_FREQUENCY, unit); } return thee; } catch (MelderError) { Melder_throw (me, U": linear fit not subtracted."); } }
int fft2note(double n) { /* Takes the index of the component in the FFT output array * and finds the closest ( appropriate ) MIDI note. * * The "closest note" is restricted by setting the Key and * Scale. The chromatic C major scale being the obvious * default (e.g. any note of the equal-tempered scale). */ if (n < 1.0) { if (noActivity++ > 70) { midiSilence(); } return; } noActivity = 0; int result; double frequency = FREQUENCY(n); int k = (sizeof(mdata)/sizeof(struct midiData))/2; int interval = (sizeof(mdata)/sizeof(struct midiData))/4; gWhere = 5; while(interval) { #ifdef DEBUG printf("Considering %s (%d)\n", mdata[k].name, mdata[k].octave); #endif if (mdata[k].frequency > frequency ) { k -= interval; } else { k += interval; } interval /= 2; } result = k; if ( abs(mdata[k].frequency-frequency) > abs(mdata[k+1].frequency-frequency) ) { result = k+1; } else if ( abs(mdata[result].frequency-frequency) > abs(mdata[k-1].frequency-frequency) ) { result = k-1; } #ifdef DEBUG printf("I like %s (%d)\n", mdata[k].name, mdata[k].octave); #endif gWhere = 7; int mnote = nearestValidMIDINote( mdata[result].note ); if (!(lastnote == mnote) || (noActivity > 20) ) { /* midiDrum(mnote, 100); */ midiNote(mnote-12, 127); writeNote(mnote); Sleep(gDelay); } lastnote = mnote; gAct = (int) frequency; gNear = (int) mdata[result].frequency; gNMIDI = mdata[result].note; return ( mdata[result].note ); }
void DSPC::handleMIDbutton(int button) { if(button>=0 && button<=1) { if(state==S_MODE) { if(mode==DSP_OFF) mode=CONCERT_HALL; else mode=MODE(mode+1); ibus.sendMessage(DEVICE_DSPC,DEVICE_DSP,0x34,0x09+mode); } else { state = S_MODE; } displayMenu(); displayDSP(); } if(!isMemory()) return; if(button==10) { flat(); } if(button>=2 && button<=3) { if(state==S_REVERB) { reverb+= button==2 ? -1 : 1; if(reverb<0) reverb=0; if(reverb>7) reverb=7; buffer[0]=0x34; buffer[1]=0x94 + memory; buffer[2]=reverb & 0x0F ; ibus.sendMessage(DEVICE_DSPC,DEVICE_DSP,(const uint8_t *)buffer,3); } else state = S_REVERB; } if((button>=4 && button<=5)) { if(state==S_ROOMSIZE) { roomsize+= button==4 ? -1 : 1; if(roomsize<0) roomsize=0; if(roomsize>7) roomsize=7; buffer[0]=0x34; buffer[1]=0x94 + memory; buffer[2]=(roomsize & 0x0F) | 0x20; ibus.sendMessage(DEVICE_DSPC,DEVICE_DSP,(const uint8_t *)buffer,3); } else state = S_ROOMSIZE; } if(button==6) { if(state==S_FREQUENCY) { if(curfreq==F_80HZ) curfreq=F_12KHZ; else curfreq = FREQUENCY(curfreq-1); } else { state = S_FREQUENCY; } } if(button==7) { if(state==S_FREQUENCY) { if(curfreq==F_12KHZ) curfreq=F_80HZ; else curfreq = FREQUENCY(curfreq+1); } else { state = S_FREQUENCY; } } if(state==S_FREQUENCY && (button>=8 && button<=9)) { int b = boost[curfreq]; b += (button==8) ? -1 : 1; if(b<-10) b=-10; if(b>10) b=10; boost[curfreq] = b; buffer[0]=0x34; buffer[1]=0x14 + memory; buffer[2]=(((curfreq*2)<<4) & 0xF0) | ((b < 0 ? (0x10 | (abs(b) & 0x0F)) : (b & 0x0F))); ibus.sendMessage(DEVICE_DSPC,DEVICE_DSP,(const uint8_t *)buffer,3); } displayDSP(); }