rustpotter/
detector.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use std::collections::HashMap;

use crate::{
    audio::{AudioEncoder, BandPassFilter, GainNormalizerFilter},
    constants::{
        DETECTOR_INTERNAL_SAMPLE_RATE, MFCCS_EXTRACTOR_FRAME_LENGTH_MS,
        MFCCS_EXTRACTOR_FRAME_SHIFT_MS, MFCCS_EXTRACTOR_PRE_EMPHASIS,
    },
    mfcc::{MfccExtractor, VadDetector},
    wakewords::{WakewordDetector, WakewordFile, WakewordV2},
    DetectorConfig, FiltersConfig, RustpotterConfig, Sample, ScoreMode, WakewordLoad,
    WakewordModel, WakewordRef,
};
/// Rustpotter is an open source wakeword spotter forged in rust
/// ```
/// use rustpotter::{Rustpotter, RustpotterConfig};
/// // assuming the audio input format match the rustpotter defaults
/// let mut rustpotter_config = RustpotterConfig::default();
/// // Configure the rustpotter
/// // ...
/// let mut rustpotter = Rustpotter::new(&rustpotter_config).unwrap();
/// // load and enable a wakeword
/// rustpotter.add_wakeword_from_file("wakeword_key", "./tests/resources/oye_casa_g.rpw").unwrap();
/// // You need a buffer of size rustpotter.get_samples_per_frame() when using samples or rustpotter.get_bytes_per_frame() when using bytes.  
/// let mut sample_buffer: Vec<i16> = vec![0; rustpotter.get_samples_per_frame()];
/// // while true { Iterate forever
///     // fill the buffer with the required samples/bytes...
///    let detection = rustpotter.process_samples(sample_buffer);
///     if let Some(detection) = detection {
///         println!("{:?}", detection);
///     }
/// // }
/// ```
pub struct Rustpotter {
    // Config
    /// Required score against the averaged mfccs matrix. Only to discard frames.
    avg_threshold: f32,
    /// Required score while comparing the wakeword against the live stream.
    threshold: f32,
    /// Required number of partial scores (scores over threshold) to consider the detection real.
    min_scores: usize,
    /// Emit detection on min partial scores.
    eager: bool,
    /// How to calculate the final score.
    score_mode: ScoreMode,
    ///
    vad_detector: Option<VadDetector>,
    // Utils
    /// Utility to encode or re-encode the input wav data.
    wav_encoder: AudioEncoder,
    /// Utility to extract a collection of mfcc for each input audio frame.
    mfcc_extractor: MfccExtractor,
    /// Score reference for it to be expressed in a 0 - 1 range.
    score_ref: f32,
    /// Comparator band size.
    band_size: u16,
    /// Optional band-pass filter implementation.
    band_pass_filter: Option<BandPassFilter>,
    /// Optional gain filter implementation.
    gain_normalizer_filter: Option<GainNormalizerFilter>,
    // State
    /// The collection of active wakewords detectors.
    wakewords: HashMap<String, Box<dyn WakewordDetector>>,
    /// Indicates that the audio_mfcc_window has enough mfcc frames to run the detection.
    /// This means its length is greater or equal to the max_mfcc_frames value.
    buffering: bool,
    /// A window of mfccs frames extracted from the input audio.
    /// It grows until match the max_mfcc_frames value.
    audio_mfcc_window: Vec<Vec<f32>>,
    /// Max number of feature frames on the wakewords.
    max_mfcc_frames: usize,
    /// Stores the partial detection with greater score while waiting for the detection countdown to be zero.
    partial_detection: Option<RustpotterDetection>,
    /// Countdown until detection fires.
    /// Whenever a better partial detection is found, it is set to the double of the max number of feature frames in the wakeword samples.
    /// When it gets to zero and a partial detection exists, it'll be considered the final detection.
    detection_countdown: usize,
    /// Current frame rms level
    rms_level: f32,
    /// Gain normalization applied to current frame.
    gain: f32,
    #[cfg(feature = "record")]
    // Path to create records
    record_path: Option<String>,
    #[cfg(feature = "record")]
    /// Audio data cache for recording
    audio_window: Vec<f32>,
    #[cfg(feature = "record")]
    /// Max audio data to retain
    max_audio_samples: usize,
}

