// // int midiPlay(FILE *midi); // Purpose: To play a midi file // Parameters: FILE *midi - opened file to be played // Return: int - number of notes played // int midiPlay(FILE *midi){ int i; float freq; freq = midiNote(midi); while(freq != 0){ i++; dspPlay(freq); freq = midiNote(midi); } return i; }
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 ); }
static int midiTone (NoteDevice *device, unsigned int duration, NoteFrequency frequency) { return midiNote(device, duration, getNearestNote(frequency)); }