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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::convert::TryFrom;

use libfvad_sys as ffi;

#[derive(PartialEq, Debug)]
pub enum Mode {
    Quality = 0,
    LowBitrate = 1,
    Aggressive = 2,
    VeryAggressive = 3,
}

#[derive(PartialEq, Debug)]
pub enum SampleRate {
    Rate8kHz = 8000,
    Rate16kHz = 16000,
    Rate32kHz = 32000,
    Rate48kHz = 48000,
}

impl TryFrom<u64> for SampleRate {
    type Error = ();

    fn try_from(n: u64) -> Result<Self, Self::Error> {
        match n {
            8000 => Ok(SampleRate::Rate8kHz),
            16000 => Ok(SampleRate::Rate16kHz),
            32000 => Ok(SampleRate::Rate32kHz),
            48000 => Ok(SampleRate::Rate48kHz),
            _ => Err(()),
        }
    }
}

impl TryFrom<u32> for SampleRate {
    type Error = ();

    fn try_from(n: u32) -> Result<Self, Self::Error> {
        SampleRate::try_from(n as u64)
    }
}

impl TryFrom<u16> for SampleRate {
    type Error = ();

    fn try_from(n: u16) -> Result<Self, Self::Error> {
        SampleRate::try_from(n as u64)
    }
}

impl TryFrom<i64> for SampleRate {
    type Error = ();

    fn try_from(n: i64) -> Result<Self, Self::Error> {
        match n {
            8000 => Ok(SampleRate::Rate8kHz),
            16000 => Ok(SampleRate::Rate16kHz),
            32000 => Ok(SampleRate::Rate32kHz),
            48000 => Ok(SampleRate::Rate48kHz),
            _ => Err(()),
        }
    }
}

impl TryFrom<i32> for SampleRate {
    type Error = ();

    fn try_from(n: i32) -> Result<Self, Self::Error> {
        SampleRate::try_from(n as i64)
    }
}

impl TryFrom<i16> for SampleRate {
    type Error = ();

    fn try_from(n: i16) -> Result<Self, Self::Error> {
        SampleRate::try_from(n as i64)
    }
}

pub struct Fvad {
    fvad: *mut ffi::Fvad,
}

impl Fvad {
    /// Creates and initializes a VAD instance.
    ///
    /// Returns:
    /// - `Some(Self)` wrapping a pointer to the new VAD instance on success
    /// - `None` in case of a memory allocation error
    pub fn new() -> Option<Self> {
        let fvad = unsafe { ffi::fvad_new() };
        match fvad.is_null() {
            false => Some(fvad.into()),
            true => None,
        }
    }

    /// Reinitializes a VAD instance, clearing all state and resetting mode and
    /// sample rate to defaults.
    pub fn reset(self) {
        unsafe { ffi::fvad_reset(self.fvad) }
    }

    /// Changes the VAD operating ("aggressiveness") mode of a VAD instance.
    ///
    /// A more aggressive (higher mode) VAD is more restrictive in reporting speech.
    /// Put in other words the probability of being speech when the VAD returns 1 is
    /// increased with increasing mode. As a consequence also the missed detection
    /// rate goes up.
    /// The default mode is `Mode::Quality`.
    pub fn set_mode(self, mode: Mode) -> Self {
        let v = mode as i32;
        match unsafe { ffi::fvad_set_mode(self.fvad, v) } {
            0 => self,
            -1 => panic!("fvad_set_mode() did not accept {} as a valid mode", v),
            n => panic!("fvad_set_mode() returned {}", n),
        }
    }

    /// Sets the input sample rate in Hz for a VAD instance.
    ///
    /// Note that internally all processing will be done 8000 Hz; input data in higher
    /// sample rates will just be downsampled first.
    pub fn set_sample_rate(self, sample_rate: SampleRate) -> Self {
        let v = sample_rate as i32;
        match unsafe { ffi::fvad_set_sample_rate(self.fvad, v) } {
            0 => self,
            -1 => panic!(
                "fvad_set_sample_rate() did not accept {} as a valid sample_rate",
                v
            ),
            n => panic!("fvad_set_sample_rate() returned {}", n),
        }
    }

    /// Calculates a VAD decision for an audio frame.
    ///
    /// `frame` is a slice of signed 16-bit samples. Only slices with a
    /// length of 10, 20 or 30 ms are supported, so for example at 8 kHz, `frame.len()`
    /// must be either 80, 160 or 240.
    ///
    /// Returns:             
    /// - `Some(true)` on active voice detection
    /// - `Some(false)` on no active voice detection
    /// - `None` on invalid frame length
    pub fn is_voice_frame(&mut self, frame: &[i16]) -> Option<bool> {
        match unsafe { ffi::fvad_process(self.fvad, frame.as_ptr(), frame.len() as u64) } {
            -1 => None,
            0 => Some(false),
            1 => Some(true),
            n => panic!("fvad_process() returned {}", n),
        }
    }
}

impl From<*mut ffi::Fvad> for Fvad {
    fn from(fvad: *mut ffi::Fvad) -> Self {
        Fvad { fvad }
    }
}

impl Drop for Fvad {
    fn drop(&mut self) {
        unsafe { ffi::fvad_free(self.fvad) }
    }
}

unsafe impl Send for Fvad {}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn try_into_sample_rate() {
        assert_eq!(SampleRate::try_from(7999i16), Err(()));
        assert_eq!(SampleRate::try_from(7999i32), Err(()));
        assert_eq!(SampleRate::try_from(7999i64), Err(()));
        assert_eq!(SampleRate::try_from(7999u16), Err(()));
        assert_eq!(SampleRate::try_from(7999u32), Err(()));
        assert_eq!(SampleRate::try_from(7999u64), Err(()));
        assert_eq!(SampleRate::try_from(8000i16), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(8000i32), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(8000i64), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(8000u16), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(8000u32), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(8000u64), Ok(SampleRate::Rate8kHz));
        assert_eq!(SampleRate::try_from(16000), Ok(SampleRate::Rate16kHz));
        assert_eq!(SampleRate::try_from(32000), Ok(SampleRate::Rate32kHz));
        assert_eq!(SampleRate::try_from(48000), Ok(SampleRate::Rate48kHz));
    }

    #[test]
    fn set_mode() {
        Fvad::new()
            .unwrap()
            .set_mode(Mode::Quality)
            .set_mode(Mode::LowBitrate)
            .set_mode(Mode::Aggressive)
            .set_mode(Mode::VeryAggressive);
    }

    #[test]
    fn set_sample_rate() {
        Fvad::new()
            .unwrap()
            .set_sample_rate(SampleRate::Rate8kHz)
            .set_sample_rate(SampleRate::Rate16kHz)
            .set_sample_rate(SampleRate::Rate32kHz)
            .set_sample_rate(SampleRate::Rate48kHz);
    }

    #[test]
    fn is_voice_frame() {
        assert_eq!(
            Fvad::new()
                .unwrap()
                .is_voice_frame(&std::iter::repeat(0).take(160).collect::<Vec<i16>>()),
            Some(false)
        );
    }
}