From ea72b01524a825bf3c40e42d57f433cceaf3b519 Mon Sep 17 00:00:00 2001 From: lowlevel-larry Date: Mon, 2 Nov 2026 09:47:35 -0300 Subject: [PATCH] Pace audio frames apart and raise the Linux prefill default Device-paced frames rendered back to back after each period pull (1024 samples on PipeWire, 4-5 frames), before the game's audio thread could clear the render event or fill the AUX return ring. Frames now stay at least 4/5 of a frame apart, as the DSP renders one frame every 5 ms on hardware. This fixes AUX clicks at frame seams (#426) and the progressive sound loss of #131 (overlapping render notifications 52.7% -> 0.1%). Linux prefill default raised to 60 ms: at 20 ms PipeWire underran thousands of times within minutes in busy scenes. The reverb ring (#421) and advance_voice stepping parts of this change landed in dev separately (d22a19c, b44caca). Fixes #131 Co-Authored-By: Claude Opus 5.5 --- runtime/audio_mixer.c | 15 +++++++++++---- runtime/audren.c | 13 ++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index 4c212e9c..c82c7b79 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -8,7 +8,7 @@ // into its destination mix, folds the submixes, runs the mix effects and writes the device sink's stereo mix into a // single-producer single-consumer ring. The SDL audio thread drains that ring without taking any lock: an empty ring // plays silence and never blocks the render thread or the guest. TOTK_AUDIO=none (or TOTK_AUDIO_OFF) mixes without a -// device; TOTK_AUDIO_PREFILL_MS (0..500, default 20) is the queued audio the output waits for after start or underrun. +// device; TOTK_AUDIO_PREFILL_MS (0..500, default 20, 60 on Linux) is the queued audio the output waits for after start or underrun. // The device's clock paces the render thread, as the audio hardware paces the Switch's DSP (audio_mixer_pace_ns): a // frame is rendered when the ring holds less than the output target, max(prefill, two device periods), so the queue and // the delay from the game to the speakers stay there and the host and device clocks cannot drift apart. LASRecomp and @@ -90,8 +90,15 @@ static void report_once(Mixer *m, unsigned bit, const char *msg, int value) { // ---- output: SDL3 audio stream fed from the ring // How much audio the output waits for before it starts, and again after an underrun: TOTK_AUDIO_PREFILL_MS rounded up // to whole 5 ms render blocks. Returns -1 for a value outside 0..500 ms. +// The Linux default is higher: at 20 ms PipeWire's queue (max(prefill, two 1024-sample periods)) underran +// thousands of times within minutes in busy scenes, and 60 ms removed the underruns. +#ifdef __linux__ +#define PREFILL_DEFAULT_MS 60 +#else +#define PREFILL_DEFAULT_MS 20 +#endif static int prefill_blocks_for(const char *e) { - if (!e || !*e) return 4; + if (!e || !*e) return PREFILL_DEFAULT_MS / 5; char *end; double ms = strtod(e, &end); if (end == e || *end || !(ms >= 0) || ms > 500) return -1; @@ -318,8 +325,8 @@ void audio_mixer_open(AudioRenderer *r) { const char *pe = cfg_get("TOTK_AUDIO_PREFILL_MS"); m->prefill = prefill_blocks_for(pe); if (m->prefill < 0) { - rt_log("audio: TOTK_AUDIO_PREFILL_MS=%s is not 0..500 ms: using 20", pe); - m->prefill = 4; + rt_log("audio: TOTK_AUDIO_PREFILL_MS=%s is not 0..500 ms: using %d", pe, PREFILL_DEFAULT_MS); + m->prefill = PREFILL_DEFAULT_MS / 5; } r->mixer = m; atomic_store(&m->playing, r->state == 0); diff --git a/runtime/audren.c b/runtime/audren.c index 2b3ca42e..4d127161 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -352,7 +352,13 @@ static void render_thread(void *p) { plat_thread_priority(PLAT_PRIO_HIGHEST); const uint64_t frame_ns = (uint64_t)r->param.sample_count * 1000000000ull / (uint64_t)r->param.sample_rate; const double frame = (double)r->param.sample_count / r->param.sample_rate; - uint64_t next = kern_now_ns(); + // When the device pulls a period (1024 samples on PipeWire, 4-5 frames) the ring drops below its target + // and the frames would otherwise render back to back. On hardware the DSP renders one frame every 5 ms and + // the game's audio thread works in between; AUX effects need that time to fill the return ring, or mix_aux + // reads stale samples and the frame clicks at its seams. Device-paced frames stay at least 4/5 of a frame + // apart, which still refills a period well before the device drains it. + const uint64_t gap_ns = frame_ns * 4 / 5; + uint64_t next = kern_now_ns(), last = 0; while (plat_cas32(&r->running, 0, 0)) { uint64_t now = kern_now_ns(); int64_t pace = audio_mixer_pace_ns(r, now); @@ -360,6 +366,10 @@ static void render_thread(void *p) { plat_wait_ns((uint64_t)pace); continue; } + if (pace == 0 && last && now - last < gap_ns) { + plat_wait_ns(gap_ns - (now - last)); + continue; + } if (pace == 0) { next = now + frame_ns; // where the timer goes on if the device stops pacing } else { @@ -367,6 +377,7 @@ static void render_thread(void *p) { else if (now - next > 100000000ull) next = now; // fell far behind (debugger, sleep): resynchronise next += frame_ns; } + last = kern_now_ns(); plat_mutex_lock(&r->lock); if (r->state == 0) { voices_prepare(r);