impl Rustpotter {
    /// Returns a configured Rustpotter instance.
    pub fn new(config: &RustpotterConfig) -> Result<Rustpotter, String> {
        let reencoder = AudioEncoder::new(
            &config.fmt,
            MFCCS_EXTRACTOR_FRAME_LENGTH_MS,
            DETECTOR_INTERNAL_SAMPLE_RATE,
        )?;
        let samples_per_frame = reencoder.get_output_frame_length();
        let samples_per_shift = (samples_per_frame as f32
            / (MFCCS_EXTRACTOR_FRAME_LENGTH_MS as f32 / MFCCS_EXTRACTOR_FRAME_SHIFT_MS as f32))
            as usize;
        let mfcc_extractor = MfccExtractor::new(
            DETECTOR_INTERNAL_SAMPLE_RATE,
            samples_per_frame,
            samples_per_shift,
            0, // setup on wakeword added
            MFCCS_EXTRACTOR_PRE_EMPHASIS,
        );
        let gain_normalizer_filter = (&config.filters.gain_normalizer).into();
        let band_pass_filter = (&config.filters.band_pass).into();
        Ok(Rustpotter {
            avg_threshold: config.detector.avg_threshold,
            threshold: config.detector.threshold,
            min_scores: config.detector.min_scores,
            eager: config.detector.eager,
            score_mode: config.detector.score_mode,
            score_ref: config.detector.score_ref,
            band_size: config.detector.band_size,
            wav_encoder: reencoder,
            vad_detector: config.detector.vad_mode.map(VadDetector::new),
            mfcc_extractor,
            band_pass_filter,
            gain_normalizer_filter,
            audio_mfcc_window: Vec::new(),
            buffering: true,
            max_mfcc_frames: 0,
            wakewords: HashMap::new(),
            partial_detection: None,
            detection_countdown: 0,
            rms_level: 0.,
            gain: 1.,
            #[cfg(feature = "record")]
            max_audio_samples: 0,
            #[cfg(feature = "record")]
            audio_window: Vec::new(),
            #[cfg(feature = "record")]
            record_path: config.detector.record_path.clone(),
        })
    }
    /// Add wakeword ref to the detector.
    pub fn add_wakeword_ref(&mut self, key: &str, wakeword: WakewordRef) -> Result<(), String> {
        self.add_wakeword(key, wakeword)
    }
    /// Add wakeword model to the detector.
    pub fn add_wakeword_model(&mut self, key: &str, wakeword: WakewordModel) -> Result<(), String> {
        self.add_wakeword(key, wakeword)
    }
    /// Add wakeword from file bytes.
    pub fn add_wakeword_from_buffer(&mut self, key: &str, buffer: &[u8]) -> Result<(), String> {
        WakewordV2::load_from_buffer(buffer)
            .and_then(|w| self.add_wakeword_ref(key, w.into()))
            .or_else(|_| {
                WakewordRef::load_from_buffer(buffer)
                    .and_then(|wakeword| self.add_wakeword_ref(key, wakeword))
                    .or_else(|_| {
                        WakewordModel::load_from_buffer(buffer)
                            .and_then(|wakeword| self.add_wakeword_model(key, wakeword))
                    })
            })
    }
    /// Add wakeword from file path.
    pub fn add_wakeword_from_file(&mut self, key: &str, path: &str) -> Result<(), String> {
        WakewordV2::load_from_file(path)
            .and_then(|w| self.add_wakeword_ref(key, w.into()))
            .or_else(|_| {
                WakewordRef::load_from_file(path)
                    .and_then(|wakeword| self.add_wakeword_ref(key, wakeword))
                    .or_else(|_| {
                        WakewordModel::load_from_file(path)
                            .and_then(|wakeword| self.add_wakeword_model(key, wakeword))
                    })
            })
    }
    /// Remove wakeword by key.
    ///
    /// Returns true on success.
    pub fn remove_wakeword(&mut self, key: &str) -> bool {
        let len = self.wakewords.len();
        self.wakewords.retain(|k, _| !k.eq(key));
        if len != self.wakewords.len() {
            self.on_wakeword_change();
            true
        } else {
            false
        }
    }
    /// Remove all wakewords.
    ///
    /// Returns true on success.
    pub fn remove_wakewords(&mut self) -> bool {
        let len = self.wakewords.len();
        self.wakewords.clear();
        if len != self.wakewords.len() {
            self.on_wakeword_change();
            true
        } else {
            false
        }
    }
    /// Returns the number of audio samples needed by the detector.
    pub fn get_samples_per_frame(&self) -> usize {
        self.wav_encoder.get_input_frame_length()
    }
    /// Returns the number of audio bytes needed by the detector.
    pub fn get_bytes_per_frame(&self) -> usize {
        self.wav_encoder.get_input_byte_length()
    }
    /// Returns a reference to the current partial detection if any.
    pub fn get_partial_detection(&self) -> Option<&RustpotterDetection> {
        self.partial_detection.as_ref()
    }
    /// Returns the rms level of the last frame (before gain normalization)
    pub fn get_rms_level(&self) -> f32 {
        self.rms_level
    }
    /// Returns the gain applied to the latest frame by the gain normalizer filter (1. if none or disabled).
    pub fn get_gain(&self) -> f32 {
        self.gain
    }
    /// Returns the gain normalizer filter rms level reference.
    pub fn get_rms_level_ref(&self) -> f32 {
        self.gain_normalizer_filter
            .as_ref()
            .map(|f| f.get_rms_level_ref())
            .unwrap_or(f32::NAN)
    }
    /// Process bytes buffer.
    ///
    /// Number of bytes provided should match the return of the get_bytes_per_frame method.
    ///
    pub fn process_bytes(&mut self, audio_bytes: &[u8]) -> Option<RustpotterDetection> {
        if audio_bytes.len() != self.get_bytes_per_frame() {
            return None;
        }
        let encoded_samples = self.wav_encoder.encode_and_resample(audio_bytes);
        self.process_audio(encoded_samples)
    }
    /// Process encoded audio samples.
    ///
    /// Number of samples provided should match the return of the get_samples_per_frame method.
    ///
    pub fn process_samples<T: Sample>(
        &mut self,
        audio_samples: Vec<T>,
    ) -> Option<RustpotterDetection> {
        if audio_samples.len() != self.get_samples_per_frame() {
            return None;
        }
        let float_samples = self.wav_encoder.rencode_and_resample::<T>(audio_samples);
        self.process_audio(float_samples)
    }
    /// Updates detector and audio filters configs.
    ///
    pub fn update_config(&mut self, config: &RustpotterConfig) {
        self.update_detector_config(&config.detector);
        self.update_filters_config(&config.filters);
    }
    /// Updates detector config.
    ///
    pub fn update_detector_config(&mut self, config: &DetectorConfig) {
        self.avg_threshold = config.avg_threshold;
        self.threshold = config.threshold;
        self.min_scores = config.min_scores;
        self.eager = config.eager;
        self.band_size = config.band_size;
        self.score_ref = config.score_ref;
        self.score_mode = config.score_mode;
        self.vad_detector = config.vad_mode.map(VadDetector::new);
        #[cfg(feature = "record")]
        {
            self.record_path = config.record_path.clone();
        }
        for wd in self.wakewords.values_mut() {
            wd.update_config(self.score_ref, self.band_size, self.score_mode);
        }
        self.reset();
    }
    /// Updates audio filters config.
    ///
    pub fn update_filters_config(&mut self, config: &FiltersConfig) {
        self.band_pass_filter = (&config.band_pass).into();
        self.gain_normalizer_filter = (&config.gain_normalizer).into();
        self.reset();
    }
    /// Clean internal State
    ///
    pub fn reset(&mut self) {
        self.buffering = true;
        self.partial_detection = None;
        self.audio_mfcc_window.clear();
        self.mfcc_extractor.reset();
        if let Some(vad) = self.vad_detector.as_mut() {
            vad.reset();
        }
        #[cfg(feature = "record")]
        {
            self.audio_window.clear();
        }
    }

