Skip to content

fix(stk): use the Csound sample rate in all instrument adapters - #58

Merged
vlazzarini merged 1 commit into
csound:developfrom
hlolli:fix/stk-sample-rate
Oct 18, 2026
Merged

vlazzarini merged 1 commit into
csound:developfrom
hlolli:fix/stk-sample-rate

Conversation

@hlolli

@hlolli hlolli commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

STK instruments keep their default 44.1 kHz sample rate even when Csound runs at another rate. In the reported Saxofony example, this makes a 48 kHz render about 147 cents sharper than a 44.1 kHz render. The rate-setting calls were lost in d2e2057.

Restore the effective instrument sample rate (insdshead->esr) in both shared adapters, before construction and noteOn(). Set it on reinitialization too. This covers all 27 Csound 7 STK opcodes and keeps the constructor error handling intact.

Move the adapters to a header so native unit tests can use a small probe instrument. Six tests cover both constructor forms, changes between 44.1/48/96 kHz, and reinitialization of an existing instrument. They check the rate seen by the constructor and noteOn() without rendering audio or starting Csound. CTest runs them when GoogleTest is available, including the existing Linux CI job.

Validation:

  • All six new tests failed before the fix and pass after it.
  • Native STK plugin and Nix WASM builds pass; all 11 existing plugin tests pass.
  • Repeating the Saxofony render at 44.1 and 48 kHz reduces the measured pitch difference from 146.7 to 1.6 cents. The model's small baseline detuning remains.

Summary by CodeRabbit

  • Bug Fixes
    • STK instrument opcodes now apply the engine’s current sample rate when instruments are initialized or restarted, helping maintain accurate playback when the sample rate changes.
  • Improvements
    • STK instrument playback now handles controller updates and released notes consistently, with silent output outside the active processing period.

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: csound/plugins/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f0844ea4-cf52-4129-b8e9-d329b89c2863
📥 Commits

Reviewing files that changed from the base of the PR and between 8de8d04 and ebb61ab.

📒 Files selected for processing: 4
  • 7.x/src/stk/CMakeLists.txt
  • 7.x/src/stk/STKInstrumentAdapter.hpp
  • 7.x/src/stk/stkOpcodes.cpp
  • 7.x/src/stk/tests/stk_adapter_test.cpp

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

📝 Walkthrough

The STK adapter templates move from stkOpcodes.cpp into STKInstrumentAdapter.hpp. Native Google Test coverage and a conditional CMake test target are added to check sample-rate behavior for both adapter forms.

Changes: STK adapter changes

Layer / File(s) Summary
Adapter runtime and opcode integration
7.x/src/stk/STKInstrumentAdapter.hpp, 7.x/src/stk/stkOpcodes.cpp
The two adapter templates now reside in a header included by stkOpcodes.cpp. They set STK’s shared sample rate during initialization, handle instrument creation, controller updates, audio output, and cleanup.
Sample-rate test coverage
7.x/src/stk/tests/stk_adapter_test.cpp, 7.x/src/stk/CMakeLists.txt
Typed tests check sample rates during construction and note start, including reinitialization. CMake builds and registers the tests for native builds when GTest is found.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Suggested reviewers: vlazzarini

Merge Risk: ⚪ Minimal · up to ebb61

This change restores Csound's sample rate in the STK instrument adapters, so instruments no longer play out of tune when Csound runs at rates other than 44.1 kHz. It also adds native unit tests for this behavior. No concrete defect was found, and the change appears ready to merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 3 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: using the Csound sample rate in all STK instrument adapters.
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.
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 3 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • 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.

@vlazzarini
vlazzarini merged commit 1267e42 into csound:develop Oct 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants