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

Merged
Maraco91 merged 1 commit from upstream-pr/194 into main 2026-11-25 00:15:29 +00:00
Maraco91 commented 2026-11-24 01:18:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Maraco91 on 2026-11-24T01:18:07Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572397379 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/194). Originally posted by [Maraco91](https://github.com/Maraco91) on 2026-11-24T01:18:07Z. --- ## 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)
DaviTostes commented 2026-11-24 06:55:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:32:10Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7119196297 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/194#issuecomment-7119196297). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:32:10Z. --- 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.
mercuryVM commented 2026-11-24 09:50:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T09:50:09Z.

Review: APPROVED


Tested 985d8d4 on Windows 11, RTX 5070, MSVC: the workspace passes (196 outside victory-shader; victory-shader --lib single-threaded: 69), including a_loop_skips_subframes_of_its_first_frame.

The bug is a real ordering mistake. voice.skip was set to the loop-start skip and then applied to the frame in hand, which is the loop's last frame. Moving the assignment after the skip is consumed applies it to the next decoded frame, the loop's first. The remaining voice.skip from stream start is still honoured on the current frame. The test checks all four steps, including the "no loops left" pass through to the third frame.

No issues found.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6036998498 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/194#pullrequestreview-6036998498). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T09:50:09Z. **Review:** APPROVED --- Tested 985d8d4 on Windows 11, RTX 5070, MSVC: the workspace passes (196 outside `victory-shader`; `victory-shader --lib` single-threaded: 69), including `a_loop_skips_subframes_of_its_first_frame`. The bug is a real ordering mistake. `voice.skip` was set to the loop-start skip and then applied to the frame in hand, which is the loop's last frame. Moving the assignment after the skip is consumed applies it to the next decoded frame, the loop's first. The remaining `voice.skip` from stream start is still honoured on the current frame. The test checks all four steps, including the "no loops left" pass through to the third frame. No issues found. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 01:02:58 +00:00
Marvisak commented 2026-11-25 01:07:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:21:57Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148433164 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/194#issuecomment-7148433164). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:21:57Z. --- <!-- 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)_
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!194
No description provided.