Expose the pyannote window shift ratio in the C API and language bindings - #3870
Conversation
Expose the pyannote segmentation window shift ratio through the C API, CXX wrapper, and supported language bindings. Treat non-positive C values as the native 0.1 default so callers that zero-initialize config structs keep their existing behavior.
📝 WalkthroughWalkthroughThe PR adds a Pyannote segmentation window shift ratio with a default of ChangesPyannote window shift ratio
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@sherpa-onnx/c-api/docs/speaker-diarization.dox`:
- Around line 17-19: Update the comment above
config.segmentation.pyannote.window_shift_ratio to state that non-positive
values use the default shift ratio of 0.1, and describe the setting as
controlling the sliding-window shift rather than overlap. Keep the example value
and valid range unchanged.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: b7afe5ce-fb83-44f4-ab90-dda71f82f8d8
📒 Files selected for processing (39)
android/SherpaOnnxSpeakerDiarization/app/src/main/java/com/k2fsa/sherpa/onnx/speaker/diarization/SpeakerDiarizationObject.ktc-api-examples/offline-speaker-diarization-c-api.ccxx-api-examples/offline-speaker-diarization-cxx-api.ccdart-api-examples/speaker-diarization/bin/speaker-diarization.dartdotnet-examples/offline-speaker-diarization/Program.csflutter/sherpa_onnx/lib/src/offline_speaker_diarization.dartflutter/sherpa_onnx/lib/src/offline_speaker_diarization_config.dartflutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dartgo-api-examples/non-streaming-speaker-diarization/main.goharmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.ccharmony-os/SherpaOnnxHar/sherpa_onnx/src/main/ets/components/NonStreamingSpeakerDiarization.etsharmony-os/SherpaOnnxSpeakerDiarization/entry/src/main/ets/workers/SpeakerDiarizationWorker.etsjava-api-examples/OfflineSpeakerDiarizationDemo.javakotlin-api-examples/test_offline_speaker_diarization.ktnodejs-addon-examples/test_offline_speaker_diarization.jsnodejs-examples/test-offline-speaker-diarization.jspascal-api-examples/speaker-diarization/main.paspython-api-examples/offline-speaker-diarization.pyrust-api-examples/examples/offline_speaker_diarization.rsscripts/dotnet/OfflineSpeakerSegmentationPyannoteModelConfig.csscripts/go/sherpa_onnx.goscripts/node-addon-api/lib/types.jssherpa-onnx/c-api/c-api.ccsherpa-onnx/c-api/c-api.hsherpa-onnx/c-api/cxx-api.ccsherpa-onnx/c-api/cxx-api.hsherpa-onnx/c-api/docs/speaker-diarization.doxsherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model.ccsherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationPyannoteModelConfig.javasherpa-onnx/jni/offline-speaker-diarization.ccsherpa-onnx/kotlin-api/OfflineSpeakerDiarization.ktsherpa-onnx/pascal-api/sherpa_onnx.passherpa-onnx/python/csrc/offline-speaker-diarization.ccsherpa-onnx/rust/sherpa-onnx-sys/src/offline_speaker_diarization.rssherpa-onnx/rust/sherpa-onnx/src/offline_speaker_diarization.rsswift-api-examples/SherpaOnnx.swiftswift-api-examples/speaker-diarization.swiftwasm/speaker-diarization/sherpa-onnx-speaker-diarization.jswasm/speaker-diarization/sherpa-onnx-wasm-main-speaker-diarization.cc
| // Zero uses the default window shift ratio of 0.1. Set a value in (0, 1] | ||
| // to change the sliding-window overlap. | ||
| config.segmentation.pyannote.window_shift_ratio = 0.1f; |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
Correct the sentinel and terminology in the C API example.
The C API maps every non-positive value, including negative values, to 0.1 in sherpa-onnx/c-api/c-api.cc Lines 3152-3155. The comment mentions only zero. Also, window_shift_ratio controls the shift. It does not directly specify the overlap.
Proposed documentation fix
-// Zero uses the default window shift ratio of 0.1. Set a value in (0, 1]
-// to change the sliding-window overlap.
+// Zero or a negative value uses the default window shift ratio of 0.1.
+// Set a value in (0, 1] to change the sliding-window shift and resulting overlap.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Zero uses the default window shift ratio of 0.1. Set a value in (0, 1] | |
| // to change the sliding-window overlap. | |
| config.segmentation.pyannote.window_shift_ratio = 0.1f; | |
| // Zero or a negative value uses the default window shift ratio of 0.1. | |
| // Set a value in (0, 1] to change the sliding-window shift and resulting overlap. | |
| config.segmentation.pyannote.window_shift_ratio = 0.1f; |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@sherpa-onnx/c-api/docs/speaker-diarization.dox` around lines 17 - 19, Update
the comment above config.segmentation.pyannote.window_shift_ratio to state that
non-positive values use the default shift ratio of 0.1, and describe the setting
as controlling the sliding-window shift rather than overlap. Keep the example
value and valid range unchanged.
There was a problem hiding this comment.
Pull request overview
This PR exposes OfflineSpeakerSegmentationPyannoteModelConfig::window_shift_ratio (added previously in the C++ core/CLI) through the C API/CXX API and propagates it across existing language bindings and examples so the configuration knob is accessible outside the CLI.
Changes:
- Extend the C API/CXX API speaker-diarization config structs/conversion to include
window_shift_ratio, with C-side sentinel behavior (<= 0→ default0.1) to preserve zero-initialized callers. - Thread the new field through multiple language bindings (Python, Rust, Java/Kotlin/JNI, Go, .NET, Flutter/Dart, Swift, Pascal, Node/WASM/HarmonyOS) and update examples accordingly.
- Add a debug-gated log in the pyannote segmentation model reporting the computed window shift in samples, and adjust the WASM binding’s layout assumptions.
Reviewed changes
Copilot reviewed 38 out of 39 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| wasm/speaker-diarization/sherpa-onnx-wasm-main-speaker-diarization.cc | Updates WASM-side struct size assertions and prints the new ratio for debugging. |
| wasm/speaker-diarization/sherpa-onnx-speaker-diarization.js | Extends the WASM JS glue to write windowShiftRatio into the packed config struct and sets typed defaults. |
| swift-api-examples/speaker-diarization.swift | Updates Swift example to pass windowShiftRatio. |
| swift-api-examples/SherpaOnnx.swift | Extends Swift helper API to accept windowShiftRatio (default 0.1). |
| sherpa-onnx/rust/sherpa-onnx/src/offline_speaker_diarization.rs | Adds window_shift_ratio to the safe Rust config and defaults it to 0.1. |
| sherpa-onnx/rust/sherpa-onnx-sys/src/offline_speaker_diarization.rs | Extends the raw Rust FFI struct with the new window_shift_ratio field. |
| sherpa-onnx/python/csrc/offline-speaker-diarization.cc | Exposes window_shift_ratio in the pybind11 config class (default 0.1). |
| sherpa-onnx/pascal-api/sherpa_onnx.pas | Adds WindowShiftRatio to Pascal records, initializes defaults, and passes it into the C config. |
| sherpa-onnx/kotlin-api/OfflineSpeakerDiarization.kt | Adds windowShiftRatio to Kotlin config data class (default 0.1f). |
| sherpa-onnx/jni/offline-speaker-diarization.cc | Reads windowShiftRatio from JVM objects into the native config via JNI macro. |
| sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationPyannoteModelConfig.java | Adds windowShiftRatio to Java builder/config object (default 0.1f). |
| sherpa-onnx/csrc/offline-speaker-segmentation-pyannote-model.cc | Adds debug-only logging of the computed shift (in samples). |
| sherpa-onnx/c-api/docs/speaker-diarization.dox | Documents window_shift_ratio usage in the C API docs snippet. |
| sherpa-onnx/c-api/cxx-api.h | Adds window_shift_ratio to the CXX API config with default 0.1f. |
| sherpa-onnx/c-api/cxx-api.cc | Propagates window_shift_ratio from CXX API config into the C config struct. |
| sherpa-onnx/c-api/c-api.h | Adds window_shift_ratio to the C struct, documenting sentinel semantics for <= 0. |
| sherpa-onnx/c-api/c-api.cc | Implements sentinel mapping (<= 0 → 0.1f) during C→C++ config conversion. |
| scripts/node-addon-api/lib/types.js | Updates Node addon JSDoc typedef to include windowShiftRatio default. |
| scripts/go/sherpa_onnx.go | Adds WindowShiftRatio to Go config and passes it to the C struct. |
| scripts/dotnet/OfflineSpeakerSegmentationPyannoteModelConfig.cs | Adds WindowShiftRatio to .NET struct and initializes it to 0.1f. |
| rust-api-examples/examples/offline_speaker_diarization.rs | Updates Rust example to set window_shift_ratio. |
| python-api-examples/offline-speaker-diarization.py | Updates Python example to pass window_shift_ratio. |
| pascal-api-examples/speaker-diarization/main.pas | Updates Pascal example to set WindowShiftRatio. |
| nodejs-examples/test-offline-speaker-diarization.js | Updates NodeJS example config to set windowShiftRatio. |
| nodejs-addon-examples/test_offline_speaker_diarization.js | Updates Node addon example config to set windowShiftRatio. |
| kotlin-api-examples/test_offline_speaker_diarization.kt | Updates Kotlin example to pass windowShiftRatio. |
| java-api-examples/OfflineSpeakerDiarizationDemo.java | Updates Java example to set windowShiftRatio via builder. |
| harmony-os/SherpaOnnxSpeakerDiarization/entry/src/main/ets/workers/SpeakerDiarizationWorker.ets | Sets windowShiftRatio in HarmonyOS worker config and fixes a trailing brace formatting change. |
| harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/ets/components/NonStreamingSpeakerDiarization.ets | Adds windowShiftRatio to the ETS config class. |
| harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/cpp/non-streaming-speaker-diarization.cc | Reads windowShiftRatio from N-API object into the native config. |
| go-api-examples/non-streaming-speaker-diarization/main.go | Updates Go example to set WindowShiftRatio. |
| flutter/sherpa_onnx/lib/src/sherpa_onnx_bindings.dart | Extends Flutter FFI struct with windowShiftRatio. |
| flutter/sherpa_onnx/lib/src/offline_speaker_diarization.dart | Copies windowShiftRatio into the native config before creation. |
| flutter/sherpa_onnx/lib/src/offline_speaker_diarization_config.dart | Adds windowShiftRatio to Dart config model, JSON, and toString. |
| dotnet-examples/offline-speaker-diarization/Program.cs | Updates .NET example to set WindowShiftRatio. |
| dart-api-examples/speaker-diarization/bin/speaker-diarization.dart | Updates Dart example to pass windowShiftRatio. |
| cxx-api-examples/offline-speaker-diarization-cxx-api.cc | Updates CXX API example to set window_shift_ratio. |
| c-api-examples/offline-speaker-diarization-c-api.c | Updates C API example and documents the “0 uses default” behavior. |
| android/SherpaOnnxSpeakerDiarization/app/src/main/java/com/k2fsa/sherpa/onnx/speaker/diarization/SpeakerDiarizationObject.kt | Updates Android app wiring to pass windowShiftRatio. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The C API example called the value the sliding-window overlap. It sets the shift, and a smaller shift is what produces more overlap, so say that directly and note the compute cost that comes with it.
csukuangfj
left a comment
There was a problem hiding this comment.
Thank you for your contribution!
Follow-up to #3769, which added
window_shift_ratioto the C++ core and the CLI but deliberately left the bindings alone to stay reviewable. This exposesOfflineSpeakerSegmentationPyannoteModelConfig::window_shift_ratiothrough the C and CXX APIs and the language bindings that already surface that config, so the knob is reachable outside the CLI.The C struct treats
0and negative values as unset and maps them to0.1. C API callers and raw bindings commonly zero-initialize config structs, and passing a zero straight through would fail the native(0, 1]validation and break every existing caller. The sentinel lives in one place, the config conversion inc-api.cc, andc-api.hdocuments it. NaN is not treated as unset; it still reaches native validation and is rejected. Typed binding defaults are0.1.For the standard
window_size=160000model the default path still computes a 16000-sample shift. A C API probe returned 16000 samples from a zero-initialized field and 32000 samples at0.2. A build of untouchedupstream/masterand a build of this branch produced byte-identical segment output with the ratio unset (same ten segments, same SHA-256).Two things worth calling out beyond the mechanical binding work. There is a new debug-only log line in
offline-speaker-segmentation-pyannote-model.ccthat reports the computed shift in samples, which is what makes propagation observable from a binding; it is inside the existingconfig_.debugguard and does not change normal output. And the WASM binding's struct packing moves from 4 to 8 bytes with its compile-time layout assertion updated to match. I could not compile that one locally, so it is worth a look in CI.The benchmark that motivated the core option, for context: a 644 s two-speaker English WAV on macOS arm64,
0.10at 40 s,0.15at 27 s (1.48x),0.20at 20 s (2.00x), with 99.89% and 99.93% frame-level label agreement against0.10.upstream/master0.2native shiftdotnetunavailablegit diff --checkpasses and no generated artifacts are included.Summary by CodeRabbit
New Features
0.1.Documentation
Bug Fixes