fix(audioout2): read grains at the next push and play them behind a cushion #839

Merged
merwie merged 4 commits from upstream-pr/839 into main 2026-10-07 20:29:12 +00:00
merwie commented 2026-10-06 08:46:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-06T08:46:00Z.


What

AudioOut2 contexts with an SDL device now read a grain's port buffers at the next push. The device callback plays the mix behind a cushion, and the queue level leaves that cushion out.

Before this, a push mixed the ports' buffers immediately and queued the mix on SDL. The queue level also left out a 40 ms cushion that the SDL queue rarely went past, so titles almost always saw an empty queue.

PPSA12544 (Hollow Knight: Silksong, Unity with FMOD) outputs through one stereo float port, with four 256-frame buffers used in rotation and queue_depth 2. I recorded all four buffers at every push and checked which reading gives continuous audio:

Buffers read Broken grain boundaries
at the push (main) 27%
at the next push 0 of 3,993
two pushes later 59%

FMOD fills a buffer after pushing it and reuses it two pushes later. Its mixer thread also runs concurrently, and the title pushes in bursts. When the next push follows within microseconds, the buffer can still be half written. Without a wait, 4–9% of boundaries broke; with a 10 µs wait, 1 broke; with 50 µs or more, none did. So a grain is read no earlier than 100 µs after its own push.

Changes:

  • sceAudioOut2ContextPush records the buffers the ports point at and reads the previous grain at the next push, at least READ_DELAY (100 µs) after that grain's push. If the next push comes sooner, it waits for the rest of the 100 µs with the context lock released, so the device callback is never blocked. For a title that fills its buffers before pushing, the read only moves later, which is harmless unless the title rewrites a pushed buffer before the next push.
  • The mixed frames go to a FIFO that the SDL device callback plays. The callback waits until a 40 ms cushion is mixed before it plays, and again after running dry (counted as a prime).
  • The queue level counts the unplayed grains (unread plus mixed) beyond that cushion.
  • Without an SDL device, nothing changes: each push is mixed at once and the wall-clock model gives the level.
  • Pad routing is unchanged: QueuePadGrain keeps the pad queue level with the main output's unplayed grains, now counted from the FIFO instead of the SDL queue.
  • ContextDestroy closes the device outside the context lock, because the callback takes that lock.
  • A TechnicalDebt entry records that the read timing is measured rather than known, since when the hardware reads the buffers is not known.

The new audio_out2_timing test runs on the SDL disk audio driver (no sound hardware needed, so it runs on Linux CI) and checks:

  • Next-push read: a buffer rewritten after its push plays the rewritten content. main plays the content from the push.
  • Queue level: non-blocking pushes are accepted at least cushion + queue_depth times before SCE_AUDIO_OUT2_ERROR_QUEUE_FULL.
  • Priming: after the output ran dry for 300 ms, a second burst plays without holes.

It fails on main and passes 10 of 10 runs with the change.

Tested

