fix(audioout2): read grains at the next push and play them behind a cushion #839
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/839"
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 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_depth2. I recorded all four buffers at every push and checked which reading gives continuous audio:main)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:
sceAudioOut2ContextPushrecords the buffers the ports point at and reads the previous grain at the next push, at leastREAD_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.QueuePadGrainkeeps the pad queue level with the main output's unplayed grains, now counted from the FIFO instead of the SDL queue.ContextDestroycloses the device outside the context lock, because the callback takes that lock.The new
audio_out2_timingtest runs on the SDL disk audio driver (no sound hardware needed, so it runs on Linux CI) and checks:mainplays the content from the push.queue_depthtimes beforeSCE_AUDIO_OUT2_ERROR_QUEUE_FULL.It fails on
mainand passes 10 of 10 runs with the change.Tested
Windows 11, Radeon RX 9070 XT, PPSA12544 from boot into gameplay (bring-up: #726).
ctestpasses (235 tests).I have not checked other AudioOut2 titles.
Checklist
main; no other open PR implements the same functionsAudioOut2Context.cppare updated to match, with generic names)NotImplemented_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 adriadam10 on 2026-10-07T16:10:04Z.
Review: CHANGES_REQUESTED
Thanks for the measurements. Requested changes:
outputFIFO,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,:41and 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 (yieldloop) 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.queueDepthpushes (:339), yet the description says that model is unchanged. Either keep it unchanged or say so and test it.AudioOut2Context.cpp:193:QueuePadGrainnow always assumescushion + queueDepthgrains on the main output instead of the device level. That's a pad-routing behaviour change that isn't mentioned or tested.Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-07T19:21:18Z.
Thanks for the review. All points are addressed in
9a8a85eb:audio_out2_timingon 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 onmainand passed 10 of 10 runs here.main.QueuePadGrain: restored to the main output's level (unplayed grains, counted from the FIFO now that there is no SDL queue). The cushion plusqueue_depthvalue is only used without a main device, as before.Also merged
main. Non-GPU tests pass (235).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_timingfails without the change, passes 20/20 idle and 20/20 under full CPU load, and the full ctest is green merged ontomain. No lock-order problem between the SDL callback andPush/ContextDestroy.On the read timing: KytyPS5 copies the PCM in
sceAudioOut2PortSetAttributesand 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 whatqueue_depthis 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:pendingGrainskeeps(slot index, data)pairs. If a port is destroyed and another port of the same context takes its slot (AudioOut2Port.cpp:162reuses the first free one) before the next push,AudioOut2MixPortsreads 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 onPortDestroy, would avoid it. A test case that destroys and recreates a port between two pushes would pin it.READ_DELAYhas passed (e.g. from the callback when the FIFO runs dry), or noting it in TechnicalDebt, would cover it.docs/dev/TechnicalDebt.md:252ends without a period.Imported from boykopovar/AnyPS5. Originally posted by merwie on 2026-10-07T20:13:45Z.
Thanks for checking it under load. Addressed in
cd7735ce:AudioOut2MixPortsskips 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 newTestRecreatedPortinaudio_out2_timingpushes 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.Non-GPU tests pass (235).
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_timingfails without the change, and dropping the generation check makesTestRecreatedPortabort; 20/20 runs idle and 20/20 under full CPU load. Merging once Linux/Windows CI is green.