    fn add_wakeword<T: WakewordFile>(&mut self, name: &str, wakeword: T) -> Result<(), String> {
        if self.wakewords.is_empty() {
            self.reset();
            self.mfcc_extractor.set_out_size(wakeword.get_mfcc_size());
        } else if !self
            .wakewords
            .values()
            .next()
            .unwrap()
            .get_mfcc_size()
            .eq(&wakeword.get_mfcc_size())
        {
            return Err(
                "Usage of wakewords with different mfcc size is not supported, ignoring wakeword"
                    .to_string(),
            );
        }
        self.wakewords.insert(
            name.to_string(),
            wakeword.get_detector(self.score_ref, self.band_size, self.score_mode),
        );
        self.on_wakeword_change();
        Ok(())
    }
    fn on_wakeword_change(&mut self) {
        let mut max_mfcc_frames = usize::MIN;
        let mut target_rms_level = f32::NAN;
        for wakeword in self.wakewords.values() {
            max_mfcc_frames = wakeword.as_ref().get_mfcc_frame_size().max(max_mfcc_frames);
            target_rms_level = wakeword.get_rms_level().max(target_rms_level);
        }
        self.max_mfcc_frames = max_mfcc_frames;
        if let Some(gain_normalizer_filter) = self.gain_normalizer_filter.as_mut() {
            gain_normalizer_filter.set_rms_level_ref(target_rms_level, self.max_mfcc_frames / 3);
        }
        self.buffering = self.audio_mfcc_window.len() < self.max_mfcc_frames;
        #[cfg(feature = "record")]
        {
            self.max_audio_samples = (self.max_mfcc_frames
                / crate::constants::MFCCS_EXTRACTOR_OUT_SHIFTS)
                * self.wav_encoder.get_output_frame_length();
        }
    }
    fn process_audio(&mut self, mut audio_buffer: Vec<f32>) -> Option<RustpotterDetection> {
        if self.wakewords.is_empty() {
            return None;
        }
        #[cfg(feature = "record")]
        if self.record_path.is_some() {
            self.audio_window.append(&mut (audio_buffer.clone()));
            if self.audio_window.len() > self.max_audio_samples {
                self.audio_window.drain(0..audio_buffer.len());
            }
        }
        self.rms_level = GainNormalizerFilter::get_rms_level(&audio_buffer);
        if self.gain_normalizer_filter.is_some() {
            self.gain = self
                .gain_normalizer_filter
                .as_mut()
                .unwrap()
                .filter(&mut audio_buffer, self.rms_level);
        }
        if self.band_pass_filter.is_some() {
            self.band_pass_filter
                .as_mut()
                .unwrap()
                .filter(&mut audio_buffer);
        }
        self.mfcc_extractor
            .compute(&audio_buffer)
            .into_iter()
            .find_map(|mfccs| self.process_new_mfccs(mfccs))
    }
    fn process_new_mfccs(&mut self, mfcc_frame: Vec<f32>) -> Option<RustpotterDetection> {
        let mut result: Option<RustpotterDetection> = None;
        let should_run = self.partial_detection.is_some()
            || self
                .vad_detector
                .as_mut()
                .map_or(true, |v| v.is_voice(&mfcc_frame));
        self.audio_mfcc_window.push(mfcc_frame);
        if self.audio_mfcc_window.len() >= self.max_mfcc_frames {
            if self.buffering {
                self.buffering = false;
            }
            if should_run {
                result = self.run_detection();
            }
        }
        if self.audio_mfcc_window.len() >= self.max_mfcc_frames {
            self.audio_mfcc_window.drain(0..1);
        }
        result
    }
    fn run_detection(&mut self) -> Option<RustpotterDetection> {
        if self.detection_countdown != 0 {
            self.detection_countdown -= 1;
        }
        if self.partial_detection.is_some()
            && self.is_detection_done(self.partial_detection.as_ref().unwrap())
        {
            let wakeword_detection = self.partial_detection.take().unwrap();
            if wakeword_detection.counter >= self.min_scores {
                self.reset();
                return Some(wakeword_detection);
            }
        }
        let wakeword_detection = self.run_wakeword_detectors().map(|mut detection| {
            detection.counter = self.partial_detection.as_ref().map_or(1, |d| d.counter + 1);
            detection.gain = self.gain;
            detection
        });
        if let Some(wakeword_detection) = wakeword_detection {
            if self.partial_detection.is_none()
                || self.partial_detection.as_ref().unwrap().score < wakeword_detection.score
            {
                #[cfg(feature = "record")]
                if let Some(record_path) = self.record_path.as_ref() {
                    self.create_audio_record(record_path, &wakeword_detection);
                }
                self.partial_detection = Some(wakeword_detection);
            } else {
                let partial_detection = self.partial_detection.as_mut().unwrap();
                partial_detection.counter = wakeword_detection.counter;
            }
            self.detection_countdown = self.max_mfcc_frames / 2;
        }
        None
    }
    fn run_wakeword_detectors(&mut self) -> Option<RustpotterDetection> {
        let mut wakeword_detections = self
            .wakewords
            .values()
            .filter_map(|wakeword| {
                wakeword.run_detection(
                    self.audio_mfcc_window.clone(),
                    self.avg_threshold,
                    self.threshold,
                )
            })
            .collect::<Vec<RustpotterDetection>>();
        wakeword_detections.sort_by(|a, b| b.score.total_cmp(&a.score));
        wakeword_detections.into_iter().next()
    }
    fn is_detection_done(&self, detection: &RustpotterDetection) -> bool {
        if self.detection_countdown == 0 {
            true
        } else {
            self.eager && detection.counter >= self.min_scores
        }
    }
    #[cfg(feature = "record")]
    fn create_audio_record(&self, record_path: &str, detection: &RustpotterDetection) {
        let spec = hound::WavSpec {
            sample_rate: DETECTOR_INTERNAL_SAMPLE_RATE as u32,
            sample_format: hound::SampleFormat::Float,
            bits_per_sample: 32,
            channels: 1,
        };
        let timestamp = std::time::UNIX_EPOCH.elapsed().unwrap().as_millis();
        let record_folder = std::path::Path::new(record_path);
        if !record_folder.exists() {
            return;
        }
        let file_path = record_folder.join(
            "[".to_string()
                + &detection.name
                + "]"
                + timestamp.to_string().as_str()
                + "-"
                + &detection.score.to_string().replace(".", "_")
                + ".wav",
        );
        let writer = hound::WavWriter::create(file_path.as_os_str(), spec);
        if let Ok(mut writer) = writer {
            self.audio_window
                .iter()
                .for_each(|sample| _ = writer.write_sample(*sample));
            _ = writer.flush();
        }
    }
}
/// Encapsulates the detection information.
#[cfg_attr(feature = "debug", derive(Debug))]
pub struct RustpotterDetection {
    /// Detected wakeword name.
    pub name: String,
    /// Detection score against the averaged template.
    pub avg_score: f32,
    /// Detection score.
    pub score: f32,
    /// Detection scores against each template.
    pub scores: HashMap<String, f32>,
    /// Partial detections counter.
    pub counter: usize,
    /// Gain applied to the scored frame
    pub gain: f32,
}