Windows 11, Radeon RX 9070 XT, PPSA12544 from boot into gameplay (bring-up: #726).

  • The audible crackle is gone. Over about 6,000 loud grains of the output, the boundaries with a jump more than 20 times the local median went from 4–9% to none. A mid-grain control sits at 0.01–0.02%.
  • 187 pushes/s, 0 primes, 0 dropped, about 40 ms of unplayed audio.
  • Non-GPU ctest passes (235 tests).

I have not checked other AudioOut2 titles.

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 (the existing explanatory comments in AudioOut2Context.cpp are updated to match, with generic names)
  • 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:4756354768 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839). Originally posted by [merwie](https://github.com/merwie) on 2026-10-06T08:46:00Z. --- ### What AudioOut2 contexts with an SDL device now read a grain's port buffers at the next push. The device callback plays the mix behind a cushion, and the queue level leaves that cushion out. Before this, a push mixed the ports' buffers immediately and queued the mix on SDL. The queue level also left out a 40 ms cushion that the SDL queue rarely went past, so titles almost always saw an empty queue. PPSA12544 (Hollow Knight: Silksong, Unity with FMOD) outputs through one stereo float port, with four 256-frame buffers used in rotation and `queue_depth` 2. I recorded all four buffers at every push and checked which reading gives continuous audio: | Buffers read | Broken grain boundaries | |---|---| | at the push (`main`) | 27% | | at the next push | 0 of 3,993 | | two pushes later | 59% | FMOD fills a buffer *after* pushing it and reuses it two pushes later. Its mixer thread also runs concurrently, and the title pushes in bursts. When the next push follows within microseconds, the buffer can still be half written. Without a wait, 4–9% of boundaries broke; with a 10 µs wait, 1 broke; with 50 µs or more, none did. So a grain is read no earlier than 100 µs after its own push. Changes: - `sceAudioOut2ContextPush` records the buffers the ports point at and reads the previous grain at the next push, at least `READ_DELAY` (100 µs) after that grain's push. If the next push comes sooner, it waits for the rest of the 100 µs with the context lock released, so the device callback is never blocked. For a title that fills its buffers before pushing, the read only moves later, which is harmless unless the title rewrites a pushed buffer before the next push. - The mixed frames go to a FIFO that the SDL device callback plays. The callback waits until a 40 ms cushion is mixed before it plays, and again after running dry (counted as a prime). - The queue level counts the unplayed grains (unread plus mixed) beyond that cushion. - Without an SDL device, nothing changes: each push is mixed at once and the wall-clock model gives the level. - Pad routing is unchanged: `QueuePadGrain` keeps the pad queue level with the main output's unplayed grains, now counted from the FIFO instead of the SDL queue. - `ContextDestroy` closes the device outside the context lock, because the callback takes that lock. - A TechnicalDebt entry records that the read timing is measured rather than known, since when the hardware reads the buffers is not known. The new `audio_out2_timing` test runs on the SDL disk audio driver (no sound hardware needed, so it runs on Linux CI) and checks: - **Next-push read:** a buffer rewritten after its push plays the rewritten content. `main` plays the content from the push. - **Queue level:** non-blocking pushes are accepted at least cushion + `queue_depth` times before `SCE_AUDIO_OUT2_ERROR_QUEUE_FULL`. - **Priming:** after the output ran dry for 300 ms, a second burst plays without holes. It fails on `main` and passes 10 of 10 runs with the change. ### Tested Windows 11, Radeon RX 9070 XT, PPSA12544 from boot into gameplay (bring-up: #726). - The audible crackle is gone. Over about 6,000 loud grains of the output, the boundaries with a jump more than 20 times the local median went from 4–9% to none. A mid-grain control sits at 0.01–0.02%. - 187 pushes/s, 0 primes, 0 dropped, about 40 ms of unplayed audio. - Non-GPU `ctest` passes (235 tests). I have not checked other AudioOut2 titles. ### 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) (the existing explanatory comments in `AudioOut2Context.cpp` are updated to match, with generic names) - [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)
adriadam10 commented 2026-10-07 16:10:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T16:10:04Z.

Review: CHANGES_REQUESTED


Thanks for the measurements. Requested changes:

  • No test. The change is all timing and concurrency (new SDL callback taking the context lock, output FIFO, pendingGrains), so it needs a Linux test that reproduces the behaviour in CI (e.g. SDL dummy audio driver: buffers read at the next push and not before, queue level excluding the cushion, priming after running dry). A log from the title alone can't be re-run by us.
  • AudioOut2Context.cpp:23, :41 and the TechnicalDebt line name PPSA12544 / Demon's Souls. Generic names only, and no new code comments (CONTRIBUTING); keep the title evidence in the PR description or a Discussion.
  • AudioOut2Context.cpp:341-343: the push busy-waits (yield loop) 100 µs on every push while holding the context lock, which the SDL callback also needs. 100 µs comes from one title's mixer thread; please explain why it is safe in general or avoid spinning under the lock.
  • Without an SDL device the read is now delayed queueDepth pushes (:339), yet the description says that model is unchanged. Either keep it unchanged or say so and test it.
  • AudioOut2Context.cpp:193: QueuePadGrain now always assumes cushion + queueDepth grains on the main output instead of the device level. That's a pad-routing behaviour change that isn't mentioned or tested.
  • Unverified on our side: audible result on Windows with PPSA12544 and the boundary percentages.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5445052506 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839#pullrequestreview-5445052506). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T16:10:04Z. **Review:** CHANGES_REQUESTED --- Thanks for the measurements. Requested changes: - No test. The change is all timing and concurrency (new SDL callback taking the context lock, `output` FIFO, `pendingGrains`), so it needs a Linux test that reproduces the behaviour in CI (e.g. SDL dummy audio driver: buffers read at the next push and not before, queue level excluding the cushion, priming after running dry). A log from the title alone can't be re-run by us. - `AudioOut2Context.cpp:23`, `:41` and the TechnicalDebt line name PPSA12544 / Demon's Souls. Generic names only, and no new code comments (CONTRIBUTING); keep the title evidence in the PR description or a Discussion. - `AudioOut2Context.cpp:341-343`: the push busy-waits (`yield` loop) 100 µs on every push while holding the context lock, which the SDL callback also needs. 100 µs comes from one title's mixer thread; please explain why it is safe in general or avoid spinning under the lock. - Without an SDL device the read is now delayed `queueDepth` pushes (`:339`), yet the description says that model is unchanged. Either keep it unchanged or say so and test it. - `AudioOut2Context.cpp:193`: `QueuePadGrain` now always assumes `cushion + queueDepth` grains on the main output instead of the device level. That's a pad-routing behaviour change that isn't mentioned or tested. - Unverified on our side: audible result on Windows with PPSA12544 and the boundary percentages.
merwie commented 2026-10-07 19:21:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-07T19:21:18Z.


Thanks for the review. All points are addressed in 9a8a85eb:

  • Test: added audio_out2_timing on the SDL disk audio driver, which runs without sound hardware, so CI can run it on Linux. It checks the next-push read, the queue level beyond the cushion, and priming after 300 ms dry. It fails on main and passed 10 of 10 runs here.
  • Title names: the comments and the TechnicalDebt line are generic now. The title evidence stays in the description.
  • Spinning under the lock: the push now waits with the context lock released, so the callback is never blocked. It only waits when the next push comes within 100 µs of the previous one, and only for the rest of the 100 µs. Why I think the delay is safe in general: for a title that fills its buffers before pushing, it only moves the read later, which can matter only if the title rewrites a pushed buffer within 100 µs. The value itself is measured, not known, and the TechnicalDebt line says so.
  • No-device path: reverted. Without a device, each push is mixed at once, as on main.
  • QueuePadGrain: restored to the main output's level (unplayed grains, counted from the FIFO now that there is no SDL queue). The cushion plus queue_depth value is only used without a main device, as before.
  • Unverified on your side: understood. The boundary numbers come from recorded buffers, so I can attach the capture tool and a recording if that helps.

Also merged main. Non-GPU tests pass (235).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6045140385 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839#issuecomment-6045140385). Originally posted by [merwie](https://github.com/merwie) on 2026-10-07T19:21:18Z. --- Thanks for the review. All points are addressed in 9a8a85eb: - **Test:** added `audio_out2_timing` on the SDL disk audio driver, which runs without sound hardware, so CI can run it on Linux. It checks the next-push read, the queue level beyond the cushion, and priming after 300 ms dry. It fails on `main` and passed 10 of 10 runs here. - **Title names:** the comments and the TechnicalDebt line are generic now. The title evidence stays in the description. - **Spinning under the lock:** the push now waits with the context lock released, so the callback is never blocked. It only waits when the next push comes within 100 µs of the previous one, and only for the rest of the 100 µs. Why I think the delay is safe in general: for a title that fills its buffers before pushing, it only moves the read later, which can matter only if the title rewrites a pushed buffer within 100 µs. The value itself is measured, not known, and the TechnicalDebt line says so. - **No-device path:** reverted. Without a device, each push is mixed at once, as on `main`. - **`QueuePadGrain`:** restored to the main output's level (unplayed grains, counted from the FIFO now that there is no SDL queue). The cushion plus `queue_depth` value is only used without a main device, as before. - **Unverified on your side:** understood. The boundary numbers come from recorded buffers, so I can attach the capture tool and a recording if that helps. Also merged `main`. Non-GPU tests pass (235).
adriadam10 commented 2026-10-07 20:05:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T20:05:29Z.

Review: CHANGES_REQUESTED


Thanks for the test, it covers what we asked. Checked locally: audio_out2_timing fails without the change, passes 20/20 idle and 20/20 under full CPU load, and the full ctest is green merged onto main. No lock-order problem between the SDL callback and Push/ContextDestroy.

On the read timing: KytyPS5 copies the PCM in sceAudioOut2PortSetAttributes and prosper mixes at the push, but those are emulator choices, and your per-push capture of all four buffers is direct evidence. The hardware also plays a grain after its push (that's what queue_depth is for), so reading later is closer to it. A title that rewrites a buffer right after pushing would be wrong on the console too. Fine with the change. What's left:

  • pendingGrains keeps (slot index, data) pairs. If a port is destroyed and another port of the same context takes its slot (AudioOut2Port.cpp:162 reuses the first free one) before the next push, AudioOut2MixPorts reads the old pointer with the new port's format and channel count. Storing the port's format with the captured pointer, or dropping a context's pending entries on PortDestroy, would avoid it. A test case that destroys and recreates a port between two pushes would pin it.
  • The last pushed grain is only read at the next push, so it never plays if the title stops pushing. Reading it once READ_DELAY has passed (e.g. from the callback when the FIFO runs dry), or noting it in TechnicalDebt, would cover it.
  • docs/dev/TechnicalDebt.md:252 ends without a period.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5447702381 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839#pullrequestreview-5447702381). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T20:05:29Z. **Review:** CHANGES_REQUESTED --- Thanks for the test, it covers what we asked. Checked locally: `audio_out2_timing` fails without the change, passes 20/20 idle and 20/20 under full CPU load, and the full ctest is green merged onto `main`. No lock-order problem between the SDL callback and `Push`/`ContextDestroy`. On the read timing: KytyPS5 copies the PCM in `sceAudioOut2PortSetAttributes` and prosper mixes at the push, but those are emulator choices, and your per-push capture of all four buffers is direct evidence. The hardware also plays a grain after its push (that's what `queue_depth` is for), so reading later is closer to it. A title that rewrites a buffer right after pushing would be wrong on the console too. Fine with the change. What's left: - `pendingGrains` keeps `(slot index, data)` pairs. If a port is destroyed and another port of the same context takes its slot (`AudioOut2Port.cpp:162` reuses the first free one) before the next push, `AudioOut2MixPorts` reads the old pointer with the new port's format and channel count. Storing the port's format with the captured pointer, or dropping a context's pending entries on `PortDestroy`, would avoid it. A test case that destroys and recreates a port between two pushes would pin it. - The last pushed grain is only read at the next push, so it never plays if the title stops pushing. Reading it once `READ_DELAY` has passed (e.g. from the callback when the FIFO runs dry), or noting it in TechnicalDebt, would cover it. - `docs/dev/TechnicalDebt.md:252` ends without a period.
merwie commented 2026-10-07 20:13:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-07T20:13:45Z.


Thanks for checking it under load. Addressed in cd7735ce:

  • Reused port slot: each port gets a generation when it is created, and a captured grain records it next to the slot and pointer. AudioOut2MixPorts skips an entry whose slot now holds a different generation, so a port created in a destroyed port's slot is never mixed with the old pointer. The new TestRecreatedPort in audio_out2_timing pushes a stereo port, destroys it, creates a mono port in the same slot (it checks the handle is reused) and pushes again. Only the mono port's grain plays. Without the generation check, the test fails.
  • Last grain: I noted it in TechnicalDebt instead of reading it from the callback. A read from the callback would mix (and queue pad audio) on SDL's audio thread, and it would read buffers earlier than the measured timing whenever a title is merely late. If you prefer the read, I can add it in a follow-up.
  • Period: added.

Non-GPU tests pass (235).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6045994660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839#issuecomment-6045994660). Originally posted by [merwie](https://github.com/merwie) on 2026-10-07T20:13:45Z. --- Thanks for checking it under load. Addressed in cd7735ce: - **Reused port slot:** each port gets a generation when it is created, and a captured grain records it next to the slot and pointer. `AudioOut2MixPorts` skips an entry whose slot now holds a different generation, so a port created in a destroyed port's slot is never mixed with the old pointer. The new `TestRecreatedPort` in `audio_out2_timing` pushes a stereo port, destroys it, creates a mono port in the same slot (it checks the handle is reused) and pushes again. Only the mono port's grain plays. Without the generation check, the test fails. - **Last grain:** I noted it in TechnicalDebt instead of reading it from the callback. A read from the callback would mix (and queue pad audio) on SDL's audio thread, and it would read buffers earlier than the measured timing whenever a title is merely late. If you prefer the read, I can add it in a follow-up. - **Period:** added. Non-GPU tests pass (235).
adriadam10 commented 2026-10-07 20:29:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T20:29:09Z.

Review: APPROVED


Thanks, the generation check covers the reused slot. Checked merged onto main: full ctest green, audio_out2_timing fails without the change, and dropping the generation check makes TestRecreatedPort abort; 20/20 runs idle and 20/20 under full CPU load. Merging once Linux/Windows CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5447958780 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/839#pullrequestreview-5447958780). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-07T20:29:09Z. **Review:** APPROVED --- Thanks, the generation check covers the reused slot. Checked merged onto `main`: full ctest green, `audio_out2_timing` fails without the change, and dropping the generation check makes `TestRecreatedPort` abort; 20/20 runs idle and 20/20 under full CPU load. Merging once Linux/Windows 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!839
No description provided.