fix(audio): pace blocking audio calls with a high-resolution timer #1486
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1486"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T08:58:43Z.
What
On Windows, blocking audio calls paced themselves with
nanosleepandthis_thread::sleep_for/sleep_until. With MinGW these go through winpthreads, which ends a wait only on a 15.6 msGetTickCount64step (#1471), so a game's audio thread ran in bursts:sceAudioOutOutput: a blocking output returned at once 2 times in 3 and then waited about 16 ms. This held for a device port, which re-checks the SDL queue everyDRAIN_SLEEP_US, and for a port without a device, which sleeps inpaceVirtualPort.sceAudioOut2ContextPush: a blocking push on a full queue re-checked everyFULL_WAIT_STEP, so it returned in groups of 3 about every 16 ms.sceAudio3dPortPush: a blocking push slept until a frame had played. It woke up to 15.6 ms late, and frames left to play run from now once the queue has emptied. A port with 5.33 ms frames therefore took 7.8 ms per push on average, about 68 % of real time.sceAudioInInput: without a device it sleeps until the next block, with intervals from 0.9 ms to 31 ms instead of 16 ms. With a device, it polls the SDL queue everysleep_for(1 ms).These calls now sleep with
PreciseSleepUs, and with a newPreciseSleepUntilbuilt on it, inlibc/include/PreciseWait.hpp. Both use a high-resolution waitable timer on Windows andthis_thread::sleep_forelsewhere.With
mainmerged in,AudioOut.cppkeeps #2130'ssleepUs, which releases the port lock while it waits, and that wait is nowPreciseSleepUs. Overlap: #1431 makessleepUsretrynanosleeponEINTR. With this changesleepUsno longer callsnanosleep, and libstdc++'ssleep_for, whichPreciseSleepUsuses on Linux, already retries onEINTR.Tested
Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H.
I added probe modes to the audio tests locally (not in this PR). Each makes 100 to 400 blocking calls in a row and times the intervals between returns with
steady_clock.guest_audio3d_port_testsandguest_audio_in_testsdo not loadlibkernel, so their probes raise the timer resolution to 0.5 ms first, asTime.cppdoes at load.dummyfor AudioOut,diskfor AudioOut2, andnonefor AudioIn.maina5df1a87sceAudioOutOutput, device portsceAudioOutOutput, vibration port (no device)sceAudioOut2ContextPush, blockingsceAudio3dPortPush, blockingsceAudioInInput, no deviceAudioOut2 still comes in groups after this change. Its queue level counts the grains the SDL device has not played yet, and the device consumes 512 samples (two grains) per callback. Pushes therefore return in pairs every 10.7 ms instead of in threes every 16 ms.
agc_driver_srgb8_color_target(#1434).Not run on Linux, where both helpers are
this_thread::sleep_foras before. No title tested.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T11:57:55Z.
Review: APPROVED
Reviewed the complete diff, PR description, and current Linux, Windows, Conventions, and Progress report checks. The helper preserves the non-Windows sleep path, and the audio call sites retain their existing deadline/queue logic while using precise waits. I found no issues.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T23:33:46Z.
Checked the diff and a scoped build + ctest of the Audio3d/AudioIn/AudioOut/libc tests on Linux (green; the non-Windows path is
sleep_foras before). The Windows numbers in the PR are the author's and weren't re-measured here.Added on top, as one linear commit (
b2cde4a0f):main.sleepUsinAudioOut.cpp(from #2130) now callsPreciseSleepUsinstead ofnanosleep, so the device-port and virtual-port waits are covered as the description says.Merging once CI is green.