HLE/audio: defer loop_start skip to next frame and loop decode in pump #301

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

@⁠AscilCH on GitHub · 2026-11-24 23:25 UTC · original
AscilCH:fix/audio-xma2-loop-skip-and-pump → main · +136 −3 · 1 files · 1 commits · open

Problem

Two XMA2 audio playback bugs in crates/victory-hle/src/modules/audio.rs:

  1. loop_start skip applied to the loop_end frame:
    • In decode(), when loops > 0 && ends_loop, voice.skip was immediately overwritten with loop_start's subframe skip (word 1, bits 17..20) before consuming voice.skip on the current loop_end frame's pcm.
    • For any looping stream with loop_start_subframe > 0, this ate samples out of the loop_end frame (dropping the tail of the loop) and decremented voice.skip before the next frame at loop_start was decoded (emitting pre-loop samples instead of skipping them).
  2. pump() emits spurious 128-sample silence blocks on buffer switches and full-frame skips:
    • pump() used if voice.pcm.len() < SAMPLES_PER_BLOCK && !decode(context, voice) instead of a while loop.
    • Whenever a single decode() call returned true without pushing 128 samples — e.g., clearing an exhausted input buffer before switching to the second double-buffer, or skipping an entire 512-sample frame when start_skip >= 4 — pump() wrote a silent 256-byte block into the output ring buffer. Conversely, at EOF with < 128 tail samples left in voice.pcm, pump() returned early and dropped those tail samples.

Fix

  • In decode(), apply the current voice.skip to pcm and extend voice.pcm before storing loop_start's skip into voice.skip.
  • In pump(), replace the if with a while voice.pcm.len() < SAMPLES_PER_BLOCK loop (with a zero-progress cap of 16 empty steps, and flushing any remaining non-empty tail on EOF).
  • Add unit tests loop_end_keeps_current_samples_and_defers_loop_start_skip and pump_keeps_decoding_across_empty_steps_without_injecting_silence.

Verification

  • rustfmt --edition 2024 --check crates/victory-hle/src/modules/audio.rs (changed hunks): clean
  • cargo test -p victory-hle --locked audio -- --test-threads=1: 7 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-24 23:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/301) `AscilCH:fix/audio-xma2-loop-skip-and-pump` → `main` · +136 −3 · 1 files · 1 commits · open ## Problem Two XMA2 audio playback bugs in `crates/victory-hle/src/modules/audio.rs`: 1. **`loop_start` skip applied to the `loop_end` frame**: - In `decode()`, when `loops > 0 && ends_loop`, `voice.skip` was immediately overwritten with `loop_start`'s subframe skip (`word 1`, bits 17..20) *before* consuming `voice.skip` on the current `loop_end` frame's `pcm`. - For any looping stream with `loop_start_subframe > 0`, this ate samples out of the `loop_end` frame (dropping the tail of the loop) and decremented `voice.skip` before the next frame at `loop_start` was decoded (emitting pre-loop samples instead of skipping them). 2. **`pump()` emits spurious 128-sample silence blocks on buffer switches and full-frame skips**: - `pump()` used `if voice.pcm.len() < SAMPLES_PER_BLOCK && !decode(context, voice)` instead of a `while` loop. - Whenever a single `decode()` call returned `true` without pushing 128 samples — e.g., clearing an exhausted input buffer before switching to the second double-buffer, or skipping an entire 512-sample frame when `start_skip >= 4` — `pump()` wrote a silent 256-byte block into the output ring buffer. Conversely, at EOF with `< 128` tail samples left in `voice.pcm`, `pump()` returned early and dropped those tail samples. ## Fix - In `decode()`, apply the current `voice.skip` to `pcm` and extend `voice.pcm` before storing `loop_start`'s skip into `voice.skip`. - In `pump()`, replace the `if` with a `while voice.pcm.len() < SAMPLES_PER_BLOCK` loop (with a zero-progress cap of 16 empty steps, and flushing any remaining non-empty tail on EOF). - Add unit tests `loop_end_keeps_current_samples_and_defers_loop_start_skip` and `pump_keeps_decoding_across_empty_steps_without_injecting_silence`. ## Verification - `rustfmt --edition 2024 --check crates/victory-hle/src/modules/audio.rs` (changed hunks): clean - `cargo test -p victory-hle --locked audio -- --test-threads=1`: 7 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic tests only, no game data.)*
Fix two XMA2 audio decoding bugs in `crates/victory-hle/src/modules/audio.rs`:

1. In `decode()`, when a frame reached `loop_end` with `loops > 0`,
   `voice.skip` was overwritten with `loop_start`'s subframe skip *before*
   consuming `voice.skip` on the current `loop_end` frame. That dropped
   leading samples from the `loop_end` frame and cleared `voice.skip` before
   the next frame at `loop_start` was decoded. Apply the current `voice.skip`
   to `pcm` first and only then set `voice.skip` to `loop_start`'s skip.
2. In `pump()`, `if voice.pcm.len() < SAMPLES_PER_BLOCK && !decode(...)`
   called `decode()` at most once per output block. Whenever `decode()`
   returned `true` with fewer than 128 samples buffered (such as switching
   between double-buffered input buffers or skipping >= 4 subframes via
   `start_skip`), `pump()` emitted a spurious 128-sample silence block; and
   at EOF with `< 128` tail samples remaining, it returned early and dropped
   them. Loop `decode()` until a full block is ready or the stream ends
   (with a zero-progress bound).

Add unit tests `loop_end_keeps_current_samples_and_defers_loop_start_skip`
and `pump_keeps_decoding_across_empty_steps_without_injecting_silence`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:40:17 +00:00
Author
Owner

