A browser guitar tuner works by feeding your mic into a WebAudio AnalyserNode, then running autocorrelation on each buffer to find the fundamental frequency. Convert that frequency to the nearest semitone and display cents-off. Zro7 Instrument Tuner uses this approach — accurate to ±1 cent on a clean signal.
Why autocorrelation, not FFT
FFT tells you which frequencies are present with amplitude — great for spectrograms, weak for the fundamental when a plucked string is dominated by harmonics. Autocorrelation asks 'at what lag does the signal repeat?' which nails the fundamental even with strong overtones. The bin resolution isn't quantized to FFT frequency bins, so accuracy is sub-Hz.
The algorithm (simplified)
- Get ~2048 samples with
analyser.getFloatTimeDomainData. - Compute autocorrelation:
r[k] = Σ x[i] * x[i+k]for k in the plausible period range (e.g. 40 Hz–1000 Hz). - Find the first peak after the initial trough — its lag
kis the period. - Frequency = sampleRate / k. Parabolic interpolation between neighbours gives sub-sample accuracy.
From frequency to note and cents
Use the 12-TET formula: semitone = 12 * log2(freq / 440) + 69. Round to nearest integer for note; the fractional part × 100 gives cents-off from perfect.
Steps
- Open Instrument Tuner.
- Grant mic permission; pluck one string cleanly.
- The dial shows the detected note and cents-off.
- Tune until the dial is centered and reads within ±2 cents.
Zro7