Audio: refill XMA rings from a decoded reserve on the title's thread #1880

Open
github-bot wants to merge 1 commit from github-bot/gh-1880 into main AGit
Owner

@⁠beamdotdev on GitHub · 2026-11-25 00:00 UTC · original
beamdotdev:audio/xma-reserve → main · +99 −24 · 1 files · 1 commits · open

Audio was choppy: XMA rings ran dry under load.

Why

  • The XMA decoding thread ran at normal priority, while the title's mixer that consumes its output is time-critical; the title's workers took the CPU from it.
  • A block the title freed (read-advance command) was refilled only after the decoding thread was scheduled and had decoded the next frame.
  • A voice whose inputs both ran out stayed silent until re-enabled, so a stream refilled a little late dropped out.

What

  • Each voice keeps two frames (~21 ms) decoded ahead of its ring. ADVANCE_XMA_READ copies from that reserve into the freed blocks at once, on the title's thread; the decoding thread then tops the reserve up.
  • The decoding thread runs THREAD_PRIORITY_TIME_CRITICAL. It works only while a ring has room or a reserve is short, so it cannot hog a core.
  • When new input arrives after a voice ran out, it resumes from a cold decoder with the error status cleared.
  • A partial block waits for more input rather than being padded with silence, except once the input has run out.
  • imdct_half's pre/post rotations are computed once per transform size instead of per channel and subframe (bit-identical output).

Testing

  • cargo test -p victory-hle audio and cargo test -p victory-formats xma pass; clippy is clean on both crates.
  • xma_contexts_decode_into_their_output_rings now also covers the refill from the reserve on read-advance and resuming after input runs out.
  • Not yet checked by ear in a title. VICTORY_AUDIO_CAPTURE (diagnostics build) can record before/after.

🤖 Generated with Claude Code

**@⁠beamdotdev** on GitHub · 2026-11-25 00:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1880) `beamdotdev:audio/xma-reserve` → `main` · +99 −24 · 1 files · 1 commits · open Audio was choppy: XMA rings ran dry under load. ## Why - The XMA decoding thread ran at normal priority, while the title's mixer that consumes its output is time-critical; the title's workers took the CPU from it. - A block the title freed (read-advance command) was refilled only after the decoding thread was scheduled and had decoded the next frame. - A voice whose inputs both ran out stayed silent until re-enabled, so a stream refilled a little late dropped out. ## What - Each voice keeps two frames (~21 ms) decoded ahead of its ring. `ADVANCE_XMA_READ` copies from that reserve into the freed blocks at once, on the title's thread; the decoding thread then tops the reserve up. - The decoding thread runs `THREAD_PRIORITY_TIME_CRITICAL`. It works only while a ring has room or a reserve is short, so it cannot hog a core. - When new input arrives after a voice ran out, it resumes from a cold decoder with the error status cleared. - A partial block waits for more input rather than being padded with silence, except once the input has run out. - `imdct_half`'s pre/post rotations are computed once per transform size instead of per channel and subframe (bit-identical output). ## Testing - `cargo test -p victory-hle audio` and `cargo test -p victory-formats xma` pass; clippy is clean on both crates. - `xma_contexts_decode_into_their_output_rings` now also covers the refill from the reserve on read-advance and resuming after input runs out. - Not yet checked by ear in a title. `VICTORY_AUDIO_CAPTURE` (diagnostics build) can record before/after. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The decoding thread ran at normal priority while the title's mixer is
time-critical, and a freed ring block was only refilled once that thread
was scheduled and had decoded, so rings ran dry under load.

* Each voice keeps two frames decoded ahead of its ring; the read-advance
  command copies from that reserve into the freed blocks at once.
* The decoding thread runs time-critical (it works only while a ring has
  room).
* A voice whose inputs ran out resumes, from a cold decoder, when the
  title gives it new input, instead of staying silent until re-enabled.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:47:32 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:37 UTC · original · edited 2026-11-25 02:28 UTC

Automated triage for the maintainers.

The reserve-and-refill part (fill called from ADVANCE_XMA_READ, plus the decoding thread topping up two frames ahead) is a reasonable way to keep XMA rings from running dry. However, the PR also changes what the emulated XMA context does, and gives no evidence for it. In decode in crates/victory-hle/src/modules/audio.rs, a voice whose input ran out now resumes when new input arrives, from a cold decoder, with context dword 2 zeroed (context.set(2, 0)). Enabling only clears the error bits (set_field(2, 26, 6, 0) in Xma::enable), so the comment "as enabling does" does not match the code, and the read offset is reset too. A partial block now waits for more input instead of being padded. Whether the hardware resumes a context that ran out, and from where, is the kind of runtime behaviour that needs a reference: the title's own handling of that state, or public documentation. The description also says the change has not been heard in a title. To move forward: give evidence for the resume semantics (or split them out, so the reserve and the thread priority can be judged alone), and attach a before/after VICTORY_AUDIO_CAPTURE comparison or a listening report from the title.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/1880#issuecomment-7149766886) · edited 2026-11-25 02:28 UTC <!-- victory-triage v1 head=3de75792f89cd2e10ece1f5db2d66a8742bb5394 verdict=needs-work at=2026-11-25T02:20:29Z --> Automated triage for the maintainers. The reserve-and-refill part (`fill` called from `ADVANCE_XMA_READ`, plus the decoding thread topping up two frames ahead) is a reasonable way to keep XMA rings from running dry. However, the PR also changes what the emulated XMA context does, and gives no evidence for it. In `decode` in `crates/victory-hle/src/modules/audio.rs`, a voice whose input ran out now resumes when new input arrives, from a cold decoder, with context dword 2 zeroed (`context.set(2, 0)`). Enabling only clears the error bits (`set_field(2, 26, 6, 0)` in `Xma::enable`), so the comment "as enabling does" does not match the code, and the read offset is reset too. A partial block now waits for more input instead of being padded. Whether the hardware resumes a context that ran out, and from where, is the kind of runtime behaviour that needs a reference: the title's own handling of that state, or public documentation. The description also says the change has not been heard in a title. To move forward: give evidence for the resume semantics (or split them out, so the reserve and the thread priority can be judged alone), and attach a before/after `VICTORY_AUDIO_CAPTURE` comparison or a listening report from the title. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠beamdotdev on GitHub · 2026-11-25 02:05 UTC · original

Rebased onto main (8fb0b28). The IMDCT rotation cache is dropped from this PR: main's own (#172) covers it. One test from main that builds a Voice now uses the renamed field. victory-hle tests: 142 pass; the one failure, dstorage::a_page_list_filled_after_its_request_starts, fails on main too.

**@⁠beamdotdev** on GitHub · 2026-11-25 02:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/1880#issuecomment-7150313659) Rebased onto main (8fb0b28). The IMDCT rotation cache is dropped from this PR: main's own (#172) covers it. One test from main that builds a Voice now uses the renamed field. victory-hle tests: 142 pass; the one failure, dstorage::a_page_list_filled_after_its_request_starts, fails on main too.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1880/head:github-bot/gh-1880'
git switch 'github-bot/gh-1880'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1880'
git switch 'github-bot/gh-1880'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1880'
git switch 'github-bot/gh-1880'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1880'
git switch 'main'
git merge --squash 'github-bot/gh-1880'
git switch 'main'
git merge --ff-only 'github-bot/gh-1880'
git switch 'main'
git merge 'github-bot/gh-1880'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1880
No description provided.