audio: keep NGS2 sampler voices within max_waveform_blocks #1450

Manually merged
github merged 1 commit from gh-pr/1450/head into gh-pr/1450/base 2026-11-26 09:57:02 +00:00
Owner

@⁠Cosmo on GitHub · 2026-10-12 11:43 UTC · original
Cosmo:fix/ngs2-max-waveform-blocks → main · +549 −143 · 2 files · 1 commits · merged 2026-10-12 12:22 UTC

Quake's background music was distorted and sounded like an alarm on repeat, compared to the game on PS5.
Quake streams the music through a ring of 4 buffers and reuses the oldest one for each new block.
Our sampler voices kept every block they got and ignored max_waveform_blocks, so as far as I can tell, the voice was still playing a block that Quake was already overwriting.

This change drops the oldest block once a voice holds more than max_waveform_blocks. With it, the music sounds right.

I don't know what the PS5 actually does when a voice gets more blocks than the limit. Dropping the oldest is my guess from how Quake uses its buffers. So the change could affect other games, for better or worse.

AI use

Research, implementation and tests were done with Claude Code at my direction.
I reviewed the code, and tested it as described above.

**@⁠Cosmo** on GitHub · 2026-10-12 11:43 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1450) `Cosmo:fix/ngs2-max-waveform-blocks` → `main` · +549 −143 · 2 files · 1 commits · merged 2026-10-12 12:22 UTC Quake's background music was distorted and sounded like an alarm on repeat, compared to the game on PS5. Quake streams the music through a ring of 4 buffers and reuses the oldest one for each new block. Our sampler voices kept every block they got and ignored `max_waveform_blocks`, so as far as I can tell, the voice was still playing a block that Quake was already overwriting. This change drops the oldest block once a voice holds more than `max_waveform_blocks`. With it, the music sounds right. I don't know what the PS5 actually does when a voice gets more blocks than the limit. Dropping the oldest is my guess from how Quake uses its buffers. So the change could affect other games, for better or worse. ## AI use Research, implementation and tests were done with Claude Code at my direction. I reviewed the code, and tested it as described above.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:56:59 +00:00
github manually merged commit 73d1de5e2a into gh-pr/1450/base 2026-11-26 09:57:02 +00:00
Author
Owner

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

Review in Change Stack →

📝 Walkthrough

Walkthrough

Ngs2VoiceControl now removes the oldest queued PCM waveform blocks when the nonzero rack limit is exceeded. This applies to standard and custom sampler racks when no decoder is active. ATRAC9 queues and queues with a zero limit are not trimmed. Tests verify the queue size and rendered samples for both sampler types.

Suggested reviewers: nmzik


Priority: ➖ Normal

Change: Bug fix

Merge Risk: 🟡 Moderate · up to 10ee6

Trimming may leave callback-driven audio suppliers waiting to reuse a buffer. Clarify the discarded-block callback behavior before merging, or explicitly accept that risk.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: enforcing max_waveform_blocks for NGS2 sampler voices.
Description check Passed The description explains the Quake audio issue, the cause, the implemented behavior, testing, and the known uncertainty about effects on other games.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

  • 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-12 11:46 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1450#issuecomment-6123631006) <!-- 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/1450?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> ## Walkthrough Ngs2VoiceControl now removes the oldest queued PCM waveform blocks when the nonzero rack limit is exceeded. This applies to standard and custom sampler racks when no decoder is active. ATRAC9 queues and queues with a zero limit are not trimmed. Tests verify the queue size and rendered samples for both sampler types. **Suggested reviewers:** `nmzik` <hr> </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"10ee66076fb3204065a2454325ac781bfd10777b" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** **🟡 Moderate** · up to `10ee6` <!-- final_review_risk_coverage:{"sourceCommitId":"10ee66076fb3204065a2454325ac781bfd10777b","coveredCommitId":"10ee66076fb3204065a2454325ac781bfd10777b","kind":"reviewed"} --> Trimming may leave callback-driven audio suppliers waiting to reuse a buffer. Clarify the discarded-block callback behavior before merging, or explicitly accept that risk. <!-- final_review_risk_end --> <!-- 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-v1.svg" width="14" height="17" 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-v1.svg" width="14" height="17" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong>✅ Passed checks (4 passed)</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely describes the main change: enforcing max_waveform_blocks for NGS2 sampler voices. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description explains the Quake audio issue, the cause, the implemented behavior, testing, and the known uncertainty about effects on other games. | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | </dd> </dl> </details> <hr> </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=1450)! 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) <hr> </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-12 11:47 UTC · original
