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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
mod bindgen;
use bindgen::*;
pub enum VadMode {
Quality,
LowBitrate,
Aggressive,
VeryAggressive,
}
pub struct Vad {
fvad: *mut Fvad
}
impl Vad {
pub fn new(sample_rate: i32) -> Result<Vad, ()> {
unsafe {
let fvad: *mut Fvad = fvad_new();
if fvad == std::ptr::null_mut() { panic!("fvad_new() did not return a valid instance (memory allocation error)"); }
let mut instance = Vad { fvad };
instance.set_sample_rate(sample_rate)?;
Ok(instance)
}
}
pub fn reset(&mut self) {
unsafe {
fvad_reset(self.fvad);
}
}
pub fn set_sample_rate(&mut self, sample_rate: i32) -> Result<(), ()> {
unsafe {
match fvad_set_sample_rate(self.fvad, sample_rate) {
0 => Ok(()),
_ => Err(()),
}
}
}
pub fn fvad_set_mode(&mut self, mode: VadMode) -> Result<(), ()> {
let imode;
match mode {
VadMode::Quality => imode = 0,
VadMode::LowBitrate => imode = 1,
VadMode::Aggressive => imode = 2,
VadMode::VeryAggressive => imode = 3,
}
unsafe {
match fvad_set_mode(self.fvad, imode) {
0 => Ok(()),
_ => Err(())
}
}
}
pub fn is_voice_segment(&mut self, buffers: &[i16]) -> Result<bool, ()> {
let buffer = &buffers[0] as *const i16;
unsafe {
match fvad_process(self.fvad, buffer, buffers.len()) {
1 => Ok(true),
0 => Ok(false),
_ => Err(())
}
}
}
}
impl Drop for Vad {
fn drop(&mut self) {
unsafe {
fvad_free(self.fvad);
}
}
}