From 2945eff743e18c0b81d503065babdca1290cc191 Mon Sep 17 00:00:00 2001 From: syfyra <258908070+syfyra@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:13:56 +0100 Subject: [PATCH] silent_audio: pace the 10 ms passes with a high-resolution timer (SILENT-AUDIO-PASS-CADENCE) PeriodicThread waited for each pass with WaitForSingleObject and a timeout in milliseconds. On Windows a timeout ends on the system timer's tick, 15.625 ms for a process that has not raised the timer resolution, and nothing in the runtime does. The 10 ms period therefore woke once per tick: about 64 passes a second instead of 100. A spatial stream hands out 480 frames each pass, so the title, which activates ISpatialAudioClient first (docs/runtime_imports.md) and is paced by its event, rendered about 30,700 frames a second against 48,000: its audio clock ran at 64% of real time. The IAudioClient stream's event came at the same rate. The wait now uses a CREATE_WAITABLE_TIMER_HIGH_RESOLUTION timer, as the gameinput and d3d12_xs shims do, set to the next pass's due time, with the stop event beside it. Where the timer cannot be created, the timeout wait remains. Tested (Windows 11, MSVC 14.44): silent_audio_test records when each spatial pass's event arrives and checks the median gap of the nine after Start, unless the whole run took over 300 ms (a starved host, where the spacing says nothing about the timer). On main: 10 passes in 143 to 159 ms, median gap 15.6 to 17.2 ms, the check fails in 3 of 3 runs. With the fix: 10 passes in 90 ms, median gap 10.0 ms, all 130 checks pass in 3 of 3 runs. ctest -j 8 (162 CPU tests) passes apart from the system-commit check of xmem_test that #1316 addresses. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/silent_audio.cpp | 23 ++++++++++++++++++----- runtime/test/silent_audio_test.cpp | 17 ++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/runtime/shims/silent_audio.cpp b/runtime/shims/silent_audio.cpp index 0e02186bb3..0915d216eb 100644 --- a/runtime/shims/silent_audio.cpp +++ b/runtime/shims/silent_audio.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -197,25 +198,37 @@ class PeriodicThread { } private: + // A wait timeout ends on the system timer's tick: 15.625 ms on Windows for a process that has not raised the timer + // resolution, and nothing in the runtime does. A 10 ms period then woke once per tick, about 64 passes a second + // instead of 100, and a spatial stream, which hands out 480 frames a pass, ran its title's audio at 64% of real + // time. A high-resolution waitable timer (Windows 10 1803 and later) wakes within a fraction of a millisecond of + // its due time; where it cannot be created, the timeout wait remains. static DWORD WINAPI run(void* self) { auto* t = static_cast(self); const DWORD period_ms = static_cast((t->period_hns_ + 9999) / 10000); const int64_t f = qpc_frequency(); const int64_t step = t->period_hns_ * f / 10000000; + const HANDLE timer = + CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); int64_t next = qpc_now() + step; for (;;) { const int64_t now = qpc_now(); - DWORD wait = 0; - if (next > now) { - wait = static_cast((next - now) * 1000 / f); - if (wait > period_ms) wait = period_ms; + const int64_t left = next > now ? next - now : 0; + LARGE_INTEGER due; + due.QuadPart = -std::max(1, left * 10000000 / f); // relative, in 100 ns units + if (left && timer && SetWaitableTimer(timer, &due, 0, nullptr, nullptr, FALSE)) { + const HANDLE handles[2] = {t->stop_, timer}; + if (WaitForMultipleObjects(2, handles, FALSE, INFINITE) != WAIT_OBJECT_0 + 1) break; + } else { + const DWORD wait = static_cast(std::min(left * 1000 / f, period_ms)); + if (WaitForSingleObject(t->stop_, wait) != WAIT_TIMEOUT) break; } - if (WaitForSingleObject(t->stop_, wait) != WAIT_TIMEOUT) break; if (qpc_now() < next) continue; next += step; if (next < qpc_now()) next = qpc_now() + step; t->tick_(t->context_); } + if (timer) CloseHandle(timer); return 0; } diff --git a/runtime/test/silent_audio_test.cpp b/runtime/test/silent_audio_test.cpp index eac3121b9d..c0b3c5f483 100644 --- a/runtime/test/silent_audio_test.cpp +++ b/runtime/test/silent_audio_test.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -639,8 +640,10 @@ void test_spatial_stream() { QueryPerformanceCounter(&t0); int passes = 0; bool buffers_ok = true; + LARGE_INTEGER woke[10]{}; for (int i = 0; i < 10; ++i) { if (WaitForSingleObject(event, 500) != WAIT_OBJECT_0) break; + QueryPerformanceCounter(&woke[i]); if (FAILED(stream->BeginUpdatingAudioObjects(&available, &frames))) break; for (ISpatialAudioObject* o : {fl, fr}) { buffer = nullptr; @@ -655,9 +658,21 @@ void test_spatial_stream() { } QueryPerformanceCounter(&t1); const double seconds = static_cast(t1.QuadPart - t0.QuadPart) / static_cast(f.QuadPart); - std::printf(" spatial: %d passes in %.1f ms\n", passes, seconds * 1000.0); + // The first event comes from Start; the nine after it are one 10 ms period apart. Waking on the default 15.625 ms + // system timer tick made every gap one tick (10 passes in about 140 ms). A host so busy that the passes took over + // 300 ms starves every thread, so the gaps say nothing about the timer there and are only printed. + double gaps[9]{}; + for (int i = 1; i < 10; ++i) + gaps[i - 1] = static_cast(woke[i].QuadPart - woke[i - 1].QuadPart) * 1000.0 / + static_cast(f.QuadPart); + std::sort(gaps, gaps + 9); + std::printf(" spatial: %d passes in %.1f ms, median gap %.2f ms\n", passes, seconds * 1000.0, gaps[4]); check(passes == 10, "the spatial event is signalled every pass while running"); check(seconds > 0.04 && seconds < 2.0, "the passes come at about real time (10 in 40 ms .. 2 s)"); + if (passes == 10 && seconds < 0.3) + check(gaps[4] > 8.0 && gaps[4] < 12.5, "the passes are 10 ms apart, not one system timer tick"); + else + std::printf(" spatial: the host starved the passes; their spacing is not checked\n"); check(buffers_ok, "every object gets a 480-frame mono float buffer in each pass"); BOOL active = TRUE; check(fr->IsActive(&active) == S_OK && !active, "an object is inactive after the pass it ended in");