fix(libSceNgs2): clamp the mastering gain to +-32 and keep NaN, as the PS5 module does #10410

Merged
JAB-dev merged 7 commits from upstream-pr/10410 into main 2026-10-26 09:01:52 +00:00
JAB-dev commented 2026-10-23 22:11:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JAB-dev on 2026-10-23T22:11:36Z.


What

The NGS2 mastering gain no longer throws on NaN or infinity and is now clamped to ±32, matching the PS5 module. Before, an infinite or NaN gain threw, and a gain above 32 was applied unclamped.

sceNgs2VoiceControl with the mastering param 0x30000004 (fbwLevel, lfeLevel) returns 0 for every value. Each level is clamped to [−32, 32] on its own, so ±inf gives ±32, and NaN is kept: the channels it scales become NaN. The rest already matched the PS5 module and is now covered by the test: the levels are linear factors on the mastering output, applied from the next render, flat for the first gain a voice receives and ramped over the next grain in eight lanes after that, which this change does not implement; the LFE level acts on channel 3 of 6- and 8-channel voices and the full-band level on every other channel, 1- and 2-channel voices included; a mastering setup resets both to 1, and STOP_IMM plus PLAY keeps them.

Level sent PS5 output ÷ input Before this PR
0, 0.5, 2, 4.5, 8, 16, 31, 32, −1, −32 the same value the same value
33, 64, 1e30 32 the value sent
−33 −32 −33
+inf / −inf 32 / −32 threw
NaN NaN threw

PS5 measured these into float render buffers only, so a NaN sample in a PCM_I16L render buffer now throws instead of converting to an arbitrary integer; the debt entry says so.

Source

The PS5 libSceNgs2.native module of firmware 3.20 and 4.03 (identical results), run on a PC with chosen inputs: a 6- and an 8-channel mastering voice fed an impulse per channel, with each level above sent alone, and 1- and 2-channel voices with mixed full-band and LFE levels. Only return codes and rendered samples were compared. Nothing from the firmware is in the repository.

Tested

Windows 11 x64, MinGW GCC 15.2.0 (WinLibs), Release, on main at e7f46f5c89.

  • TestMasteringGainClampedLikePs5 (new, in Ngs2Render.cpp) renders into float buffers:
    • on 6- and 8-channel voices, full-band 32, 33, 64, 1e30, +inf, −32, −33, −inf and NaN, and the same set as LFE levels, check channel 3 against the LFE level and every other channel against the full-band level;
    • 1-channel (0.5/2, inf/NaN) and 2-channel (−inf/1e30, 2/0.5) voices check that only the full-band level applies;
    • a gain sent between two renders applies from the first sample of the next, STOP_IMM plus PLAY keeps it, and a new setup resets it to 1;
    • a NaN gain into a PCM_I16L buffer throws.
  • The NaN check in TestMasteringGain that expected a throw is removed.
  • Negative control: with the main sources and the new test, ngs2_render and ngs2_core_render fail at the first clamped case (gain 33 renders 16.5, not 16).
  • Mutation: 12 mutants, all caught: each clamp removed, each lower bound widened, the bound changed to 31 or 33, fmin/fmax (drops NaN) in place of the clamp for each level, the LFE level taken from the full-band field, each setup reset removed, and the PCM_I16L NaN throw removed.
  • ctest -R ngs2: 13/13 pass. tools/check_conventions.py --base origin/main and git diff --check are clean.

