Skip to content

feat(libSceNgs2): implement the native mastering limiter - #22536

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
kattkaries:feat/ngs2-mastering-limiter
Nov 19, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
kattkaries:feat/ngs2-mastering-limiter

Conversation

@kattkaries

@kattkaries kattkaries commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Implement the standard PS5 NGS2 mastering limiter (0x30000003) using a linked peak detector over 32-sample blocks, with 10 ms attack and 100 ms release. Reuse the compressor render path and apply the limiter before output channel mapping and mastering gain. Follow the published firmware 13.42 DSP analysis.

This addresses the limiter portion of #21876. The LFE effect remains open. The firmware placement, sample rate source and event lifecycle flags remain unverified. The review update follows the PS5 13.590.001 measurements: NaN disables the limiter, mastering setup clears it, and thresholds clamp to [0, 1].

The overlap tool could not automatically compare #1384 and #5548 against main. I manually compared both diffs and the nearby custom mastering PR #18501. Their overlapping files cover reverb, sampler PCM input and custom mastering modules respectively.

Tested

Linux x86-64, GCC 16.2.1, Release, rebased on cc2b8933b1. ngs2_render, ngs2_core_render, ngs2_routing, and ngs2_jobcontrol all passed. The new render regression compares step and sine input against a reference envelope across two linked channels and checks NaN and bypass behavior. No title or console run.

Review update at e301b1244b: Linux x86-64 Release, rebuilt and passed ngs2_core_render and ngs2_render. The native suite checks that NaN and a fresh setup bypass the limiter on above-threshold input, that zero attenuates, and that out-of-range values (including infinities) match the corresponding endpoint. Conventions passed.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: PPSA36136 The Legend of Heroes: Trails From Zero (PPSA36136, Windows): abort on NGS2 voice param #19988 (reported limiter failure, title boot unverified)
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Nov 17, 2026

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against e17fff2043:

  • Conventions: tools/check_conventions.py passed.
  • Build: ngs2_core_render_tests, ngs2_render_tests built cleanly.
  • CTest (ngs2_core_render, ngs2_render):
    1/2 Test #4642: ngs2_render ......................   Passed    3.49 sec
    2/2 Test #4632: ngs2_core_render .................   Passed    7.03 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libSceNgs2.native/src/Render.cpp, core/libs/prx/libSceNgs2.native/src/Voice.cpp reverted to main and the new tests kept, the suite fails:
    1/2 Test #4642: ngs2_render ......................Subprocess aborted***Exception:   0.86 sec
    2/2 Test #4632: ngs2_core_render .................   Passed    7.41 sec
    50% tests passed, 1 tests failed out of 2
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified Render.cpp ProcessCompressor mastering limiter branch threshold/x target calculation and attack/release coefficient smoothing over 32-sample blocks.
    • Verified Voice.cpp CheckMasteringEffect and ApplyParam handling for SCE_NGS2_MASTERING_VOICE_PARAM_LIMITER, including NaN handling, enable/disable flags, and threshold range [0.0, 1.0].
    • Verified ResetSetup() in Voice.cpp resets compressor state and gain array to 1.0f on voice configuration changes.
    • Verified docs/dev/debt/changes/feat-ngs2-mastering-limiter.md debt fragment adherence to AGENTS.md conventions.
    • Verified Ngs2Render.cpp unit tests exercise enabled, disabled, NaN threshold, and multi-grain limiter rendering against reference exponential decay models.

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the mastering limiter — no blocking findings

Nothing blocks this; the points below are optional. The change matches its description at the head reviewed, but I did not build or run the tests in this review.

What I ran

Head e17fff2043638042c86b69f5e94a96f79fd89aac, base main. Read-only review of the diff and the head repository through the reader:

python3 repo_read.py search "struct Ngs2SubmixerVoiceCompressorParam" core/libs/prx/libSceNgs2.native
python3 repo_read.py cat core/libs/prx/libSceNgs2.native/include/Ngs2Types.hpp 490 502

No build, no test run, no merge preview.

Results

