Lesson 2 of 15
Frequency to Note
The Inverse Formula
Given a frequency f, which MIDI note is it closest to?
Inverting the formula f = 440 × 2^((n−69)/12):
n = 69 + 12 × log₂(f / 440)
Round to the nearest integer to get the closest MIDI note.
Logarithm Review
log₂(x) = Math.log2(x) in JavaScript.
Cents
The fractional part tells you how far off the frequency is from the nearest note, measured in cents (1/100th of a semitone):
cents = (exact_n − nearest_n) × 100
- ±50 cents = halfway between notes (maximally out of tune)
- 0 cents = perfectly in tune
Examples
| Frequency | Exact n | Nearest MIDI | Note |
|---|---|---|---|
| 440 Hz | 69.0 | 69 | A4 |
| 432 Hz | 68.68 | 69 | A4 (−32 cents flat) |
| 450 Hz | 69.39 | 69 | A4 (+39 cents sharp) |
| 261.63 Hz | 60.0 | 60 | C4 |
Your Task
Implement freqToNote(f) that returns the nearest MIDI note number for frequency f.
Run your code to hear the note detected from 440 Hz.
Web Audio API loading...
Loading...
Click "Run" to execute your code.