fix(audio): pace blocking audio calls with a high-resolution timer #1486

Merged
popcoder565 merged 1 commit from upstream-pr/1486 into main 2026-10-10 01:03:00 +00:00
popcoder565 commented 2026-10-08 08:58:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T08:58:43Z.


What

On Windows, blocking audio calls paced themselves with nanosleep and this_thread::sleep_for/sleep_until. With MinGW these go through winpthreads, which ends a wait only on a 15.6 ms GetTickCount64 step (#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 every DRAIN_SLEEP_US, and for a port without a device, which sleeps in paceVirtualPort.
  • sceAudioOut2ContextPush: a blocking push on a full queue re-checked every FULL_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 every sleep_for(1 ms).

These calls now sleep with PreciseSleepUs, and with a new PreciseSleepUntil built on it, in libc/include/PreciseWait.hpp. Both use a high-resolution waitable timer on Windows and this_thread::sleep_for elsewhere.

With main merged in, AudioOut.cpp keeps #2130's sleepUs, which releases the port lock while it waits, and that wait is now PreciseSleepUs. Overlap: #1431 makes sleepUs retry nanosleep on EINTR. With this change sleepUs no longer calls nanosleep, and libstdc++'s sleep_for, which PreciseSleepUs uses on Linux, already retries on EINTR.

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_tests and guest_audio_in_tests do not load libkernel, so their probes raise the timer resolution to 0.5 ms first, as Time.cpp does at load.
  • Without that, a high-resolution timer in a process that has not raised the resolution waits 7 ms to 25 ms for a 16 ms request on this machine (5 ms requests stay precise).
  • The SDL audio driver was dummy for AudioOut, disk for AudioOut2, and none for AudioIn.
Call Expected main a5df1a87 This change
sceAudioOutOutput, device port 5.33 ms 187 to 188 of 300 under 1 ms, 112 to 113 over 10 ms, max 17.7 ms 3.1 to 8.5 ms, median 5.52 to 5.56 ms
sceAudioOutOutput, vibration port (no device) 5.33 ms 197 to 198 of 300 under 1 ms, 102 to 103 over 10 ms, max 20.4 ms 4.3 to 6.4 ms, median 5.2 to 5.3 ms
sceAudioOut2ContextPush, blocking 5.33 ms 195 to 196 of 300 under 1 ms, max 17.3 ms 150 of 300 under 1 ms, max 13.0 ms
sceAudio3dPortPush, blocking 5.33 ms 0.001 to 20.6 ms, average 7.8 ms 4.8 to 5.9 ms, average 5.33 ms
sceAudioInInput, no device 16 ms 0.9 to 31.0 ms, 43 to 54 of 100 more than 2 ms off 15.3 to 16.7 ms, none more than 2 ms off

AudioOut2 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.

  • Full ctest: 422 of 423 pass, all 12 audio tests included. The one failure is agc_driver_srgb8_color_target (#1434).

Not run on Linux, where both helpers are this_thread::sleep_for as before. No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4784347370 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1486). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T08:58:43Z. --- ### What On Windows, blocking audio calls paced themselves with `nanosleep` and `this_thread::sleep_for`/`sleep_until`. With MinGW these go through winpthreads, which ends a wait only on a 15.6 ms `GetTickCount64` step (#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 every `DRAIN_SLEEP_US`, and for a port without a device, which sleeps in `paceVirtualPort`. - **`sceAudioOut2ContextPush`:** a blocking push on a full queue re-checked every `FULL_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 every `sleep_for(1 ms)`. These calls now sleep with `PreciseSleepUs`, and with a new `PreciseSleepUntil` built on it, in `libc/include/PreciseWait.hpp`. Both use a high-resolution waitable timer on Windows and `this_thread::sleep_for` elsewhere. With `main` merged in, `AudioOut.cpp` keeps #2130's `sleepUs`, which releases the port lock while it waits, and that wait is now `PreciseSleepUs`. Overlap: #1431 makes `sleepUs` retry `nanosleep` on `EINTR`. With this change `sleepUs` no longer calls `nanosleep`, and libstdc++'s `sleep_for`, which `PreciseSleepUs` uses on Linux, already retries on `EINTR`. ### 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_tests` and `guest_audio_in_tests` do not load `libkernel`, so their probes raise the timer resolution to 0.5 ms first, as `Time.cpp` does at load. - Without that, a high-resolution timer in a process that has not raised the resolution waits 7 ms to 25 ms for a 16 ms request on this machine (5 ms requests stay precise). - The SDL audio driver was `dummy` for AudioOut, `disk` for AudioOut2, and `none` for AudioIn. | Call | Expected | `main` a5df1a87 | This change | |---|---|---|---| | `sceAudioOutOutput`, device port | 5.33 ms | 187 to 188 of 300 under 1 ms, 112 to 113 over 10 ms, max 17.7 ms | 3.1 to 8.5 ms, median 5.52 to 5.56 ms | | `sceAudioOutOutput`, vibration port (no device) | 5.33 ms | 197 to 198 of 300 under 1 ms, 102 to 103 over 10 ms, max 20.4 ms | 4.3 to 6.4 ms, median 5.2 to 5.3 ms | | `sceAudioOut2ContextPush`, blocking | 5.33 ms | 195 to 196 of 300 under 1 ms, max 17.3 ms | 150 of 300 under 1 ms, max 13.0 ms | | `sceAudio3dPortPush`, blocking | 5.33 ms | 0.001 to 20.6 ms, average 7.8 ms | 4.8 to 5.9 ms, average 5.33 ms | | `sceAudioInInput`, no device | 16 ms | 0.9 to 31.0 ms, 43 to 54 of 100 more than 2 ms off | 15.3 to 16.7 ms, none more than 2 ms off | AudioOut2 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. - Full ctest: 422 of 423 pass, all 12 audio tests included. The one failure is `agc_driver_srgb8_color_target` (#1434). Not run on Linux, where both helpers are `this_thread::sleep_for` as before. No title tested. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-08 11:57:55 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5456194910 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1486#pullrequestreview-5456194910). Originally posted by [MarcZX10](https://github.com/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.
adriadam10 commented 2026-10-09 23:33:46 +00:00 (Migrated from github.com)

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_for as 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):

  • Dropped the merge commit from the branch (CONTRIBUTING: no merge commits) by rebasing onto current main.
  • sleepUs in AudioOut.cpp (from #2130) now calls PreciseSleepUs instead of nanosleep, so the device-port and virtual-port waits are covered as the description says.

Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6091061490 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1486#issuecomment-6091061490). Originally posted by [adriadam10](https://github.com/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_for` as 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): - Dropped the merge commit from the branch (CONTRIBUTING: no merge commits) by rebasing onto current `main`. - `sleepUs` in `AudioOut.cpp` (from #2130) now calls `PreciseSleepUs` instead of `nanosleep`, so the device-port and virtual-port waits are covered as the description says. Merging once CI is green.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!1486
No description provided.