// Functions for converting frequencies to the closest musical note. These // are tuned to use A4 (MIDI note 69 LOL) as exactly 440 Hz. // Sample usage: // note[440 Hz] note[f is frequency] := { noteNames = ["C", "C♯/D♭", "D", "D♯/E♭", "E", "F", "F♯/G♭", "G", "G♯/A♭", "A", "A♯/B♭", "B"] roundnotes = MIDINote[f] octaves = floor[roundnotes/12] - 1 // Octaves above C0 (MIDI 0 is at C-1) scale = roundnotes - ((octaves+1) * 12) // Semitones above C in that octave return noteNames@scale + octaves } /** Turns a frequency into the closest MIDI note number. Also note that this is extrapolated, so there can be notes smaller than 0 or greater than 127. MIDI starts with note 0 being C-1 at about 8.1758 Hz and goes up to note 127 being G9 at ~12544 Hz. A4 is note 69 at exactly 440 Hz. C4 "middle C" is note 60 Sample usage: MIDINote[440 Hz] */ MIDINote[f is frequency] := { notesize = 2^(1/12) A4 = 440 Hz C4 = A4 / (notesize^9) // Freq. of C4 ("Middle C"), 9 semitones down from A4 CMinus1 = C4 / 2^5 // C-1 is MIDI note 0, 5 octaves down from C4 ratio = f / CMinus1 notes = ln[ratio] / ln[notesize] // This is number of semitones above C-1 roundnotes = round[notes] return roundnotes }