Architecture · Guides

How pitch detection works: frequency, cents, MIDI numbers and the YIN algorithm.

A clear explanation of how software finds the note you sang: fundamental frequency, MIDI note numbers, cents and the YIN algorithm.

Key takeaways

  • A musical pitch is a frequency. Software estimates the fundamental frequency of a short window of audio and converts it to a note.
  • MIDI note number is 69 + 12 × log2(f / 440), where 69 is A4 at 440 Hz.
  • A cent is one hundredth of a semitone. Singers are rarely exactly in tune, so tolerance is essential.
  • YIN is a widely used time-domain algorithm built on autocorrelation, with fixes that reduce octave errors.

From sound to number

When you hum, your vocal cords vibrate at a certain rate. A rate of 220 vibrations per second is 220 hertz (Hz), which is the note A3. A pitch detector's job is to find that rate, called the fundamental frequency, from a recording.

Real voices contain the fundamental and a stack of harmonics at multiples of it, plus noise and breath. The ear hears the fundamental as the pitch. Software has to extract it from a messy signal.

From frequency to note

Notes are spaced logarithmically: each octave doubles the frequency. Using A4 = 440 Hz as the reference, the MIDI note number of a frequency f is:

n = 69 + 12 × log2(f / 440)

For 440 Hz this gives 69. For 220 Hz it gives 57 (A3). Rounding n to the nearest integer gives the note. The remainder tells you how far from perfectly in tune you are.

What is a cent?

A semitone is the gap between two neighbouring piano keys. A cent is one hundredth of a semitone, so 100 cents is one key. A trained singer might be within 10 to 20 cents; a casual hummer can drift 25 cents or more.

This matters because the note changes the instant the number crosses halfway to the next one, at 50 cents. A voice that wobbles around that boundary will flicker between two notes unless the software has a tolerance. That is why practical tools add a tolerance, say 65 or 85 cents, before declaring a new note.

Autocorrelation: the core idea

Many pitch detectors use autocorrelation. The idea is to compare the signal with a delayed copy of itself. A periodic signal matches itself well when the delay equals one period. Find the delay with the strongest match and you have the period; the frequency is its inverse.

It sounds simple, but it makes characteristic mistakes. A signal repeating every period also repeats every two periods, so the detector can pick a delay that is twice the true one and report a pitch an octave too low. Noise makes this worse.

The YIN algorithm

YIN, published by Alain de Cheveigné and Hideki Kawahara in 2002, is a refinement of autocorrelation designed to cut those errors. In outline:

  1. Compute a difference function that measures how different the signal is from a delayed copy of itself.
  2. Normalise it with a cumulative mean, so that small delays do not look artificially good.
  3. Apply an absolute threshold to pick the first delay that dips clearly low, rather than the global minimum, which reduces octave errors.
  4. Refine the estimate with parabolic interpolation to get sub-sample accuracy.

YIN also gives a confidence value, which is useful for ignoring breaths and silence.

Windows, hops and latency

Software analyses the audio in short windows, say 32 milliseconds, and moves forward by a small step, say 8 milliseconds. That gives a new pitch estimate many times a second, and the delay between your voice and the displayed note is roughly half a window plus one step. PitchPen tracks pitch about 125 times a second with a listening delay of about 24 milliseconds.

From pitch track to notes

A raw pitch track is not a melody. Turning it into notes needs more steps:

  • Confidence filtering: ignore moments the detector is unsure of.
  • Segmentation: decide where one note ends and another begins.
  • Tolerance: hold a note until the voice has clearly moved on.
  • Merging and dropping: join short gaps and ignore very short notes.
  • Key detection: compare notes to scale templates to suggest a key.

The cleanup is where tools differ most. Anyone can turn a pitch into a note name; deciding what you *meant* is the hard part. See how to turn humming into MIDI in FL Studio for the practical side.

Frequently asked questions

What is the fundamental frequency?

The lowest frequency of a periodic sound, and the one the ear usually hears as its pitch. Harmonics sit at whole-number multiples of it.

Why do detectors make octave errors?

Because a periodic signal also repeats at multiples of its period, a detector can lock on to the wrong repeat. Algorithms like YIN add steps to reduce this.

Is pitch detection the same as speech recognition?

No. Pitch detection finds how high a sound is. Speech recognition works out which words were said.

See how PitchPen applies pitch tracking and note cleanup.

Read the build story