Skip to content

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

Merged
adriadam10 merged 4 commits into
boykopovar:mainfrom
merwie:fix/audioout2-read-grain-at-playback
Oct 7, 2026
Merged

adriadam10 merged 4 commits into
boykopovar:mainfrom
merwie:fix/audioout2-read-grain-at-playback

Conversation

@merwie

@merwie merwie commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

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

…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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and others added 2 commits October 7, 2026 21:11
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>
@merwie

merwie commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

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

  • 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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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>
@merwie

merwie commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking it under load. Addressed in cd7735c:

  • 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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@adriadam10
adriadam10 merged commit fb0d627 into boykopovar:main Oct 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants