audio: avoid mixer stalls while decoding XMA voices #515

Open
github-bot wants to merge 2 commits from github-bot/gh-515 into main AGit
Owner

@⁠HolyRoses on GitHub · 2026-12-06 01:44 UTC · original
HolyRoses:main → main · +711 −132 · 6 files · 2 commits · open

Mixer read commands currently share the lock held while an XMA voice's output ring is decoded and filled. A long decode or a preempted decoder can therefore hold up the title's time-critical mixer, contributing to audio stalls and crackling.

Separate decoder state from ring bookkeeping so mixer reads take only the short ring-update lock. Decode and publish one output block per voice per round, releasing the voice lock between blocks, so other voices get a turn before one ring is filled. Reuse the scheduling list between passes.

The port retains existing overlap carry, input-refill handling, complete-block guards, read/write offset wrapping, decoder priority, and Opus/PCM conversion. Scheduling logic is adapted from the December 5 source snapshot identified as 67a25de.

Two regression tests verify that mixer reads proceed while decoder state is locked and that the scheduler visits the next voice before refilling the first.

Validation

  • Before rebasing onto the latest upstream, the patch passed 14 Windows audio tests and 6 Opus/PCM tests under Wine, plus 36 native format tests (4 ignored).
  • Windows x64 release build and diagnostics-enabled check passed on that base; all 170 replacement shader variants were compiled and validated.
  • After rebasing onto 459db13, cargo xwin check --locked -p larp-hle --target x86_64-pc-windows-msvc passed. The audio source is byte-for-byte identical to the previously tested version.

Native Windows cutscene playback has not yet been evaluated, so improvement to the reported desync/crackling still needs an in-game comparison.

