From ada341e2b11472a65454d783ac4a019ff8101a50 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 31 Oct 2026 07:12:48 +0300 Subject: [PATCH] Pace audio rendering by the output device The render thread ran on its own 5 ms timer while the device consumed on its own clock, so the queue crept: on DirectSound it grew 2.7 ms every 5 s, which fills the 340 ms ring in about ten minutes of play. The render thread now renders the next frame when the device has taken the ring below max(prefill, two device periods), as the audio hardware paces the Switch's DSP and as LASRecomp and SavageWindRecomp pace their renderers by the SDL stream. Without a device, while the renderer is stopped and while the device takes nothing for 50 ms (at least four periods) it keeps the timer, so a stalled device does not stall the game; the ring trim then drops what piled up, measured from the target. 60 s on the monsters save: WASAPI 15 ms queued, DirectSound 27-40 ms without growth, no underruns. With the SDL audio thread suspended for 1.5 s the game held 60 fps and the queue was back at 15 ms after one trim. Co-Authored-By: Claude Opus 5.5 --- runtime/audio_mixer.c | 44 ++++++++++++++++++++++++++++++++++++------- runtime/audren.c | 31 ++++++++++++++++++++---------- runtime/audren.h | 4 ++++ 3 files changed, 62 insertions(+), 17 deletions(-) diff --git a/runtime/audio_mixer.c b/runtime/audio_mixer.c index a34885c7..6dc625b8 100644 --- a/runtime/audio_mixer.c +++ b/runtime/audio_mixer.c @@ -9,9 +9,13 @@ // 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. -// The render thread runs on the host clock and the device on its own, and after a stall the render thread catches up in -// a burst, so audio can pile up in the ring; the SDL thread skips what is queued beyond the prefill plus QUEUE_SLACK_MS, -// which keeps the delay from the game to the speakers at prefill plus one device period instead of up to the ring size. +// 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 +// SavageWindRecomp pace their renderers by the SDL stream the same way. Without a device, while the renderer is +// stopped and while the device takes nothing (a stall), the render thread keeps real time on its own timer; what piled +// up meanwhile is skipped on the SDL thread once the device resumes (more than QUEUE_SLACK_MS above the target plus the +// device's request), as MacNativeGames' audio_mixer.c trims its ring after a device stall. #include #include #include @@ -29,7 +33,8 @@ #define MAX_FRAME 1024 #define RING_FRAMES 16384 // stereo frames, about 340 ms at 48 kHz -#define QUEUE_SLACK_MS 40 // queued audio above prefill plus the device's request that ring_pull skips +#define QUEUE_SLACK_MS 40 // queued audio above the target plus the device's request that ring_pull skips +#define STALL_MS 50 // the device took nothing this long (at least 4 periods): the timer paces #define SPLITTER_UNUSED 0xffffffffu #define dm_guest_write_prepare(p, n) ((void)(p), (void)(n)) // guest memory is plain host memory on Windows @@ -49,6 +54,9 @@ typedef struct Mixer { float ring[RING_FRAMES * 2]; _Atomic uint64_t wr, rd, underruns, overruns, missing_frames, callback_frames, callback_nonzero, skipped_frames; _Atomic int playing; int ready; // ready: the callback's own state + int target, period; // output target and device period, frames (output_open) + uint64_t stall_ns; + _Atomic uint64_t pulled_ns; // kern_now_ns of the device's last request, 0 before the first _Atomic uint64_t flush_until; _Atomic unsigned stop_epoch; unsigned callback_epoch; EffectState *effect; int *effect_order; unsigned effect_reported; uint64_t *sink_address; uint32_t *sink_size, *sink_offset, *sink_old, *sink_written; @@ -121,8 +129,9 @@ static void ring_pull(Mixer *m, float *out, uint32_t frames) { } m->ready = 1; } - uint64_t keep = prefill + frames, slack = (uint64_t)m->rate * QUEUE_SLACK_MS / 1000; - if (have > keep + slack) { // audio piled up (render burst, clock drift): skip the oldest, keep prefill + request + uint64_t keep = (prefill > (uint64_t)m->target ? prefill : (uint64_t)m->target) + frames; + uint64_t slack = (uint64_t)m->rate * QUEUE_SLACK_MS / 1000; + if (have > keep + slack) { // piled up while the device stalled: skip the oldest, keep target + request atomic_fetch_add(&m->skipped_frames, have - keep); rd = wr - keep; have = keep; @@ -149,6 +158,7 @@ static void SDLCALL sdl_pull(void *user, SDL_AudioStream *stream, int additional (void)total; Mixer *m = user; float tmp[2 * 1024]; + atomic_store_explicit(&m->pulled_ns, kern_now_ns(), memory_order_release); while (additional > 0) { uint32_t frames = (uint32_t)additional / (2 * sizeof(float)); if (!frames) break; @@ -182,8 +192,28 @@ static void output_open(Mixer *m, int rate) { return; } m->device_ok = 1; + SDL_AudioSpec dev; + int period = 0; + if (!SDL_GetAudioDeviceFormat(SDL_GetAudioStreamDevice(m->stream), &dev, &period) || period <= 0) period = m->frame; + m->period = period; + m->target = m->frame * m->prefill > 2 * period ? m->frame * m->prefill : 2 * period; + if (m->target > RING_FRAMES / 2) m->target = RING_FRAMES / 2; + uint64_t period_ns = (uint64_t)period * 1000000000ull / (uint64_t)rate; + m->stall_ns = 4 * period_ns > STALL_MS * 1000000ull ? 4 * period_ns : STALL_MS * 1000000ull; const char *name = SDL_GetAudioDeviceName(SDL_GetAudioStreamDevice(m->stream)); - rt_log("audio: SDL device '%s' opened, %d Hz stereo float, prefill %d ms", name ? name : "?", rate, m->prefill * 5); + rt_log("audio: SDL device '%s' opened, %d Hz stereo float, prefill %d ms, device period %d frames; the device paces " + "rendering at %.1f ms queued", name ? name : "?", rate, m->prefill * 5, period, m->target * 1000.0 / rate); +} + +int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now) { + Mixer *m = r->mixer; + if (!m || !m->device_ok || !atomic_load_explicit(&m->playing, memory_order_acquire)) return -1; + uint64_t pulled = atomic_load_explicit(&m->pulled_ns, memory_order_acquire); + if (!pulled || (now > pulled && now - pulled > m->stall_ns)) return -1; + uint64_t have = atomic_load(&m->wr) - atomic_load_explicit(&m->rd, memory_order_acquire); + if (have < (uint64_t)m->target) return 0; + int64_t ns = (int64_t)((have - (uint64_t)m->target + 1) * 1000000000ull / (uint64_t)m->rate); + return ns < 500000 ? 500000 : ns; // until the device has taken it below the target } // Render thread only. diff --git a/runtime/audren.c b/runtime/audren.c index f031d031..8a379c0b 100644 --- a/runtime/audren.c +++ b/runtime/audren.c @@ -316,7 +316,8 @@ static void request_update(AudioRenderer *r, const uint8_t *input, uint64_t in_s ReleaseSRWLockExclusive(&r->lock); } -// ---- render tick: one frame every sample_count / sample_rate seconds, on a host thread +// ---- render tick: one frame each time the output device has taken the ring below its target (audio_mixer_pace_ns), +// or every sample_count / sample_rate seconds of the host clock without a device, while stopped or while it stalls static void advance_voice(AudioRenderer *r, ServerVoice *v, double out_seconds) { if (!v->in.in_use || v->state != SV_STARTED || !v->in.sample_rate) return; double want = v->fraction + out_seconds * v->in.sample_rate * (v->in.pitch > 0 ? v->in.pitch : 1.0); @@ -348,6 +349,12 @@ static void advance_voice(AudioRenderer *r, ServerVoice *v, double out_seconds) } } +static void wait_ns(HANDLE timer, uint64_t ns) { + LARGE_INTEGER due = {.QuadPart = -(LONGLONG)((ns + 99) / 100)}; // relative, 100 ns units + if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) rt_fatal("audren: SetWaitableTimer failed (error %lu)", GetLastError()); + WaitForSingleObject(timer, INFINITE); +} + static DWORD WINAPI render_thread(void *p) { AudioRenderer *r = p; SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); @@ -358,6 +365,19 @@ static DWORD WINAPI render_thread(void *p) { const double frame = (double)r->param.sample_count / r->param.sample_rate; uint64_t next = kern_now_ns(); while (InterlockedCompareExchange(&r->running, 0, 0)) { + uint64_t now = kern_now_ns(); + int64_t pace = audio_mixer_pace_ns(r, now); + if (pace > 0) { + wait_ns(timer, (uint64_t)pace); + continue; + } + if (pace == 0) { + next = now + frame_ns; // where the timer goes on if the device stops pacing + } else { + if (next > now) wait_ns(timer, next - now); + else if (now - next > 100000000ull) next = now; // fell far behind (debugger, sleep): resynchronise + next += frame_ns; + } AcquireSRWLockExclusive(&r->lock); if (r->state == 0) { voices_prepare(r); @@ -374,15 +394,6 @@ static DWORD WINAPI render_thread(void *p) { } ReleaseSRWLockExclusive(&r->lock); kern_event_signal(r->event); - next += frame_ns; - uint64_t now = kern_now_ns(); - if (next > now) { - LARGE_INTEGER due = {.QuadPart = -(LONGLONG)((next - now + 99) / 100)}; // relative, 100 ns units - if (!SetWaitableTimer(timer, &due, 0, NULL, NULL, FALSE)) - rt_fatal("audren: SetWaitableTimer failed (error %lu)", GetLastError()); - WaitForSingleObject(timer, INFINITE); - } else if (now - next > 100000000ull) - next = now; // fell far behind (debugger, sleep): resynchronise } CloseHandle(timer); return 0; diff --git a/runtime/audren.h b/runtime/audren.h index 92da770d..6dc74bc7 100644 --- a/runtime/audren.h +++ b/runtime/audren.h @@ -180,5 +180,9 @@ void audio_mixer_update_effects(AudioRenderer *r); void audio_mixer_effect_result(AudioRenderer *r, int id, void *output, unsigned size); uint32_t audio_mixer_sink_written(AudioRenderer *r, int id); void audio_mixer_render(AudioRenderer *r, double seconds); // after audio_voices_prepare, before voices advance +// Device pacing for the render thread at kern_now_ns `now`: 0 renders the next frame now, a positive value waits that +// many ns (the ring is at its target), -1 leaves the pace to the render thread's timer (no device, renderer stopped, +// device stalled). +int64_t audio_mixer_pace_ns(AudioRenderer *r, uint64_t now); int audio_buffer_empty(const AudrenWaveBuffer *b); // no samples to decode: played as soon as it is reached void audio_device_handler(IpcSession *s, const IpcRequest *rq, IpcResponse *rs); // IAudioDevice