Audio: an XMA loop's skip is of its first frame, not of its last #194

Manually merged
github merged 1 commit from gh-pr/194/head into gh-pr/194/base 2026-11-26 03:54:07 +00:00
Owner

@⁠Maraco91 on GitHub · 2026-11-24 01:18 UTC · original
Maraco91:audio/xma-loop-start-skip → main · +72 −2 · 1 files · 1 commits · merged 2026-11-25 00:15 UTC

What

A looping XMA context with a loop-start skip plays the wrong samples at every turn of the loop.

When a frame ends a loop, decode cuts it at the loop's end subframe, sets voice.skip from the context ("loop start subframes to skip", word 1 bits 17-19) and jumps back to the loop's start. But the lines right after it apply voice.skip to the frame in hand, which is the loop's last frame:

if loops > 0 && ends_loop {
    pcm.truncate(keep);
    next = loop start;
    voice.skip = loop skip;          // meant for the frame decoded next
}
let skip = (voice.skip * channels).min(pcm.len());   // taken from this one
voice.skip -= skip / channels;

So the head of the loop's last frame is dropped, and the loop's first frame, decoded next with nothing left to skip, plays from its start: the subframes before the loop point. With an end subframe of 1 and a skip of 2 the last frame contributes nothing at all. The total number of samples per turn comes out the same, which is why the output ring's counts never showed it; what plays at the seam is wrong, by up to 7 subframes (896 samples).

Now the pending skip is applied to the frame in hand as before (the stream's start skip; or the loop's, when its first frame is also its last), and the loop's skip is set after that, for the frame decoded next.

Basis: the field's description in this file's own header, and the public XMA2 loop definition, where the skip is "subframes to skip in the start frame until we reach the loop" (from memory of xma2defs.h). I have not compared against the console's output or a title's stream (I have no game files).

Tested

Windows 11 Pro 10.0.26200, rustc 1.98.1 (x86_64-pc-windows-msvc), no game data, no GPU involved.

New unit test a_loop_skips_subframes_of_its_first_frame: the existing synthetic silent stream (frames at bits 32, 63, 94), a context looping once over the first two frames with end subframe 1 and a skip of 2 subframes, and decode called frame by frame. It checks how many samples each call adds and where the read position goes: 512 (the first frame, before the loop), 256 (the loop's last frame: its first two subframes), 256 (the loop's first frame without its first two), then, control, 512 again with no loops left and the read position on the third frame. The stream is silence, so it proves which frame each cut is taken from, not the audio.

  • main (d2a4bae) + the test only: cargo test -p victory-hle --lib a_loop_skips fails: the loop's last frame: left: 0, right: 256.
  • with the fix: cargo test -p victory-hle --lib audio::: 6 passed, 0 failed (among them xma_contexts_decode_into_their_output_rings, unchanged: 26 blocks).
  • cargo clippy -p victory-hle --tests: nothing in audio.rs. rustfmt --check: nothing in the lines changed (existing differences at pump and dump_input, and in audio/capture.rs).
  • cargo xtask policy --staged: ok.

Not tested: a title run, or by ear.

#88 touches advance_read in this file, not decode.

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

