ngs2: implement the sampler voice peak meter that Trails in the Sky uses for lip-sync (PPSA28465) #1853
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1853
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1853"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@MrBigBritches on GitHub · 2026-10-23 01:38 UTC · original
MrBigBritches:ngs2-sampler-peak-meter→main· +237 −6 · 2 files · 7 commits · openTrails in the Sky 1st Chapter (PPSA28465) stopped at startup with:
0x10000009is the sampler voice peak meter control (SamplerVoicePeakMeterParam { enableFlag, reserved }). Sampler controls are numbered in this order: setup, waveform blocks, waveform address, frame offset, exit loop, pitch, envelope, distortion, user FX, peak meter, filter. The controls the emulator already handles (0, 1, 4, 5, 0xA) match that order.Fixes #314
Changes
Ngs2VoiceControlaccepts the control on sampler racks. It reads a typedNgs2SamplerPeakMeterParam, and the control id is namedNGS2_SAMPLER_CONTROL_PEAK_METER.Ngs2PeakHeighttakes the largest value across all channels, skips NaN and infinite samples, and doesn't clamp above full scale.Ngs2VoiceGetStatereports that value aspeak_heightfor sampler voices. It is 0 while the meter is off and for any render where the voice produces nothing (paused, stopped, empty, or without channels).num_peak_meter_blocksis 1 instead of 8, and a rack asking for any other count logs a warning (see below).What the game does with the meter
Measured in Trails in the Sky with temporary diagnostics. The diagnostics are not part of this PR.
num_peak_meter_blocks. Every rack the game creates asks for 1, while up to 22 voices are metered at once. So the count isn't a limit on metered voices. Whether a larger count means a longer window, sub-blocks of a grain or an allocation size can't be told from this. At 1, every one of these interpretations gives the same result. The meter therefore reports each grain's peak, and the log shows a warning for every rack that asks for another count.[0.5, 0.5]matrix, so a meter taken after routing would read exactly half (−6 dB). Sony's actual tap point is unknown. The peak is taken before routing by choice. A voice can feed several destinations with different volumes and matrices, so only that point gives one level per voice, as a per-voice channel meter on a mixer does. The struct layouts fit this too. The sampler state has a singlepeakHeight, while the mastering state has per-channel input and output peaks. Halving the reported peak made no visible difference to the game's lip-sync. So the game isn't sensitive to a 6 dB level change, though that doesn't show which tap point Sony uses.Testing
ngs2_sampler_testscovers:Each case was checked against a deliberately broken implementation.
The full test suite passes on Windows (clang-cl), apart from
kernel_file_system, which fails the same way onmain.Trails in the Sky 1st Chapter plays with audio and lip-sync during voiced dialogue.
Known remaining gaps (not in this PR)
port.input >= voice.rack->NumBuffers()in the voice mixing loop. A port routes into an input its destination rack doesn't have. Skipping that route locally lets the game continue.num_peak_meter_blocksto 8. They have no meter yet, so nothing reads it.0x1b, which is still ignored. Its 96-byte parameter matches the custom reverb (I3DL2) parameter, so that bus probably plays without reverb.Trails in the Sky 1st Chapter stopped at startup with "unsupported sampler control: 0x10000009". That control is the sampler voice peak meter (SamplerVoicePeakMeterParam {enableFlag, reserved}), which comes after setup, waveform blocks/address/offset, exit loop, pitch, envelope, distortion and user FX in the sampler control order; the controls already handled (0, 1, 4, 5, 0xa) match that order. While the meter is enabled, each render records the largest absolute sample of the voice after its filters, and the sampler voice state reports it as peak_height. It is 0 while the meter is off or the voice renders nothing. The rack's peak-meter block count is not used: the peak covers only the last render. The game now runs past startup at a steady 60 fps in a 90-second run. TestPeakMeter covers enabling, per-render peaks, running out of data and disabling. Co-Authored-By: Claude Opus 5.5 <[email protected]>@coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · edited 2026-10-28 01:22 UTC
Walkthrough
The sampler adds peak-meter control and state. When enabled, it records the largest absolute finite sample from the filtered voice buffer during rendering. Sampler state reports the stored peak while the meter is enabled. The default sampler rack peak-meter block count changes to one, and rack creation warns when configured with another count. Tests cover peak calculation, meter control, resets, filtering, stereo output, and paused playback.
Suggested reviewers:
nmzik,cosmoPriority:
Normal
Change: Bug fix
Merge Risk:
Low · up to
5e75eA reset voice can briefly report a stale peak. The change is mergeable with awareness of this narrow issue.
Pre-merge checks:
4 ·
1
#314] has no explicit coding acceptance criteria. The change supports sampler peak-meter control0x10000009, validates the control payload, records the post-filter peak for each render, and r…src/libs/ngs2.cpp. The test changes add focused coverage for that behavior in `tests/Ngs…Full details: Linked Issues check
Explanation
Issue [
#314] has no explicit coding acceptance criteria. The change supports sampler peak-meter control0x10000009, validates the control payload, records the post-filter peak for each render, and reportspeak_heightwhile the meter is enabled.Ngs2SamplerTests.cppcovers disabled metering, per-render behavior, empty output, filtering, reset, and disabling. The reported startup blocker is therefore addressed. The issue does not require the remaining sampler controls or the other runtime symptoms to be resolved.Full details: Out of Scope Changes check
Explanation
The source changes add sampler peak-meter state, control handling, render measurement, and state reporting in
src/libs/ngs2.cpp. The test changes add focused coverage for that behavior intests/Ngs2SamplerTests.cpp. The sampler rack block-count handling and reset behavior support the same feature. No unrelated change is established.Full details: Docstring Coverage
Explanation
Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 11 functions across 2 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · edited 2026-10-25 23:30 UTC
On
src/libs/ngs2.cppline 1798 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Clear the peak before the zero-channel return.
If an enabled voice reports a peak and then receives a deferred sampler setup,
SetupSamplersetschannelsto zero. The next render returns at Line 1792 before this reset runs.Ngs2VoiceGetStatecontinues to report the old peak although the voice rendered nothing. Move the reset before thechannels == 0check.✅ Addressed in commit eaca50d
@coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
aa6dd904-e597-4989-a1de-142df33a4fa7📥 Commits
Reviewing files that changed from the base of the PR and between
79837b423dand 0c5e1b1fdba8ae281549f78bc071ae8ae9c2af11.📒 Files selected for processing: 2
src/libs/ngs2.cpptests/Ngs2SamplerTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@ -1805,0 +1859,4 @@// this render when the voice produced nothing.if (voice.peak_meter_enabled && voice.has_samples) {voice.peak_height =Ngs2PeakHeight({voice.Buffer(0, grain), size_t {grain} * voice.channels});@coderabbitai[bot] on GitHub · 2026-10-28 01:21 UTC · original · edited 2026-10-28 01:45 UTC
Preserve a callback reset during rendering.
If a voice produces samples and a later block-completion callback calls
SetEvent(8)orSetEvent(4),SetEventclearspeak_height.Ngs2ConsumeSamplescan still return withhas_samplestrue for the earlier samples. This assignment then restores a nonzero peak after the reset. Do not publish a peak from that render when a callback has reset the voice. Add a test that resets the voice from a callback after an earlier block produced a sample.@coderabbitai[bot] on GitHub · 2026-10-28 01:21 UTC · original · reviewed
Actionable comments posted: 1
97f68f16-b3da-42e2-b827-aa50eacdccc3Reviewing files that changed from the base of the PR and between eaca50d508963c34b189b283114cb3b7be6688d7 and
5e75e20b24.src/libs/ngs2.cpptests/Ngs2SamplerTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@ -1805,0 +1859,4 @@// this render when the voice produced nothing.if (voice.peak_meter_enabled && voice.has_samples) {voice.peak_height =Ngs2PeakHeight({voice.Buffer(0, grain), size_t {grain} * voice.channels});@MrBigBritches on GitHub · 2026-10-28 01:45 UTC · original
Keeping this as is. Samples rendered before a callback kills or resets the voice are still mixed into the destination this grain, since mixing only checks has_samples. So the reported peak matches audio that was actually output. It lasts one grain (about 5 ms), because the next render zeroes peak_height and the voice is Empty by then. Letting the reset win would report silence for a grain that was audible.
@ -1805,0 +1859,4 @@// this render when the voice produced nothing.if (voice.peak_meter_enabled && voice.has_samples) {voice.peak_height =Ngs2PeakHeight({voice.Buffer(0, grain), size_t {grain} * voice.channels});@coderabbitai[bot] on GitHub · 2026-10-28 01:46 UTC · original
@MrBigBritches You're right.
SetEvent(4)andSetEvent(8)do not discard samples already produced in the current grain. Destination mixing useshas_samples, not the source voice’s updated play state.The peak therefore represents the retained, filtered samples for that grain. The next render clears it. My proposed change would make the meter inconsistent with those samples. I withdraw the finding; no change is needed.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.