-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add ten-vad Rust API example to remove silences from a file #3778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright (c) 2026 Xiaomi Corporation | ||
| // | ||
| // This file demonstrates how to use ten-vad with sherpa-onnx's | ||
| // Rust API to remove non-speech segments and save speech-only audio. | ||
| // | ||
| // See ../README.md for how to run it | ||
|
|
||
| use clap::Parser; | ||
| use sherpa_onnx::{self, TenVadModelConfig, VadModelConfig, VoiceActivityDetector, Wave}; | ||
|
|
||
| /// Simple VAD example: remove non-speech segments from a WAV file using ten-vad | ||
| #[derive(Parser, Debug)] | ||
| #[command(author, version, about, long_about = None)] | ||
| struct Args { | ||
| /// Path to input WAV file | ||
| #[arg(long)] | ||
| input: String, | ||
|
|
||
| /// Path to output WAV file | ||
| #[arg(long)] | ||
| output: String, | ||
|
|
||
| /// Path to ten-vad ONNX model | ||
| #[arg(long)] | ||
| ten_vad_model: String, | ||
| } | ||
|
|
||
| fn main() -> anyhow::Result<()> { | ||
| let args = Args::parse(); | ||
|
|
||
| // Read WAV file | ||
| let wave = Wave::read(&args.input) | ||
| .ok_or_else(|| anyhow::anyhow!("Failed to read WAV file: {}", &args.input))?; | ||
| let sample_rate = wave.sample_rate(); | ||
| let input_num_samples = wave.num_samples(); | ||
| let input_duration = input_num_samples as f32 / sample_rate as f32; | ||
|
|
||
| println!( | ||
| "Input WAV: sample rate: {}, num samples: {}, duration: {:.2}s", | ||
| sample_rate, input_num_samples, input_duration | ||
| ); | ||
|
|
||
| if sample_rate != 16000 { | ||
| anyhow::bail!( | ||
| "ten-vad expects a sample rate of 16000 Hz, but the input file has {} Hz", | ||
| sample_rate | ||
| ); | ||
| } | ||
|
|
||
| // Configure VAD | ||
| let mut ten_vad_config = TenVadModelConfig::default(); | ||
| ten_vad_config.model = Some(args.ten_vad_model); | ||
|
|
||
| // ten-vad expects a window size of 256 samples at 16 kHz. | ||
| // Please don't change it unless you know what you are doing. | ||
| ten_vad_config.window_size = 256; | ||
|
|
||
| // You can tune the values below | ||
| ten_vad_config.threshold = 0.5; | ||
| ten_vad_config.min_silence_duration = 0.25; | ||
| ten_vad_config.min_speech_duration = 0.5; | ||
| ten_vad_config.max_speech_duration = 5.0; | ||
|
|
||
| let window_size = ten_vad_config.window_size as usize; | ||
|
|
||
| let vad_config = VadModelConfig { | ||
| silero_vad: Default::default(), | ||
| ten_vad: ten_vad_config, | ||
| sample_rate, | ||
| num_threads: 1, | ||
| provider: Some("cpu".to_string()), | ||
| debug: false, | ||
| }; | ||
|
|
||
| let vad = VoiceActivityDetector::create(&vad_config, 30.0) | ||
| .ok_or_else(|| anyhow::anyhow!("Failed to create VoiceActivityDetector"))?; | ||
|
|
||
| let mut speech_samples = Vec::with_capacity(input_num_samples as usize); | ||
|
|
||
| for chunk in wave.samples().chunks(window_size) { | ||
| vad.accept_waveform(chunk); | ||
|
|
||
| while let Some(seg) = vad.front() { | ||
| speech_samples.extend_from_slice(seg.samples()); | ||
| vad.pop(); | ||
| } | ||
| } | ||
|
|
||
| vad.flush(); | ||
| while let Some(seg) = vad.front() { | ||
| speech_samples.extend_from_slice(seg.samples()); | ||
| vad.pop(); | ||
| } | ||
|
|
||
| // Write speech-only samples to output WAV | ||
| let ok = sherpa_onnx::write(&args.output, &speech_samples, sample_rate); | ||
| if !ok { | ||
| anyhow::bail!("Failed to save speech-only audio to {}", args.output); | ||
| } | ||
| println!("Saved speech-only audio to {}", args.output); | ||
|
|
||
| // Summary | ||
| let output_num_samples = speech_samples.len(); | ||
| let output_duration = output_num_samples as f32 / sample_rate as f32; | ||
| println!("\n=== Summary ==="); | ||
| println!( | ||
| "Input: sample rate = {}, samples = {}, duration = {:.2}s", | ||
| sample_rate, input_num_samples, input_duration | ||
| ); | ||
| println!( | ||
| "Output: sample rate = {}, samples = {}, duration = {:.2}s", | ||
| sample_rate, output_num_samples, output_duration | ||
| ); | ||
| if input_duration > 0.0 { | ||
| println!( | ||
| "Removed non-speech: {:.2}% of input removed", | ||
| 100.0 * (1.0 - output_duration / input_duration) | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||
| set -ex | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # https://k2-fsa.github.io/sherpa/onnx/vad/index.html | ||||||||||||||||||||||||||||||
| if [ ! -f "./ten-vad.onnx" ]; then | ||||||||||||||||||||||||||||||
| curl -SLf -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx | ||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if [ ! -f ./lei-jun-test.wav ]; then | ||||||||||||||||||||||||||||||
| curl -SLf -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav | ||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||
|
Comment on lines
+5
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default,
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| cargo run --example ten_vad_remove_silence -- \ | ||||||||||||||||||||||||||||||
| --input ./lei-jun-test.wav \ | ||||||||||||||||||||||||||||||
| --output ./no-silence-ten-vad.wav \ | ||||||||||||||||||||||||||||||
| --ten-vad-model ./ten-vad.onnx | ||||||||||||||||||||||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
ten-vadstrictly requires a sample rate of 16000 Hz (and the underlying C++ implementation will abruptly terminate the process withexit(-1)if it is not), it is highly recommended to validate the sample rate in Rust and return a graceful error.