@⁠superboynick on GitHub · 2026-11-25 17:47 UTC · original

Numbers for the pump, gathered with an opt-in trace (PR #1046) on a run that reaches the prologue on an RTX 5070 Ti. One representative voice over 110 s of that run:

submissions 317,440 bytes total 2.8 KB/s process events 21 0.2/s queue depth 1 buffer, pending fell 35,330 -> 35,138 bytes

For reference, 48 kHz 16-bit stereo is 192 KB/s. So this voice is being fed at roughly 1.5% of real time, and the pump drains it at the same order of magnitude. Whatever the title believes about pacing, the amounts do not add up to continuous playback - which matches what it sounds like: a short fragment repeating rather than a clean dropout.

Completion is reported immediately after submission on this path, before any processing happens:

submit sub=2 bytes=45056 pending=43310 queue=1 complete sub=2 bytes=45056 pending=43310 queue=1 process sub=2 bytes=45056 pending=42415 queue=1 process sub=2 bytes=45056 pending=42415 queue=1

So the title is told its submit finished while ~42 KB of it is still pending. If the title uses completion to decide when to refill, that alone could explain both the low submission rate and the repetition.

Two questions this raises for the loop handling you are changing here: should completion wait for the pending bytes of that voice to drain rather than firing at submit time, and should the pump step be driven by bytes consumed instead of a fixed 2 ms tick? I do not know which the title expects; the trace above is what I can measure today.

No buffer contents in the trace: the hash covers 64 bytes only. AI-assisted.

**@⁠superboynick** on GitHub · 2026-11-25 17:47 UTC · [original](https://github.com/fxceless/victory-fork/pull/301#issuecomment-7169994873) Numbers for the pump, gathered with an opt-in trace (PR #1046) on a run that reaches the prologue on an RTX 5070 Ti. One representative voice over 110 s of that run: ` submissions 317,440 bytes total 2.8 KB/s process events 21 0.2/s queue depth 1 buffer, pending fell 35,330 -> 35,138 bytes ` For reference, 48 kHz 16-bit stereo is 192 KB/s. So this voice is being fed at roughly 1.5% of real time, and the pump drains it at the same order of magnitude. Whatever the title believes about pacing, the amounts do not add up to continuous playback - which matches what it sounds like: a short fragment repeating rather than a clean dropout. Completion is reported immediately after submission on this path, before any processing happens: ` submit sub=2 bytes=45056 pending=43310 queue=1 complete sub=2 bytes=45056 pending=43310 queue=1 process sub=2 bytes=45056 pending=42415 queue=1 process sub=2 bytes=45056 pending=42415 queue=1 ` So the title is told its submit finished while ~42 KB of it is still pending. If the title uses completion to decide when to refill, that alone could explain both the low submission rate and the repetition. Two questions this raises for the loop handling you are changing here: should completion wait for the pending bytes of that voice to drain rather than firing at submit time, and should the pump step be driven by bytes consumed instead of a fixed 2 ms tick? I do not know which the title expects; the trace above is what I can measure today. No buffer contents in the trace: the hash covers 64 bytes only. AI-assisted.
Author
Owner

@⁠superboynick on GitHub · 2026-11-25 18:05 UTC · original

Correction to my previous comment, before anyone acts on it.

I reported a single voice at 2.8 KB/s and compared it with 192 KB/s for real-time PCM. That comparison was wrong: that voice is one of 351, and most of them are short sound effects rather than a continuous stream. Summed across all voices in the same run, submissions are about 468 KB/s, which is not starved at all.

I also built the experiment my question implied - hold the completion until the voice's pending bytes drain, with a 200 ms ceiling - and measured both ways on the same machine and stage:

completion at submit time 60.6 s 777 submissions 28,374 KB 468 KB/s completion deferred 61.1 s 705 submissions 29,086 KB 476 KB/s

The difference is 1.6%, inside run-to-run noise. So the title does not pace its refills off our completion callback in any way that shows up here, and deferring it does not change the feed.

What stands from the earlier message is only the observation that completion fires while pending bytes remain, which is worth knowing but does not explain a repeated fragment by itself. I do not have a measurement that identifies the cause of the repetition yet, and I would rather retract the inference than have it sit in the thread as if it were established.

The tracing itself is unaffected and still available in #1046. AI-assisted.

**@⁠superboynick** on GitHub · 2026-11-25 18:05 UTC · [original](https://github.com/fxceless/victory-fork/pull/301#issuecomment-7170434651) Correction to my previous comment, before anyone acts on it. I reported a single voice at 2.8 KB/s and compared it with 192 KB/s for real-time PCM. That comparison was wrong: that voice is one of 351, and most of them are short sound effects rather than a continuous stream. Summed across all voices in the same run, submissions are about **468 KB/s**, which is not starved at all. I also built the experiment my question implied - hold the completion until the voice's pending bytes drain, with a 200 ms ceiling - and measured both ways on the same machine and stage: ` completion at submit time 60.6 s 777 submissions 28,374 KB 468 KB/s completion deferred 61.1 s 705 submissions 29,086 KB 476 KB/s ` The difference is 1.6%, inside run-to-run noise. So the title does not pace its refills off our completion callback in any way that shows up here, and deferring it does not change the feed. What stands from the earlier message is only the observation that completion fires while pending bytes remain, which is worth knowing but does not explain a repeated fragment by itself. I do not have a measurement that identifies the cause of the repetition yet, and I would rather retract the inference than have it sit in the thread as if it were established. The tracing itself is unaffected and still available in #1046. AI-assisted.
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/301/head:github-bot/gh-301'
git switch 'github-bot/gh-301'

Merge

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