Linux not tested.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4989887470 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10410). Originally posted by [JAB-dev](https://github.com/JAB-dev) on 2026-10-23T22:11:36Z. --- ### What The NGS2 mastering gain no longer throws on NaN or infinity and is now clamped to ±32, matching the PS5 module. Before, an infinite or NaN gain threw, and a gain above 32 was applied unclamped. `sceNgs2VoiceControl` with the mastering param `0x30000004` (`fbwLevel`, `lfeLevel`) returns 0 for every value. Each level is clamped to [−32, 32] on its own, so ±inf gives ±32, and NaN is kept: the channels it scales become NaN. The rest already matched the PS5 module and is now covered by the test: the levels are linear factors on the mastering output, applied from the next render, flat for the first gain a voice receives and ramped over the next grain in eight lanes after that, which this change does not implement; the LFE level acts on channel 3 of 6- and 8-channel voices and the full-band level on every other channel, 1- and 2-channel voices included; a mastering setup resets both to 1, and STOP_IMM plus PLAY keeps them. | Level sent | PS5 output ÷ input | Before this PR | |---|---|---| | 0, 0.5, 2, 4.5, 8, 16, 31, 32, −1, −32 | the same value | the same value | | 33, 64, 1e30 | 32 | the value sent | | −33 | −32 | −33 | | +inf / −inf | 32 / −32 | threw | | NaN | NaN | threw | PS5 measured these into float render buffers only, so a NaN sample in a `PCM_I16L` render buffer now throws instead of converting to an arbitrary integer; the debt entry says so. ### Source The PS5 `libSceNgs2.native` module of firmware 3.20 and 4.03 (identical results), run on a PC with chosen inputs: a 6- and an 8-channel mastering voice fed an impulse per channel, with each level above sent alone, and 1- and 2-channel voices with mixed full-band and LFE levels. Only return codes and rendered samples were compared. Nothing from the firmware is in the repository. ### 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. --> Windows 11 x64, MinGW GCC 15.2.0 (WinLibs), Release, on `main` at e7f46f5c89. - `TestMasteringGainClampedLikePs5` (new, in `Ngs2Render.cpp`) renders into float buffers: - on 6- and 8-channel voices, full-band 32, 33, 64, 1e30, +inf, −32, −33, −inf and NaN, and the same set as LFE levels, check channel 3 against the LFE level and every other channel against the full-band level; - 1-channel (0.5/2, inf/NaN) and 2-channel (−inf/1e30, 2/0.5) voices check that only the full-band level applies; - a gain sent between two renders applies from the first sample of the next, STOP_IMM plus PLAY keeps it, and a new setup resets it to 1; - a NaN gain into a `PCM_I16L` buffer throws. - The NaN check in `TestMasteringGain` that expected a throw is removed. - Negative control: with the `main` sources and the new test, `ngs2_render` and `ngs2_core_render` fail at the first clamped case (gain 33 renders 16.5, not 16). - Mutation: 12 mutants, all caught: each clamp removed, each lower bound widened, the bound changed to 31 or 33, `fmin`/`fmax` (drops NaN) in place of the clamp for each level, the LFE level taken from the full-band field, each setup reset removed, and the `PCM_I16L` NaN throw removed. - `ctest -R ngs2`: 13/13 pass. `tools/check_conventions.py --base origin/main` and `git diff --check` are clean. Linux not tested. ### 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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-23 22:54:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T22:54:13Z.

Review: APPROVED


Reviewed the clamping path, NaN handling, channel routing, lifecycle behavior, and new render fixtures against the stated firmware measurements. std::clamp preserves NaN while bounding finite values and infinities; the tests cover both mastering fields, mono/stereo routing, surround LFE routing, reset and stop/play behavior, plus the explicitly unverified PCM_I16L NaN case. The hosted repository gates pass; build/test jobs are skipped by path policy, and the author reports 13/13 focused NGS2 CTests on Windows with mutation checks. No blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635264143 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10410#pullrequestreview-5635264143). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T22:54:13Z. **Review:** APPROVED --- Reviewed the clamping path, NaN handling, channel routing, lifecycle behavior, and new render fixtures against the stated firmware measurements. `std::clamp` preserves NaN while bounding finite values and infinities; the tests cover both mastering fields, mono/stereo routing, surround LFE routing, reset and stop/play behavior, plus the explicitly unverified PCM_I16L NaN case. The hosted repository gates pass; build/test jobs are skipped by path policy, and the author reports 13/13 focused NGS2 CTests on Windows with mutation checks. No blocking issue found.
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!10410
No description provided.