**@⁠Maraco91** on GitHub · 2026-11-24 01:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/194) `Maraco91:audio/xma-loop-start-skip` → `main` · +72 −2 · 1 files · 1 commits · merged 2026-11-25 00:15 UTC ## What A looping XMA context with a loop-start skip plays the wrong samples at every turn of the loop. When a frame ends a loop, `decode` cuts it at the loop's end subframe, sets `voice.skip` from the context ("loop start subframes to skip", word 1 bits 17-19) and jumps back to the loop's start. But the lines right after it apply `voice.skip` to the frame in hand, which is the loop's **last** frame: ```rust if loops > 0 && ends_loop { pcm.truncate(keep); next = loop start; voice.skip = loop skip; // meant for the frame decoded next } let skip = (voice.skip * channels).min(pcm.len()); // taken from this one voice.skip -= skip / channels; ``` So the head of the loop's last frame is dropped, and the loop's first frame, decoded next with nothing left to skip, plays from its start: the subframes before the loop point. With an end subframe of 1 and a skip of 2 the last frame contributes nothing at all. The total number of samples per turn comes out the same, which is why the output ring's counts never showed it; what plays at the seam is wrong, by up to 7 subframes (896 samples). Now the pending skip is applied to the frame in hand as before (the stream's start skip; or the loop's, when its first frame is also its last), and the loop's skip is set after that, for the frame decoded next. Basis: the field's description in this file's own header, and the public XMA2 loop definition, where the skip is "subframes to skip in the start frame until we reach the loop" (from memory of `xma2defs.h`). I have not compared against the console's output or a title's stream (I have no game files). ## Tested Windows 11 Pro 10.0.26200, rustc 1.98.1 (`x86_64-pc-windows-msvc`), no game data, no GPU involved. New unit test `a_loop_skips_subframes_of_its_first_frame`: the existing synthetic silent stream (frames at bits 32, 63, 94), a context looping once over the first two frames with end subframe 1 and a skip of 2 subframes, and `decode` called frame by frame. It checks how many samples each call adds and where the read position goes: 512 (the first frame, before the loop), 256 (the loop's last frame: its first two subframes), 256 (the loop's first frame without its first two), then, control, 512 again with no loops left and the read position on the third frame. The stream is silence, so it proves which frame each cut is taken from, not the audio. - `main` (d2a4bae) + the test only: `cargo test -p victory-hle --lib a_loop_skips` **fails**: `the loop's last frame: left: 0, right: 256`. - with the fix: `cargo test -p victory-hle --lib audio::`: **6 passed, 0 failed** (among them `xma_contexts_decode_into_their_output_rings`, unchanged: 26 blocks). - `cargo clippy -p victory-hle --tests`: nothing in `audio.rs`. `rustfmt --check`: nothing in the lines changed (existing differences at `pump` and `dump_input`, and in `audio/capture.rs`). - `cargo xtask policy --staged`: ok. Not tested: a title run, or by ear. #88 touches `advance_read` in this file, not `decode`. AI-assisted (Claude Code); the tests above were run on the machine described. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
When a frame ended a loop, decode cut it at the loop's end subframe, set
the loop's skip (the subframes of the loop's first frame that lie before
the loop) and then took that skip out of the frame in hand: the loop's
last. So the head of the last frame was dropped instead, and the first
frame played from its start, with the audio before the loop in it. The
number of samples came out the same; the seam did not.

The pending skip is still applied to the frame in hand, and the loop's
skip is set after it, for the frame decoded next.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:54:05 +00:00
github manually merged commit 985d8d4b02 into gh-pr/194/base 2026-11-26 03:54:07 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:32 UTC · original

Tested 985d8d4 (Arch Linux x86_64, rustc 1.98.1, cargo test -p victory-formats): pass.

  • 20 passed, 0 failed, 3 ignored, including the 1 test this PR adds.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/194#issuecomment-7119196297) Tested `985d8d4` (Arch Linux x86_64, rustc 1.98.1, `cargo test -p victory-formats`): **pass**. - **20 passed, 0 failed, 3 ignored**, including the 1 test this PR adds. No game data used.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:21 UTC · original

Automated triage for the maintainers.

Merge candidate. In decode in crates/victory-hle/src/modules/audio.rs, when a frame ends a loop, main sets voice.skip to the loop-start skip and then immediately applies it to the frame being decoded, i.e. the loop's last frame instead of its first. The PR applies the pending skip to the current frame first and then sets the loop's skip for the next frame, which matches the field's description in the file header ("loop start subframes to skip"); the single-frame loop case still works. The new test a_loop_skips_subframes_of_its_first_frame checks how many samples each frame contributes, which is the right thing to check.

CI: no check runs on the head commit.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/194#issuecomment-7148433164) <!-- victory-triage v1 head=985d8d4b025384b5f13a3177ef906c8b6cce6840 verdict=merge-candidate at=2026-11-25T00:18:15Z --> Automated triage for the maintainers. **Merge candidate.** In `decode` in `crates/victory-hle/src/modules/audio.rs`, when a frame ends a loop, main sets `voice.skip` to the loop-start skip and then immediately applies it to the frame being decoded, i.e. the loop's last frame instead of its first. The PR applies the pending skip to the current frame first and then sets the loop's skip for the next frame, which matches the field's description in the file header ("loop start subframes to skip"); the single-frame loop case still works. The new test `a_loop_skips_subframes_of_its_first_frame` checks how many samples each frame contributes, which is the right thing to check. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
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!194
No description provided.