feat(libSceNgs2.native): parse looped PCM waveforms #9149

Merged
MarrasFederico merged 7 commits from upstream-pr/9149 into main 2026-11-02 22:34:39 +00:00
MarrasFederico commented 2026-10-21 13:35:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-10-21T13:35:50Z.


What

sceNgs2ParseWaveformData threw for a PCM file whose smpl chunk declares a loop ("parsing looped waveforms is not implemented"). PCM loops now follow prosper's reading of libSceNgs2.native.sprx: its loop point code and block list code.

  • What is read: with a non-zero loop count, only the first loop's start (smpl +0x2c) and inclusive end (+0x30) are read. They are stored as the loop begin and end + 1, which wraps in 32 bits. The loop count beyond non-zero, the type, the fraction, the play count and any further loops are ignored.
  • Blocks: an end not after the start keeps the single block. Otherwise up to three blocks in whole frames with no skip: an intro when the start is not 0, the loop block repeating forever (0xFFFFFFFF), and a tail when the end is not the last sample.
  • What throws (recorded in the PCM parsing debt entry):
    • End past the last sample: the module does not check this, and its tail block size wraps.
    • Short smpl chunk: a chunk too short for its loop count or its first loop, where the module reads the bytes that follow it.

ATRAC9 loop parsing is unchanged and keeps its own rules.

This builds on #9148, which generalizes the PCM parser this change extends. #9148 is merged and this branch is merged with current main, so the diff holds only this PR's commits.

