-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwav.rs
56 lines (48 loc) · 1.79 KB
/
wav.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
wget https://github.com/thewh1teagle/vad-rs/releases/download/v0.1.0/silero_vad.onnx
wget https://github.com/thewh1teagle/vad-rs/releases/download/v0.1.0/motivation.wav
cargo run --example wav silero_vad.onnx motivation.wav
*/
use hound::WavReader;
use vad_rs::{Vad, VadStatus};
fn main() {
let model_path = std::env::args()
.nth(1)
.expect("Please specify model filename");
let audio_path = std::env::args()
.nth(2)
.expect("Please specify audio filename");
let mut reader = WavReader::open(audio_path).unwrap();
let spec = reader.spec();
let mut vad = Vad::new(model_path, spec.sample_rate.try_into().unwrap()).unwrap();
let chunk_size = (0.1 * spec.sample_rate as f32) as usize; // 0.1s
let mut samples: Vec<f32> = reader
.samples::<i16>()
.map(|s| s.unwrap() as f32 / i16::MAX as f32)
.collect();
let mut is_speech = false;
let mut start_time = 0.0;
let sample_rate = spec.sample_rate as f32;
// Add 1s of silence to the end of the samples
samples.extend(vec![0.0; sample_rate as usize]);
for (i, chunk) in samples.chunks(chunk_size).enumerate() {
let time = i as f32 * chunk_size as f32 / sample_rate;
if let Ok(mut result) = vad.compute(chunk) {
match result.status() {
VadStatus::Speech => {
if !is_speech {
start_time = time;
is_speech = true;
}
}
VadStatus::Silence => {
if is_speech {
println!("Speech detected from {:.2}s to {:.2}s", start_time, time);
is_speech = false;
}
}
_ => {}
}
}
}
}