Repository navigation
fix(audioout2): read grains at the next push and play them behind a cushion - #839
Conversation
…ushion A push mixed the ports' buffers at once and queued the mix on SDL, and the queue level left out a 40 ms cushion that the SDL queue rarely exceeded, so titles saw an empty queue. PPSA12544's FMOD output fills a 256-frame buffer after pushing it, reuses it two pushes later, and its mixer thread can still be writing a buffer when the next push follows within microseconds: a grain boundary broke about every 5 ms (an audible crackle). A push now records the buffers the ports point at and reads the previous grain at the next push, 100 us after it. The mixed frames are played by the SDL device callback, which waits for a 40 ms cushion before it plays (again after running dry), and the queue level counts the unplayed grains beyond that cushion. Recording the four buffers at every push showed 27% of boundaries broken when read at the push, 59% when read two pushes later and none when read at the next push; the title's output went from 4-9% to none of its grain boundaries broken over 6,000 loud grains. AI-assisted: yes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
adriadam10
left a comment
There was a problem hiding this comment.
Thanks for the measurements. Requested changes:
- No test. The change is all timing and concurrency (new SDL callback taking the context lock,
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.- Without an SDL device the read is now delayed
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.- Unverified on our side: audible result on Windows with PPSA12544 and the boundary percentages.
…audioout2-read-grain-at-playback
Address review: read a grain's buffers at the next push without spinning under the context lock, keep the no-device model mixing at push time, size the pad queue from the unplayed main grains again, use generic names in comments and TechnicalDebt, and add audio_out2_timing (next-push read, queue level beyond the cushion, priming after running dry). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. All points are addressed in 9a8a85e:
Also merged |
adriadam10
left a comment
There was a problem hiding this comment.
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:
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.- The last pushed grain is only read at the next push, so it never plays if the title stops pushing. Reading it once
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.
A captured grain stored only the port slot and data pointer, so a port created in a destroyed port's slot before the next push mixed the old pointer with its own format. Ports now carry a generation that the capture records. TechnicalDebt notes that the last grain pushed before a title stops is never played. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for checking it under load. Addressed in cd7735c:
Non-GPU tests pass (235). |
adriadam10
left a comment
There was a problem hiding this comment.
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.
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