Tested

Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, full build with BUILD_TESTING=ON. Windows was not compiled. No title was run.

  • ngs2_parse, TestParsePcmLoops:
    • Loop with intro and tail: a 16-bit and a 24-bit stereo file with a loop from 10 to 59 (inclusive) give three blocks in whole frames with no skip: 10 samples, 50 repeated forever, then 40.
    • Ignored fields: the same blocks come back with play count 3, with two loops whose first is ping-pong, and with three backward loops with a fraction.
    • Loop at an edge:
      • a loop over the whole file gives one block repeated forever;
      • a loop from 0 with a tail gives the loop and the tail;
      • a loop ending at the last sample gives the intro and the loop;
      • a single-sample loop gives three blocks.
    • End not after the start: the end equal to the start, a backward range, and an end of 0xFFFFFFFF (wrapping to 0) keep the single block and store the loop positions. A smpl chunk with no loops also keeps the single block.
    • Not tested: the forms that still throw (an end past the last sample, and a smpl chunk too short for its count or first loop), so the test does not pin them as unimplemented.
  • The test fails on this branch's base (#9148), where a looped PCM file throws. These changes also make it fail:
    • returning INVALID_WAVEFORM_DATA for an end not after the start;
    • mapping the play count to repeats;
    • computing the end in 64 bits.
  • ngs2_atrac9 and its loop cases still pass.
  • After merging current main (#9148 and #9324 included, conflicts in Ngs2Parse.cpp and the PCM parsing debt entry resolved by keeping both changes), the full build succeeds and the 45 ngs2 tests pass.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py finds no conflicting lines. It could not compare #5797 and #8609, which conflict with main; neither touches loop parsing.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952491196 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9149). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-10-21T13:35:50Z. --- ### What `sceNgs2ParseWaveformData` threw for a PCM file whose `smpl` chunk declares a loop ("parsing looped waveforms is not implemented"). PCM loops now follow [prosper's reading of `libSceNgs2.native.sprx`](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L249-L257): its loop point code and [block list code](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L294-L343). - **What is read:** with a non-zero loop count, only the first loop's start (`smpl` +0x2c) and inclusive end (+0x30) are read. They are stored as the loop begin and `end + 1`, which wraps in 32 bits. The loop count beyond non-zero, the type, the fraction, the play count and any further loops are ignored. - **Blocks:** an end not after the start keeps the single block. Otherwise up to three blocks in whole frames with no skip: an intro when the start is not 0, the loop block repeating forever (`0xFFFFFFFF`), and a tail when the end is not the last sample. - **What throws** (recorded in the PCM parsing debt entry): - **End past the last sample:** the module does not check this, and its tail block size wraps. - **Short `smpl` chunk:** a chunk too short for its loop count or its first loop, where the module reads the bytes that follow it. ATRAC9 loop parsing is unchanged and keeps its own rules. This builds on #9148, which generalizes the PCM parser this change extends. #9148 is merged and this branch is merged with current `main`, so the diff holds only this PR's commits. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Ubuntu 26.04.1, kernel 7.0.0-38, GCC 15.2.0, Release, full build with `BUILD_TESTING=ON`. Windows was not compiled. No title was run. - `ngs2_parse`, `TestParsePcmLoops`: - **Loop with intro and tail:** a 16-bit and a 24-bit stereo file with a loop from 10 to 59 (inclusive) give three blocks in whole frames with no skip: 10 samples, 50 repeated forever, then 40. - **Ignored fields:** the same blocks come back with play count 3, with two loops whose first is ping-pong, and with three backward loops with a fraction. - **Loop at an edge:** - a loop over the whole file gives one block repeated forever; - a loop from 0 with a tail gives the loop and the tail; - a loop ending at the last sample gives the intro and the loop; - a single-sample loop gives three blocks. - **End not after the start:** the end equal to the start, a backward range, and an end of `0xFFFFFFFF` (wrapping to 0) keep the single block and store the loop positions. A `smpl` chunk with no loops also keeps the single block. - **Not tested:** the forms that still throw (an end past the last sample, and a `smpl` chunk too short for its count or first loop), so the test does not pin them as unimplemented. - The test fails on this branch's base (#9148), where a looped PCM file throws. These changes also make it fail: - returning `INVALID_WAVEFORM_DATA` for an end not after the start; - mapping the play count to repeats; - computing the end in 64 bits. - `ngs2_atrac9` and its loop cases still pass. - After merging current `main` (#9148 and #9324 included, conflicts in `Ngs2Parse.cpp` and the PCM parsing debt entry resolved by keeping both changes), the full build succeeds and the 45 `ngs2` tests pass. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` finds no conflicting lines. It could not compare #5797 and #8609, which conflict with `main`; neither touches loop parsing. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
KernelPanicNT commented 2026-10-21 14:56:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by KernelPanicNT on 2026-10-21T14:56:52Z.


I had a branch for the same change (not opened, since this one came first), built from the same prosper reading. A few places where this PR differs from what that reading says the native module does, in case they are worth aligning or recording:

  1. End not after begin. Here begin >= end returns INVALID_WAVEFORM_DATA. In prosper's loop point code (0x116cd..0x11729) the module stores the loop positions and keeps the single block, returning success.
  2. Loop type, fraction and count. The block list code (0x11a20..0x11cdd) reads only the first loop's start and end (smpl +0x2c, +0x30). It does not read the loop count beyond "non-zero", the type, the fraction or the play count. So a file with two loops, or a ping-pong first loop, parses on the console, but throws here. The play count difference is already in the debt entry; the others could be listed there too.
  3. End past the last sample. Here it returns INVALID_WAVEFORM_DATA. The module does not check it and the tail block size wraps, so a throw recorded as debt may be closer than an error code the module never returns.

Cases worth a test either way: a two-loop chunk with a ping-pong first loop, a backwards loop, an end that wraps to 0 (0xFFFFFFFF), a loop from 0 to the last sample, and a loop from 0 with a tail. I am dropping my branch, so this is only for reference.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319107365 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9149#issuecomment-6319107365). Originally posted by [KernelPanicNT](https://github.com/KernelPanicNT) on 2026-10-21T14:56:52Z. --- I had a branch for the same change (not opened, since this one came first), built from the same prosper reading. A few places where this PR differs from what that reading says the native module does, in case they are worth aligning or recording: 1. **End not after begin.** Here `begin >= end` returns `INVALID_WAVEFORM_DATA`. In prosper's [loop point code (`0x116cd..0x11729`)](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L249-L257) the module stores the loop positions and keeps the single block, returning success. 2. **Loop type, fraction and count.** The [block list code (`0x11a20..0x11cdd`)](https://github.com/mattias800/prosper/blob/40d95db32efe1ad00106a5b9a84e018fffa9ecff/prosper/src/hle/audio/ngs2_waveform.cpp#L294-L343) reads only the first loop's start and end (`smpl` +0x2c, +0x30). It does not read the loop count beyond "non-zero", the type, the fraction or the play count. So a file with two loops, or a ping-pong first loop, parses on the console, but throws here. The play count difference is already in the debt entry; the others could be listed there too. 3. **End past the last sample.** Here it returns `INVALID_WAVEFORM_DATA`. The module does not check it and the tail block size wraps, so a throw recorded as debt may be closer than an error code the module never returns. Cases worth a test either way: a two-loop chunk with a ping-pong first loop, a backwards loop, an end that wraps to 0 (`0xFFFFFFFF`), a loop from 0 to the last sample, and a loop from 0 with a tail. I am dropping my branch, so this is only for reference.
MarrasFederico commented 2026-10-21 15:06:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarrasFederico on 2026-10-21T15:06:31Z.


@KernelPanicNT thanks, all three hold against those lines, so PCM loops now follow the module in 403956973. ATRAC9 keeps its own loop parser, unchanged:

  1. An end not after the start keeps the single block and returns success, with the loop begin and end + 1 stored.
  2. Only the first loop's start and inclusive end are read. Two loops with a ping-pong first loop, a backward or fractional loop, and any play count parse, and the loop block always repeats forever.
  3. An end past the last sample now throws, recorded in the debt entry, instead of returning an error code the module never returns. A smpl chunk too short for its count or first loop also throws, since the module reads the bytes after it.

TestParsePcmLoops covers your cases:

  • a two-loop chunk with a ping-pong first loop, and backward loops;
  • an end of 0xFFFFFFFF (wrapping to 0);
  • a loop from 0 to the last sample, and a loop from 0 with a tail;
  • a loop ending at the last sample, and a single-sample loop.

It fails if the error code, the play count mapping or a 64-bit end come back. I updated the description too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319305555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9149#issuecomment-6319305555). Originally posted by [MarrasFederico](https://github.com/MarrasFederico) on 2026-10-21T15:06:31Z. --- @KernelPanicNT thanks, all three hold against those lines, so PCM loops now follow the module in 403956973. ATRAC9 keeps its own loop parser, unchanged: 1. An end not after the start keeps the single block and returns success, with the loop begin and `end + 1` stored. 2. Only the first loop's start and inclusive end are read. Two loops with a ping-pong first loop, a backward or fractional loop, and any play count parse, and the loop block always repeats forever. 3. An end past the last sample now throws, recorded in the debt entry, instead of returning an error code the module never returns. A `smpl` chunk too short for its count or first loop also throws, since the module reads the bytes after it. `TestParsePcmLoops` covers your cases: - a two-loop chunk with a ping-pong first loop, and backward loops; - an end of `0xFFFFFFFF` (wrapping to 0); - a loop from 0 to the last sample, and a loop from 0 with a tail; - a loop ending at the last sample, and a single-sample loop. It fails if the error code, the play count mapping or a 64-bit end come back. I updated the description too.
ycbuzi commented 2026-10-21 20:49:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T20:49:08Z.


Windows run of 4039569737 merged onto main 0c6a5bd593 (66d404c184, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libSceNgs2.native and ngs2_parse_tests with no errors or warnings. ctest --repeat until-fail:2 on ngs2_parse: passes on both repeats. The head carries the PRs below it in its stack (#9148) too. Not run: a title.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326234287 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9149#issuecomment-6326234287). Originally posted by [ycbuzi](https://github.com/ycbuzi) on 2026-10-21T20:49:08Z. --- Windows run of 4039569737 merged onto `main` 0c6a5bd593 (66d404c184, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, `BUILD_TESTING=ON`. Built `libSceNgs2.native` and `ngs2_parse_tests` with no errors or warnings. `ctest --repeat until-fail:2` on `ngs2_parse`: passes on both repeats. The head carries the PRs below it in its stack (#9148) too. Not run: a title.
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
boykopovar/AnyPS5!9149
No description provided.