On src/libs/ngs2.cpp line 2415 (outdated):

@@ -2406,6 +2406,14 @@ int KYTY_SYSV_ABI Ngs2VoiceControl(uintptr_t voice_handle, const Ngs2VoiceParamH
 							        block.data_size);
 							voice->blocks.push_back({blocks.data + block.data_offset, block});
 						}
+						// PPSA01880 reuses the oldest of its four buffers for each new block. Dropping the oldest block beyond
+						// max_waveform_blocks fixes its music; what the PS5 does here is unverified.
+						const uint32_t max_blocks = voice->rack->type == Ngs2RackType::Sampler
+						                                ? voice->rack->option.sampler.max_waveform_blocks
+						                                : voice->rack->option.custom_sampler.max_waveform_blocks;
+						while (voice->decoder == nullptr && max_blocks != 0 && voice->blocks.size() > max_blocks) {
+							voice->blocks.pop_front();

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Notify the caller when trimming removes a block.

If a voice has a completion callback and a new PCM block exceeds max_waveform_blocks, pop_front() removes the oldest block without calling Ngs2FinishBlock. The caller never receives that block’s completion notification. A caller that waits for the notification before reusing a buffer can stop supplying audio. Preserve the completion callback contract when discarding a block.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-12 11:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1450#discussion_r4247650165) On `src/libs/ngs2.cpp` line 2415 (outdated): ```diff @@ -2406,6 +2406,14 @@ int KYTY_SYSV_ABI Ngs2VoiceControl(uintptr_t voice_handle, const Ngs2VoiceParamH block.data_size); voice->blocks.push_back({blocks.data + block.data_offset, block}); } + // PPSA01880 reuses the oldest of its four buffers for each new block. Dropping the oldest block beyond + // max_waveform_blocks fixes its music; what the PS5 does here is unverified. + const uint32_t max_blocks = voice->rack->type == Ngs2RackType::Sampler + ? voice->rack->option.sampler.max_waveform_blocks + : voice->rack->option.custom_sampler.max_waveform_blocks; + while (voice->decoder == nullptr && max_blocks != 0 && voice->blocks.size() > max_blocks) { + voice->blocks.pop_front(); ``` **🎯 Functional Correctness** | **🟠 Major** | **🏗️ Heavy lift** **Notify the caller when trimming removes a block.** If a voice has a completion callback and a new PCM block exceeds `max_waveform_blocks`, `pop_front()` removes the oldest block without calling `Ngs2FinishBlock`. The caller never receives that block’s completion notification. A caller that waits for the notification before reusing a buffer can stop supplying audio. Preserve the completion callback contract when discarding a block. <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:a2121aae89e3d0b40d0ea00e --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-12 11:47 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: af90f0dc-402e-4728-adc2-3eae8bdf71b5
📥 Commits

Reviewing files that changed from the base of the PR and between 5ce9a52936 and 10ee66076fb3204065a2454325ac781bfd10777b.

📒 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; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-12 11:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1450#pullrequestreview-5490279748) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `af90f0dc-402e-4728-adc2-3eae8bdf71b5` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between 5ce9a529367d4c78a2b85de8a449796c5159cd8f and 10ee66076fb3204065a2454325ac781bfd10777b. </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; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=e0d4bb47-cf76-4e9d-acb6-698ad34a3c97 attempt=5c681555-3a59-44d7-89c7-e8da5d474059 batch=1/1 -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-12 12:22 UTC · original

@⁠Cosmo Much better now :) Thanks

**@⁠Nmzik** on GitHub · 2026-10-12 12:22 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1450#issuecomment-6124174290) @⁠Cosmo Much better now :) Thanks
This discussion has been locked. Commenting is limited to contributors.
No description provided.