ngs2: implement the sampler voice peak meter that Trails in the Sky uses for lip-sync (PPSA28465) #1853

Open
github-bot wants to merge 7 commits from github-bot/gh-1853 into main AGit
Owner

@⁠MrBigBritches on GitHub · 2026-10-23 01:38 UTC · original
MrBigBritches:ngs2-sampler-peak-meter → main · +237 −6 · 2 files · 7 commits · open

Trails in the Sky 1st Chapter (PPSA28465) stopped at startup with:

unsupported sampler control: 0x10000009

0x10000009 is 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

  • Ngs2VoiceControl accepts the control on sampler racks. It reads a typed Ngs2SamplerPeakMeterParam, and the control id is named NGS2_SAMPLER_CONTROL_PEAK_METER.
  • While the meter is enabled, each render records the largest absolute sample of the voice, after its filters and before port volume and routing. Ngs2PeakHeight takes the largest value across all channels, skips NaN and infinite samples, and doesn't clamp above full scale.
  • Ngs2VoiceGetState reports that value as peak_height for 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).
  • Voice setup turns the meter off. A kill or reset event clears the peak, so a reused voice never reports the previous sound's level.
  • The sampler rack's default num_peak_meter_blocks is 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.

  • Read rate. The game reads each metered voice's peak about twice per render (512-sample grains at 48 kHz), from its own thread. It never lets a render pass unread, so reporting each grain's peak loses nothing.
  • 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.
  • Measurement point. No metered voice has a filter. Every metered voice is routed mono-to-stereo through a [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 single peakHeight, 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.
  • No public source (shadPS4, upstream Kyty, OpenOrbis) describes the meter beyond the struct layouts.

Testing

  • ngs2_sampler_tests covers:

    • the peak with the meter on and off
    • each render's peak
    • running out of data, and a voice without channels
    • disabling the meter
    • setup, kill and reset
    • stereo with the louder sample on the right channel
    • reading twice without a render in between
    • a voice behind a low-pass filter
    • a paused voice
    • NaN, infinite and above-full-scale samples
    • the default block count

    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 on main.

  • Trails in the Sky 1st Chapter plays with audio and lip-sync during voiced dialogue.

Known remaining gaps (not in this PR)

  • Later in Trails in the Sky, the emulator stops at 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.
  • Lip-sync looks slightly behind the voice. The cause isn't known yet. Extra buffering in the host audio output would make the mouths move early rather than late, so it doesn't explain this.
  • The submixer and mastering racks still default num_peak_meter_blocks to 8. They have no meter yet, so nothing reads it.
  • Envelopes (sampler control 6) aren't implemented. When they are, the meter should move after the envelope.
  • The game's custom submixer rack uses module 0x1b, which is still ignored. Its 96-byte parameter matches the custom reverb (I3DL2) parameter, so that bus probably plays without reverb.
  • Other sampler controls (waveform address and offset, envelope, distortion, user FX) still stop the emulator if used.
**@⁠MrBigBritches** on GitHub · 2026-10-23 01:38 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853) `MrBigBritches:ngs2-sampler-peak-meter` → `main` · +237 −6 · 2 files · 7 commits · open Trails in the Sky 1st Chapter (PPSA28465) stopped at startup with: unsupported sampler control: 0x10000009 `0x10000009` is 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 https://git.gtahub.st/github/KytyPS5--KytyPS5/issues/314 ### Changes - `Ngs2VoiceControl` accepts the control on sampler racks. It reads a typed `Ngs2SamplerPeakMeterParam`, and the control id is named `NGS2_SAMPLER_CONTROL_PEAK_METER`. - While the meter is enabled, each render records the largest absolute sample of the voice, after its filters and before port volume and routing. `Ngs2PeakHeight` takes the largest value across all channels, skips NaN and infinite samples, and doesn't clamp above full scale. - `Ngs2VoiceGetState` reports that value as `peak_height` for 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). - Voice setup turns the meter off. A kill or reset event clears the peak, so a reused voice never reports the previous sound's level. - The sampler rack's default `num_peak_meter_blocks` is 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. - **Read rate.** The game reads each metered voice's peak about twice per render (512-sample grains at 48 kHz), from its own thread. It never lets a render pass unread, so reporting each grain's peak loses nothing. - **`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. - **Measurement point.** No metered voice has a filter. Every metered voice is routed mono-to-stereo through a `[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 single `peakHeight`, 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. - No public source (shadPS4, upstream Kyty, OpenOrbis) describes the meter beyond the struct layouts. ### Testing - `ngs2_sampler_tests` covers: - the peak with the meter on and off - each render's peak - running out of data, and a voice without channels - disabling the meter - setup, kill and reset - stereo with the louder sample on the right channel - reading twice without a render in between - a voice behind a low-pass filter - a paused voice - NaN, infinite and above-full-scale samples - the default block count 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 on `main`. - Trails in the Sky 1st Chapter plays with audio and lip-sync during voiced dialogue. ### Known remaining gaps (not in this PR) - Later in Trails in the Sky, the emulator stops at `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. - Lip-sync looks slightly behind the voice. The cause isn't known yet. Extra buffering in the host audio output would make the mouths move early rather than late, so it doesn't explain this. - The submixer and mastering racks still default `num_peak_meter_blocks` to 8. They have no meter yet, so nothing reads it. - Envelopes (sampler control 6) aren't implemented. When they are, the meter should move after the envelope. - The game's custom submixer rack uses module `0x1b`, which is still ignored. Its 96-byte parameter matches the custom reverb (I3DL2) parameter, so that bus probably plays without reverb. - Other sampler controls (waveform address and offset, envelope, distortion, user FX) still stop the emulator if used.
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]>
A deferred sampler setup can leave an enabled voice without channels.
Its render then returned before clearing the peak, so the sampler voice
state kept reporting the peak of its last real render although the voice
rendered nothing. Clear the peak before that return.

