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

FrequencyExact nNearest MIDINote
440 Hz69.069A4
432 Hz68.6869A4 (−32 cents flat)
450 Hz69.3969A4 (+39 cents sharp)
261.63 Hz60.060C4

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.