DEV Community

Nova Lumen Labs
Nova Lumen Labs

Posted on

Why a plain FFT tuner locks onto the wrong note, and what I used instead (McLeod Pitch Method on iOS)

I'm a solo developer at a small studio, and I recently shipped a guitar tuner and metronome called Maestro. The tuner was the part I got wrong first, so this post is about that.

The naive approach fails on real instruments

The obvious way to build a tuner: take a window of microphone samples, run an FFT, pick the tallest peak, convert to a note. It works on a sine wave. On a real guitar it doesn't, because the loudest bin is often the 2nd or 3rd harmonic, not the fundamental. A low E (about 82 Hz) will happily read as the E an octave up, or as a B, depending on the pluck.

What I used: the McLeod Pitch Method (MPM)

MPM computes a normalized square-difference autocorrelation of the window. Instead of looking for the biggest peak in frequency space, it looks for repeating structure in time, and normalizes by signal energy at each lag so quiet tails don't lose to loud attacks. Then it picks the first strong peak, which is the fundamental, and refines it with parabolic interpolation for sub-sample precision (this is what gets you to a fraction of a cent).

A few practical details from the implementation:

  • Range: roughly 30 Hz to 2 kHz, so a 5-string bass low B and a violin's top register both fit. A 4096-frame buffer holds more than two periods at 30 Hz, which keeps the autocorrelation meaningful.
  • Two gates before accepting a result: an RMS floor (so digital silence and room hiss don't run the expensive part) and a clarity threshold on the normalized peak height (I use 0.6) so breath noise doesn't show a phantom pitch.
  • The heavy loops go through Accelerate's vDSP, so analysis fits inside one tap callback (about 93 ms of audio) even on older phones.
  • The detector is a pure function: samples plus sample rate in, optional frequency plus clarity out. That made it easy to unit test against synthesized sine waves, with no microphone involved. Smoothing and note-name math live in a separate tracker.

The metronome and the rest

The metronome covers 40 to 240 BPM with tap tempo and haptics, and there is a practice log with streaks. The tuner and basic metronome are free; a Pro tier adds extra tunings, insights and export. It is on iOS and Android.

What I'd still like feedback on

Percussive, very low notes on cheap phone mics are the hardest case for me. If you build audio tools and have seen better gating for that, I'd like to hear it.

Links (I'm the developer):

Disclosure: I used an AI assistant to help draft and edit this post; the technical details come from the app's source code.

Top comments (0)