TestPeakMeter now renders a voice without channels between two peaks. It
fails without the fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A killed or reset voice kept its last peak until it rendered again, so
a game reading it from another thread could see the previous sound's
level. Clear the peak on those events.

Sampler setup starts a new sound and already clears the voice's ports,
matrices and filters, so it now turns the peak meter off too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the bare control number 9 and the hand-counted parameter size
with NGS2_SAMPLER_CONTROL_PEAK_METER and a typed
Ngs2SamplerPeakMeterParam, checked like the filter control's parameter.

The tests build the control from the same names, and the fixture's
setup moves into a Setup helper so the reset test can set a voice up
again without repeating it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Move the peak scan into Ngs2PeakHeight. It reads only the voice's own
output buffer and skips NaN and infinite samples, so a misbehaving
filter can't hand the game a peak it can't turn into a level. Levels
above full scale are kept.

Say at the call why the meter reads the voice after its filters and
before port volume, and that it should move after the envelope once
envelopes are implemented.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A stereo voice whose right channel is louder must report the right
channel's peak, and reading the peak must not reset it.

The meter reads the voice after its filters. A Nyquist-rate signal
through a 100 Hz low-pass must report the small filtered level, not the
source's level. Also reword the meter field's comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Trails in the Sky 1st Chapter asks for one peak meter block on every
rack while metering up to 22 voices at once, so the count is not a cap
on metered voices. What a larger count means is unknown: a longer
window, sub-blocks of a grain or an allocation size would all fit. The
meter keeps reporting each grain's peak, which every reading agrees on
for one block.

Default the sampler rack to that one block instead of an invented 8,
and warn when a game asks for another count so the first such game
leaves evidence. Test that a paused voice reports 0 from its next
render and that the default is one block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:07:11 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · edited 2026-10-28 01:22 UTC

Review in Change Stack →

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, cosmo

Priority: Normal

Change: Bug fix

Merge Risk: Low · up to 5e75e

A reset voice can briefly report a stale peak. The change is mergeable with awareness of this narrow issue.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning 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. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Issue [#314] has no explicit coding acceptance criteria. The change supports sampler peak-meter control 0x10000009, validates the control payload, records the post-filter peak for each render, and r…
Out of Scope Changes check Passed 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 in `tests/Ngs…
Description check Passed The description clearly explains the sampler voice peak-meter support, the startup failure it addresses, implementation details, testing, and remaining gaps.
Title check Passed The title clearly identifies the primary change: implementing the sampler voice peak meter for Trails in the Sky.
Full details: Linked Issues check

Explanation

Issue [#314] has no explicit coding acceptance criteria. The change supports sampler peak-meter control 0x10000009, validates the control payload, records the post-filter peak for each render, and reports peak_height while the meter is enabled. Ngs2SamplerTests.cpp covers 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 in tests/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.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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 help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-23 01:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#issuecomment-6355343158) · edited 2026-10-28 01:22 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/1853?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> 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`, `cosmo` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <strong>Priority: <picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/priority-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Normal</strong> <!-- change_assessment_commit:"5e75e20b24d172fb9ceb7055dd681ef8a16dcf9c" --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-low-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Low</strong> · up to <code>5e75e</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🔵 Low** · up to `5e75e` --> <!-- final_review_risk_coverage:{"sourceCommitId":"5e75e20b24d172fb9ceb7055dd681ef8a16dcf9c","coveredCommitId":"5e75e20b24d172fb9ceb7055dd681ef8a16dcf9c","kind":"reviewed"} --> A reset voice can briefly report a stale peak. The change is mergeable with awareness of this narrow issue. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | 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. Documenta… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Issue [`#314`] has no explicit coding acceptance criteria. The change supports sampler peak-meter control `0x10000009`, validates the control payload, records the post-filter peak for each render, and r… | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 in `tests/Ngs… | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description clearly explains the sampler voice peak-meter support, the startup failure it addresses, implementation details, testing, and remaining gaps. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly identifies the primary change: implementing the sampler voice peak meter for Trails in the Sky. | <details> <summary><strong>Full details: Linked Issues check</strong></summary> <dl> <dd> **Explanation** Issue [`#314`] has no explicit coding acceptance criteria. The change supports sampler peak-meter control `0x10000009`, validates the control payload, records the post-filter peak for each render, and reports `peak_height` while the meter is enabled. `Ngs2SamplerTests.cpp` covers 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. </dd> </dl> </details> <details> <summary><strong>Full details: Out of Scope Changes check</strong></summary> <dl> <dd> **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 in `tests/Ngs2SamplerTests.cpp`. The sampler rack block-count handling and reset behavior support the same feature. No unrelated change is established. </dd> </dl> </details> </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **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. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1853)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@⁠coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · edited 2026-10-25 23:30 UTC
On src/libs/ngs2.cpp line 1798 (outdated):

@@ -1791,6 +1795,7 @@ static void Ngs2RenderVoice(Ngs2VoiceInternal& voice, const std::vector<Ngs2Voic
 	voice.rendering = true;
 	voice.ClearSamples(grain);
 	voice.has_samples = false;
+	voice.peak_height = 0.0f; // a voice that is not playing renders silence

🎯 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, SetupSampler sets channels to zero. The next render returns at Line 1792 before this reset runs. Ngs2VoiceGetState continues to report the old peak although the voice rendered nothing. Move the reset before the channels == 0 check.

✅ Addressed in commit eaca50d

**@⁠coderabbitai[bot]** on GitHub · 2026-10-23 01:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#discussion_r4355298906) · edited 2026-10-25 23:30 UTC On `src/libs/ngs2.cpp` line 1798 (outdated): ```diff @@ -1791,6 +1795,7 @@ static void Ngs2RenderVoice(Ngs2VoiceInternal& voice, const std::vector<Ngs2Voic voice.rendering = true; voice.ClearSamples(grain); voice.has_samples = false; + voice.peak_height = 0.0f; // a voice that is not playing renders silence ``` <!-- coderabbit-ai-generated:v1 --> **🎯 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, `SetupSampler` sets `channels` to zero. The next render returns at Line 1792 before this reset runs. `Ngs2VoiceGetState` continues to report the old peak although the voice rendered nothing. Move the reset before the `channels == 0` check. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:21f90b03277e5f2ea310efa5 --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commit eaca50d
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-23 01:41 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 79837b423d and 0c5e1b1fdba8ae281549f78bc071ae8ae9c2af11.

📒 Files selected for processing: 2
  • src/libs/ngs2.cpp
  • tests/Ngs2SamplerTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-23 01:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#pullrequestreview-5622962059) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/79837b423dd035b34b2dc476e1d2dce8ae0d18bf/.coderabbit.yaml>) - **Commit**: [79837b4](<https://github.com/KytyPS5/KytyPS5/commit/79837b423dd035b34b2dc476e1d2dce8ae0d18bf>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `aa6dd904-e597-4989-a1de-142df33a4fa7` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 79837b423dd035b34b2dc476e1d2dce8ae0d18bf and 0c5e1b1fdba8ae281549f78bc071ae8ae9c2af11. </details> <details> <summary>📒 Files selected for processing: 2</summary> * `src/libs/ngs2.cpp` * `tests/Ngs2SamplerTests.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=4541ddc4-9f7c-4fb8-96d1-a0bed67e7852 attempt=eb3eac3d-924b-419b-bb70-76c0db5005ac batch=1/1 -->
@ -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});
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-28 01:21 UTC · original · edited 2026-10-28 01:45 UTC

Minor · Functional Correctness · Quick win

Preserve a callback reset during rendering.