**@⁠HolyRoses** on GitHub · 2026-12-06 01:44 UTC · [original](https://github.com/gamingwithhomie/larp/pull/515) `HolyRoses:main` → `main` · +711 −132 · 6 files · 2 commits · open Mixer read commands currently share the lock held while an XMA voice's output ring is decoded and filled. A long decode or a preempted decoder can therefore hold up the title's time-critical mixer, contributing to audio stalls and crackling. Separate decoder state from ring bookkeeping so mixer reads take only the short ring-update lock. Decode and publish one output block per voice per round, releasing the voice lock between blocks, so other voices get a turn before one ring is filled. Reuse the scheduling list between passes. The port retains existing overlap carry, input-refill handling, complete-block guards, read/write offset wrapping, decoder priority, and Opus/PCM conversion. Scheduling logic is adapted from the December 5 source snapshot identified as `67a25de`. Two regression tests verify that mixer reads proceed while decoder state is locked and that the scheduler visits the next voice before refilling the first. ### Validation - Before rebasing onto the latest upstream, the patch passed 14 Windows audio tests and 6 Opus/PCM tests under Wine, plus 36 native format tests (4 ignored). - Windows x64 release build and diagnostics-enabled check passed on that base; all 170 replacement shader variants were compiled and validated. - After rebasing onto `459db13`, `cargo xwin check --locked -p larp-hle --target x86_64-pc-windows-msvc` passed. The audio source is byte-for-byte identical to the previously tested version. Native Windows cutscene playback has not yet been evaluated, so improvement to the reported desync/crackling still needs an in-game comparison.
Give each XMA context separate decoder-state and ring-bookkeeping locks so
mixer read commands do not wait for decoding. Publish one output block per
voice per round and reuse the worker's scheduling list between passes.

Preserve overlap carry, input refill, complete-block output guards, ring
offset wrapping, decoder priority, and existing Opus/PCM conversion.

Port the scheduling logic from the December 5 source snapshot identified
as 67a25de. Add regression coverage for nonblocking mixer reads and fair
voice scheduling.

Validation on a8ad2c1 with this patch:
- 14 Windows audio tests and 6 Opus/PCM tests passed under Wine.
- 36 format tests passed; 4 ignored.
- Windows diagnostics check and x64 release build passed.
- All 170 replacement shader variants and runtime ZIP checksums verified.
- Native Windows cutscene playback remains untested.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-06 01:46:55 +00:00
Author
Owner

@⁠christopear on GitHub · 2026-12-06 02:01 UTC · original

I think the lock split leaves a Rust-side race on context word 1 in ef7eab2b. This follows from the access paths even without concurrent writes by the title.

  • The mixer in advance_read() holds slot.ring and uses set_field(1, 31, 1, 1) for a nonzero advance. That reads and writes the whole u32.
  • The worker holds slot.voice when step() reads that same word's writable bit, before acquiring the ring lock. There are also whole-word reads for other fields during decode, including channels(). The outer XMA lock is shared on both paths.
  • read_volatile and write_volatile do not synchronize those accesses. Even writing the same bit value, or reading a different field of the word, can race. The previous common voice lock serialized these two paths.

Could the decoder snapshot the needed shared words under a short ring lock, decode from the snapshot outside the lock, then recheck the live read position under the publication lock? I'd audit all word-1 reads when doing that. The existing progress test holds the voice mutex without actually reading the context concurrently, so it can still pass with this issue. This is a source review finding. I haven't attributed an audible glitch or crash to it.

**@⁠christopear** on GitHub · 2026-12-06 02:01 UTC · [original](https://github.com/gamingwithhomie/larp/pull/515#issuecomment-7449626511) I think the lock split leaves a Rust-side race on context word 1 in `ef7eab2b`. This follows from the access paths even without concurrent writes by the title. * The mixer in `advance_read()` holds `slot.ring` and uses `set_field(1, 31, 1, 1)` for a nonzero advance. That reads and writes the whole u32. * The worker holds `slot.voice` when `step()` reads that same word's writable bit, before acquiring the ring lock. There are also whole-word reads for other fields during decode, including `channels()`. The outer XMA lock is shared on both paths. * `read_volatile` and `write_volatile` do not synchronize those accesses. Even writing the same bit value, or reading a different field of the word, can race. The previous common voice lock serialized these two paths. Could the decoder snapshot the needed shared words under a short ring lock, decode from the snapshot outside the lock, then recheck the live read position under the publication lock? I'd audit all word-1 reads when doing that. The existing progress test holds the voice mutex without actually reading the context concurrently, so it can still pass with this issue. This is a source review finding. I haven't attributed an audible glitch or crash to it.
Snapshot context words under the ring mutex, then decode and copy PCM
without holding it. Merge decoder field updates under the same short
mutex and recheck the live read cursor when publishing a completed block.
Include enable/disable diagnostics in the locking protocol.

This addresses christopear's PR #515 review of word 1 and the equivalent
word-0 hazard (mixer ring-size reads versus decoder input/loop updates).
The voice mutex remains independent so mixer reads never wait on decoding.

Add concurrent real-XMA decoding/mixer coverage, masked-update and live
publication regressions, and input-refill coverage. Synchronize existing
background-decoder test observations too. Add a standalone native harness
that imports the actual audio module for reproducible race-detector runs.

Validation: the original patch plus the concurrent test reproduced the
reported word-1 race under ThreadSanitizer. All 17 corrected audio tests
pass normally and with instrumented-std ThreadSanitizer, diagnostics off
and on (including LARP_VOICE_LOG=1), on aarch64 macOS / Rust 1.98.1.
Windows x64 cargo xwin check of larp-hle and its tests with diagnostics
passes. Windows gameplay validation remains outstanding.
Author
Owner

@⁠HolyRoses on GitHub · 2026-12-06 02:39 UTC · original

@⁠christopear recheck pls

**@⁠HolyRoses** on GitHub · 2026-12-06 02:39 UTC · [original](https://github.com/gamingwithhomie/larp/pull/515#issuecomment-7450102525) @⁠christopear recheck pls
Author
Owner

@⁠christopear on GitHub · 2026-12-06 02:56 UTC · original

Rechecked 1f44510. My earlier objection about the mixer and decoder racing on context word 1 is resolved for those access paths.

I used the new concurrent test as a positive control against ef7eab2b on x86-64 Linux. ThreadSanitizer caught the race between the decoder read and the mixer update. On the revised head:

  • All 17 native audio tests passed normally and under ThreadSanitizer, each with diagnostics off and with diagnostics plus LARP_VOICE_LOG=1. I instrumented the standard library with nightly-2026-09-20.
  • The focused concurrent test also passed in five more fresh sanitizer runs per configuration.
  • I also cross-built the actual Windows MSVC release larp-hle library tests. All 17 modules::audio tests passed under Proton Experimental without the standalone shim.

The snapshot is taken under the ring lock, field updates merge into the live word under that lock, and publication checks the live read position again. This addresses both the unsynchronized access and the risk of losing a mixer advance during decode. I haven't measured audible stutter or cutscene behavior.

**@⁠christopear** on GitHub · 2026-12-06 02:56 UTC · [original](https://github.com/gamingwithhomie/larp/pull/515#issuecomment-7450316232) Rechecked `1f44510`. My earlier objection about the mixer and decoder racing on context word 1 is resolved for those access paths. I used the new concurrent test as a positive control against `ef7eab2b` on x86-64 Linux. ThreadSanitizer caught the race between the decoder read and the mixer update. On the revised head: * All 17 native audio tests passed normally and under ThreadSanitizer, each with diagnostics off and with diagnostics plus LARP_VOICE_LOG=1. I instrumented the standard library with nightly-2026-09-20. * The focused concurrent test also passed in five more fresh sanitizer runs per configuration. * I also cross-built the actual Windows MSVC release larp-hle library tests. All 17 modules::audio tests passed under Proton Experimental without the standalone shim. The snapshot is taken under the ring lock, field updates merge into the live word under that lock, and publication checks the live read position again. This addresses both the unsynchronized access and the risk of losing a mixer advance during decode. I haven't measured audible stutter or cutscene behavior.
Author
Owner

@⁠TrinityForever15 on GitHub · 2026-12-06 03:17 UTC · original

Ported onto amd-test v1.1.7 and running on RX 6650 XT. All 17 modules::audio unit tests pass, including this PR's xma_read_commands_do_not_wait_for_decoder_state, xma_decode_and_mixer_reads_share_context_safely, xma_visits_the_next_voice_before_refilling_the_first and xma_publication_uses_the_live_read_position. In play the XMA decoder thread starts and decodes normally, with no XMA errors in the log. I haven't done a dedicated before/after audio comparison, so I can't put a number on the stall reduction yet. https://github.com/iceboerg00/larp-amd/issues/64

**@⁠TrinityForever15** on GitHub · 2026-12-06 03:17 UTC · [original](https://github.com/gamingwithhomie/larp/pull/515#issuecomment-7450573573) Ported onto `amd-test` v1.1.7 and running on RX 6650 XT. All 17 `modules::audio` unit tests pass, including this PR's `xma_read_commands_do_not_wait_for_decoder_state`, `xma_decode_and_mixer_reads_share_context_safely`, `xma_visits_the_next_voice_before_refilling_the_first` and `xma_publication_uses_the_live_read_position`. In play the XMA decoder thread starts and decodes normally, with no XMA errors in the log. I haven't done a dedicated before/after audio comparison, so I can't put a number on the stall reduction yet. https://github.com/iceboerg00/larp-amd/issues/64
This pull request can be merged automatically.
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/515/head:github-bot/gh-515'
git switch 'github-bot/gh-515'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-515'
git switch 'github-bot/gh-515'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-515'
git switch 'github-bot/gh-515'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-515'
git switch 'main'
git merge --squash 'github-bot/gh-515'
git switch 'main'
git merge --ff-only 'github-bot/gh-515'
git switch 'main'
git merge 'github-bot/gh-515'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.