Skip to content

Tune to a fraction of a cent: a new pitch detector and a ±3 cent window - #8

Merged
AndrianLeah merged 1 commit into
masterfrom
fix/tuning-accuracy
Dec 1, 2026
Merged

AndrianLeah merged 1 commit into
masterfrom
fix/tuning-accuracy

Conversation

@AndrianLeah

Copy link
Copy Markdown
Owner

Why

The tuner worked poorly in real use:

  • The microphone went through the browser's voice processing. getUserMedia was called with the defaults: echo cancellation, noise suppression and automatic gain all on. Tuned for speech, they fade a sustained note out as if it were noise and keep moving the level.
  • "In tune" meant ±8 cents. Two strings at +8 and −8 beat audibly against each other.
  • The needle was slow. A 22-frame median plus an EMA (α = 0.04) put it about 0.6 s behind a peg being turned.
  • The string tuned before still ringing pulled the reading. A period method (pitchy, and any autocorrelation) reads a blend of everything that rings: with the previous string 6 dB down, the next one read 15–22 cents flat. Players tuned it sharp to make the meter happy.
  • Bug: instant confirmation. A string that had hovered 8–20 cents off was confirmed at once when it crossed into the window, because its stability timer was never reset.
  • Bug: lost direction hint. The tune up / tune down hint disappeared if the string drifted far after it had once been close.
  • Startup on Safari and Android. The AudioContext was created after the permission prompt, which Safari keeps suspended. The Android manifest lacked MODIFY_AUDIO_SETTINGS.

What changed

Capture (useAudioCapture)

  • Echo cancellation, noise suppression and automatic gain are off.
  • The AudioContext is created within the tap that starts the tuner.
  • The noise gate follows the room's own noise level, down to −60 dBFS, so a quiet instrument (an unplugged electric) is still heard.
  • Every tick sends a frame, and the analysis and the low-pass filter are aimed at the string being tuned (setTarget). Changing tuning no longer restarts the microphone.

Detector (new: src/tuner/detector.ts and src/tuner/fft.ts, replacing pitchy)

  1. Coarse pitch. The McLeod Pitch Method on a decimated window (≥ 64 samples per period, so a 31 Hz bass B is cheap). The same overlap is used at every lag, which removes the spurious long-lag peaks pitchy has. Computed in double precision through one FFT pair.
  2. Fine pitch. The phase advance of the fundamental and of the 2nd harmonic between two Hann-windowed frames of 12 periods, as a strobe tuner measures. A string a third or a fourth away falls outside these narrow bands. Only this fine pitch is ever shown.
  3. Fundamental share. The energy at the pitch and its octave. B3 and E4 ringing together repeat at the period of E2 with no E2 in them; this phantom is now rejected.
  4. Harmonic spread. The cents between the two harmonics' pitches: small for a clean string or a blend of strings, wide when noise is in the bands.

Engine (src/tuner/engine.ts)

  • In tune within ±3 cents after at least 3 readings, and stays in until ±4 so it does not flicker at the edge.
  • Reads up to a fifth either way. An octave-up reading is folded to the string. Tuned strings still ringing, and phantoms, are ignored.
  • A 5-reading median and a needle smoother (60 ms steady, 25 ms while the pitch moves), weighted by how well the harmonics agree.
  • Time in tune before confirming: guitar 1 s, bass 1.2 s, ukulele 0.7 s, violin 0.6 s.
  • Choosing a string after every string was confirmed now keeps listening; before, the auto-stop timer still fired.

Measured on the virtual instrument

Before Now
Error on a held note — < 0.03 ¢ (< 0.2 ¢ in a noisy room)
Per-frame error at 10 dB SNR 4–15 ¢ (pitchy) < 1.4 ¢
Previous string ringing 6 dB down 15–22 ¢ flat < 1 ¢
"In tune" window ±8 ¢ ±3 ¢
Needle behind a turning peg ~600 ms 130–220 ms
First reading after a pluck (guitar) — 80–220 ms, already within 0.4 ¢

Testing

  • npm test: 287 tests, up from 243. npm run lint, vue-tsc -b and vite build are clean.
  • New detector tests: every preset string, noise, a stiff (inharmonic) string, a phone microphone on a bass, other strings ringing, phantoms and silence.
  • The virtual instrument now has room noise, inharmonicity and gliding notes. End-to-end tests cover accuracy on every string, a 5-cent-sharp string never being passed while the string below rings, an unplugged guitar, a noisy room, 44.1 kHz, stiff strings, phantoms and a turning peg.
  • Checked in a real browser (Chromium), with real BiquadFilter, AnalyserNode and AudioContext, playing synthetic strings into the app:
    • A2 at +5 ¢ read +5.00 ¢ with "tune down";
    • A2 at +0.3 ¢ confirmed in 1.1 s;
    • D3 a whole tone flat read "tune up";
    • A2 5 ¢ sharp over a just-replucked, equally loud E2 was never shown as in tune.

For the reviewer

  • Not yet tried with a real instrument on a phone. That is the check still missing.
  • Narrower green band. CentsMeter draws ±ACCEPT_CENTS, so the band is now ±3 %.
  • Two strings equally loud. If the previous string rings as loud as the new one (re-plucked a moment before), the tuner shows nothing until it fades, rather than a wrong value.
  • Low bass strings. The first reading takes about 0.35 s.
  • Dependency. pitchy is removed from package.json.

The tuner heard the strings through the browser's voice processing,
called ±8 cents in tune, followed a peg about 0.6 s late, and read a
string 5–20 cents flat while the one tuned before it still rang.

Capture (useAudioCapture):
- echo cancellation, noise suppression and automatic gain are off:
  they faded sustained notes out as noise and kept moving the level;
- the AudioContext is created within the tap that starts it, so Safari
  does not keep it suspended;
- the noise gate follows the room's own noise level, down to −60 dBFS,
  so a quiet instrument is still heard;
- every tick sends a frame, and the analysis and the low-pass filter
  are aimed at the string being tuned.

Detector (new, src/tuner/detector.ts, replacing pitchy):
- the McLeod Pitch Method on a decimated window with the same overlap
  at every lag, in double precision through one FFT pair;
- then a narrow-band phase measurement of the fundamental and the 2nd
  harmonic, as a strobe tuner does: another string ringing a third or
  a fourth away no longer pulls the reading;
- the share of energy at the pitch and its octave, which tells a real
  string from a phantom (B3 and E4 together repeat like an E2);
- the spread between the two harmonics, which tells noise from a blend
  of strings.

Engine:
- in tune within ±3 cents (and until ±4), after at least 3 readings;
- readings up to a fifth either way; a reading an octave up is the 2nd
  harmonic; tuned strings still ringing and phantoms are ignored;
- a 5-reading median, and a needle that follows a turning peg 130–220
  ms behind, trusting each reading by how well its harmonics agree;
- a string that hovered just off now needs its full time in tune (it
  was confirmed at once), and choosing a string once all were in tune
  keeps listening;
- time in tune: guitar 1 s, bass 1.2 s, ukulele 0.7 s, violin 0.6 s.

Android declares MODIFY_AUDIO_SETTINGS for the WebView's audio capture.
The virtual instrument gains room noise, stiff strings and gliding
notes; the tests cover accuracy, noise, other strings ringing,
phantoms, a phone microphone on a bass and a turning peg (287 tests).
The README's signal-processing section is rewritten.
@AndrianLeah
AndrianLeah merged commit 45f8564 into master Dec 1, 2026
1 check passed
@AndrianLeah
AndrianLeah deleted the fix/tuning-accuracy branch December 1, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant