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");