If a voice produces samples and a later block-completion callback calls SetEvent(8) or SetEvent(4), SetEvent clears peak_height. Ngs2ConsumeSamples can still return with has_samples true 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](https://github.com/KytyPS5/KytyPS5/pull/1853#discussion_r4395980143) · edited 2026-10-28 01:45 UTC <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** **Preserve a callback reset during rendering.** If a voice produces samples and a later block-completion callback calls `SetEvent(8)` or `SetEvent(4)`, `SetEvent` clears `peak_height`. `Ngs2ConsumeSamples` can still return with `has_samples` true 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. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:525a446c2399c98080348ba7 --> <!-- This is an auto-generated reply by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-28 01:21 UTC · original · reviewed

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between eaca50d508963c34b189b283114cb3b7be6688d7 and 5e75e20b24.

Files selected for processing: 2
  • src/libs/ngs2.cpp
  • tests/Ngs2SamplerTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-28 01:21 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#pullrequestreview-5676635126) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** --- <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/1be1c6c4f928e52f740ae7f76841c1da74220e54/.coderabbit.yaml>) - **Commit**: [1be1c6c](<https://github.com/KytyPS5/KytyPS5/commit/1be1c6c4f928e52f740ae7f76841c1da74220e54>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `97f68f16-b3da-42e2-b827-aa50eacdccc3` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between eaca50d508963c34b189b283114cb3b7be6688d7 and 5e75e20b24d172fb9ceb7055dd681ef8a16dcf9c. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 2</summary> <dl> <dd> * `src/libs/ngs2.cpp` * `tests/Ngs2SamplerTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=1de95919-b552-476f-863d-0422093a1337 attempt=4e76f66e-024a-4d64-b5e8-9b2e4072c9e6 batch=1/1 -->
@ -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});
Author
Owner

@⁠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.

**@⁠MrBigBritches** on GitHub · 2026-10-28 01:45 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#discussion_r4396099912) 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});
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-28 01:46 UTC · original

@⁠MrBigBritches You're right. SetEvent(4) and SetEvent(8) do not discard samples already produced in the current grain. Destination mixing uses has_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
Learnt from: MrBigBritches
Repo: KytyPS5/KytyPS5 PR: 1853
File: src/libs/ngs2.cpp:1860-1862
Timestamp: 2026-10-28T01:45:55.911Z
Learning: Within repository "KytyPS5/KytyPS5", only for files matching one of ["src/libs/ngs2.cpp"]: When reviewing callback kill/reset handling and sampler peak metering in src/libs/ngs2.cpp, check whether samples already produced in the current grain remain available for destination mixing. If SetEvent(4) or SetEvent(8) leaves those samples intact and has_samples remains true, do not require the reported peak to remain zero solely because the voice became Empty; the enabled meter represents the retained, filtered samples for that render. Check that the next render clears the peak. Distinguish source replacement that changes source_generation, because Ngs2ConsumeSamples clears the current samples in that case.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

✅ Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-28 01:46 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1853#discussion_r4396102186) <!-- coderabbit-ai-generated:v1 --> @⁠MrBigBritches You're right. `SetEvent(4)` and `SetEvent(8)` do not discard samples already produced in the current grain. Destination mixing uses `has_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. --- <details> <summary>✏️ Learnings added</summary> <dl> <dd> ``` Learnt from: MrBigBritches Repo: KytyPS5/KytyPS5 PR: 1853 File: src/libs/ngs2.cpp:1860-1862 Timestamp: 2026-10-28T01:45:55.911Z Learning: Within repository "KytyPS5/KytyPS5", only for files matching one of ["src/libs/ngs2.cpp"]: When reviewing callback kill/reset handling and sampler peak metering in src/libs/ngs2.cpp, check whether samples already produced in the current grain remain available for destination mixing. If SetEvent(4) or SetEvent(8) leaves those samples intact and has_samples remains true, do not require the reported peak to remain zero solely because the voice became Empty; the enabled meter represents the retained, filtered samples for that render. Check that the next render clears the peak. Distinguish source replacement that changes source_generation, because Ngs2ConsumeSamples clears the current samples in that case. ``` > Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings). </dd> </dl> </details> <!-- <review_comment_withdrawn> --> ✅ Review thread resolved. <sub><i>You are interacting with an AI system.</i></sub> <!-- This is an auto-generated reply by CodeRabbit -->
This pull request has changes conflicting with the target branch.
  • src/libs/ngs2.cpp
  • tests/Ngs2SamplerTests.cpp
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.
git fetch -u origin '+refs/pull/1853/head:github-bot/gh-1853'
git switch 'github-bot/gh-1853'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1853'
git switch 'github-bot/gh-1853'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1853'
git switch 'github-bot/gh-1853'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1853'
git switch 'main'
git merge --squash 'github-bot/gh-1853'
git switch 'main'
git merge --ff-only 'github-bot/gh-1853'
git switch 'main'
git merge 'github-bot/gh-1853'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.