Skip to content

fix(tuner): move pitch detection off the audio thread - #306

Merged
OpenSauce merged 1 commit into
mainfrom
fix/tuner-off-rt-thread
Nov 28, 2026
Merged

OpenSauce merged 1 commit into
mainfrom
fix/tuner-off-rt-thread

Conversation

@OpenSauce

@OpenSauce OpenSauce commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #240

Problem

With the tuner open, the standalone xruns constantly. The output is silenced, so you hear nothing wrong, but the JACK xrun counter climbs.

The tuner ran its pitch search inside the audio callback. Once every 4096 samples, one callback did roughly 3.5M operations and allocated memory for the readout (format! for the note name, Arc::new, and dropping the old Arc). Measured in release:

Rate / block Tuner work in that callback Block budget
48 kHz / 128 1.3 ms 2.67 ms
96 kHz / 128 2.2 ms 1.33 ms

At 96 kHz that callback missed its deadline every time; smaller buffers or a Pi make it worse. The existing no-alloc test stopped just short of 4096 samples, so it never ran the search.

Fix

  • Audio thread: Tuner::process only pushes input samples into a lock-free crossbeam ArrayQueue. When the queue is full, the oldest sample is dropped. The worst case per callback is now about 0.002 ms.
  • GUI thread: TunerHandle::get_tuner_info empties the queue and runs detection there. The GUI already polls it every 20 ms while the tuner is open.
  • Reset on open: TunerHandle::reset() runs when the tuner opens, so leftover samples from the last time it was open don't produce a stale first reading.
  • Cleanup: removed arc-swap from rustortion-core, which no longer uses it, and corrected the stale engine → GUI line in CLAUDE.md.

Detection accuracy fixes

These bugs existed before this change. Fixing them first required working out what the tuner was actually reading.

  • Wrong octave. AMDF summed differences without averaging them, so longer lags scored better and it picked multiples of the real period: a 110 Hz tone read as 55 Hz. Replaced with YIN's difference function, which compares every lag over the same window and takes the first dip below a threshold.
  • Octave number off by one below C4. freq_to_note used integer division that rounds toward zero. It now uses div_euclid.
  • Bass low strings unreadable. The search floor moves from 50.1 Hz to 30 Hz, which covers a 5-string low B. Input above 48 kHz is averaged down before analysis, so the 4096-sample window still covers 30 Hz and GUI-thread cost stays about 1.5–2.5 ms per analysis at any rate.
  • High notes read about 5 cents flat. Detection only resolved whole-sample periods. Parabolic interpolation now refines the period between samples.

Readings for harmonic-rich test tones, B0 through C6, at 48, 96 and 192 kHz are all within 0.1 cent and name the right octave.

Testing

  • tuner_does_not_allocate in no_alloc.rs drives the engine well past the analysis window. Before the fix it failed with 16 allocations.
  • Tuner unit tests: octave labels, fundamental detection with a strong 2nd harmonic, and a sweep from B0 to C6 at 44.1/48/96 kHz with a ±2 cent limit.
  • make lint and make test
  • Manual: open the tuner on JACK and confirm the xrun counter stays flat

The tuner ran its pitch search inside the JACK callback once every 4096
samples (~3.5M ops plus readout allocations), blowing the block budget
and causing xruns while the tuner was open. The RT side now only pushes
samples into a lock-free queue; TunerHandle drains it and analyses on
the GUI thread.

Also fixes detection accuracy:
- YIN difference function replaces AMDF, which favoured long lags and
  read e.g. 110 Hz as 55 Hz
- octave numbers below C4 were one too high (truncating division)
- floor lowered from 50.1 Hz to 30 Hz so bass low E/B are readable;
  input above 48 kHz is averaged down to keep the window long enough
- parabolic interpolation removes the ~5 cent integer-lag error on
  high notes
@OpenSauce
OpenSauce marked this pull request as ready for review November 28, 2026 17:54
@OpenSauce
OpenSauce enabled auto-merge (squash) November 28, 2026 17:54
@OpenSauce
OpenSauce merged commit 52dc209 into main Nov 28, 2026
10 checks passed
@OpenSauce
OpenSauce deleted the fix/tuner-off-rt-thread branch November 28, 2026 17:57
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.

Fix xruns triggered by the tuner

1 participant