Check Result Note
Changed hunks read passed Render.cpp, Voice.cpp, test hunks
Compressor param layout passed Matches the aggregate init in Voice.cpp:624
Build and tests (mine) not run No build in this review
Tests (author-reported) not run Author reports a Linux GCC run; not verified here
Negative control not run Not performed

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libSceNgs2.native/src/Voice.cpp:624: a threshold of exactly 0 is accepted. With target = threshold / x (Render.cpp:449) the gain then decays to 0, so the voice goes silent. That may be the intended linear limit, but it is not covered by a test. Either add a test for it or reject 0 as the submixer path does.
  • core/libs/prx/libSceNgs2.native/tests/Ngs2Render.cpp:1757: the new limiter test checks one threshold (0.25), one attack and release pair, and the NaN and disable paths. Knee-free only, which is the only path mastering uses, so this is acceptable for now.

Not verified

  • Build and test results on this head. I did not run the four test suites named in the PR.
  • Firmware behaviour: placement, sample-rate source, lifecycle flags and bit-exact output. The PR says these remain unverified and I did not check them.
  • Gain state across unrendered blocks: ProcessCompressor is not called when a voice is not playing, so the gain resumes from its last value. The debt note says lifecycle is unverified; I did not test this.
  • Full text of Ngs2Render.cpp, Render.cpp and Voice.cpp outside the changed hunks. I read the changed regions and their direct callers only.

To get this to a good state

Nothing is required.

Optional: decide whether threshold 0 should be accepted, and add a test for it if it is.

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1384 core/libs/prx/libSceNgs2.native/src/Render.cpp
core/libs/prx/libSceNgs2.native/src/Voice.cpp

@maskym

maskym commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Windows result for head e17fff2043, merged alone into main 56c87adbdc (the PR's Tested section names no Windows run).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. main itself fails to compile one test on Windows at this commit (UploadCacheReadOnlyFlush.cpp:177, setenv; #19899 fixes it), so the tree was built with ninja -k 0. Microsoft Defender on this machine quarantines 34 AGC test executables as Trojan:Win64/ShellCode.MKA!MTB when they are linked (the case docs/dev/BUILD.md describes), so their objcopy step fails on both trees and those tests do not run here. This PR adds no other build failure.

The tests this PR adds or extends, ngs2_core_render, ngs2_render, pass 2/2, three runs. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@eaglescoutmaster

Copy link
Copy Markdown
Contributor

Compared the limiter at e17fff2043 with the reading on #21876 and with the PS5 measurement the debt fragment cites (#8039, the 0x30000003 table). Code read, not built or run.

The DSP matches the #21876 reading: trunc(rate * t), exp(-1/N), 32-sample peak blocks, link mode 0, target T/L, attack when the target is below the gain, the 0.01 snap, bypass resetting the gains.

Three points differ from the #8039 measurement:

  1. Voice.cpp:1197, } else if (!std::isnan(threshold)) {, leaves a previously set limiter active when a NaN threshold arrives. Measured: "threshold NaN | returns 0 and the limiter is off, also when a valid limiter param was sent before it". The test's NaN grain is below the threshold, so it passes either way.
  2. In the setup reset (Voice.cpp:246), compressor.gain.fill(1.0f); is the only compressor reset; the configured parameters survive. Measured: "A new 0x30000000 setup clears LFE and limiter, before PLAY or between renders".
  3. Voice.cpp:625 throws for a threshold outside 0 to 1, and the fragment calls the firmware's handling unknown. Measured: "threshold ≥ 1 (1, 1.0001, 2, 100, +inf) | same as 1" and "threshold ≤ 0 (0, -0, -1, -6, -inf, 1e-9) | same as 0". Throwing is allowed, but the measurement gives the answer, so it could clamp instead.

Not checked: the rate source, placement relative to channel mapping, and the mixer inputs.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing in the inspected changes blocks merging; the author-reported checks were not run during this review.

What I ran

Reviewed head e301b1244b67993a45c537691d21488ff700dfac using repo_read.py to inspect the prepared diff and changed source at that head. No commands or tests were run by me; no merged-preview check was performed.

Results

Check Result Note
Diff and changed code review passed All four changed files read
Tests not run Reported by author, not run here
Console behavior not run Firmware behavior remains partly unverified

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests or console runs were performed by me. The author reports Linux x86-64 Release test runs, but those results were not independently verified. Firmware placement, limiter sample rate source, event lifecycle flags, and bit-exact output remain unverified per the change documentation. Read all changed files listed in the manifest: Render.cpp, Voice.cpp, Ngs2Render.cpp, and feat-ngs2-mastering-limiter.md; no changed files were excluded.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit 928e3dd into boykopovar:main Nov 19, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs prx unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants