aboutsummaryrefslogtreecommitdiffstats
path: root/crates/asist-audio/src/vad.rs
blob: 24cf7173eccf85c45c477cf846e0878d8fb06220 (plain)
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
//! Voice activity detection and splitting into utterances.
//!
//! It is written as a pure state machine: it is fed samples and returns
//! events, with no threads or channels inside. That way the behaviour that is
//! hardest to debug by ear (when a turn starts, when silence ends it, when
//! the echo of the assistant's own speaker is ignored) can be fully tested
//! with synthetic audio and no microphone.

use std::collections::VecDeque;

use asist_core::config::VadConfig;

use crate::{rms, ASR_SAMPLE_RATE};

/// A closed utterance, ready to be transcribed.
#[derive(Debug, Clone)]
pub struct Utterance {
    pub samples: Vec<f32>,
    pub sample_rate: u32,
}

impl Utterance {
    pub fn duration_secs(&self) -> f32 {
        self.samples.len() as f32 / self.sample_rate as f32
    }
}

/// What the segmenter reports to the outside.
#[derive(Debug, Clone)]
pub enum VoiceEvent {
    /// Ha empezado a hablarse.
    Started,
    /// New audio inside the current utterance, for the partial
    /// transcriptions.
    Audio(Vec<f32>),
    /// Utterance finished and long enough to be transcribed.
    Ended(Utterance),
    /// Finished but too short: a knock on the table, a cough.
    Discarded,
    /// Speech was detected while the assistant was talking, with barge-in
    /// on. The orchestrator cuts playback when it gets this.
    BargeIn,
}

/// What the segmenter does with the microphone while the speaker plays.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Gate {
    /// Nothing is playing on the speaker: listen normally.
    Open,
    /// The assistant is talking. Depending on the configuration, input is
    /// either ignored (half duplex) or needs more volume to interrupt (barge-in).
    Speaking,
}

pub struct Segmenter {
    config: VadConfig,
    frame_samples: usize,
    preroll_samples: usize,
    silence_hold_samples: usize,
    min_utterance_samples: usize,
    max_utterance_samples: usize,

    pending: Vec<f32>,
    preroll: VecDeque<f32>,
    utterance: Vec<f32>,
    /// Samples of the utterance that really were above the threshold. The
    /// minimum is measured on this and not on `utterance`, which carries the
    /// preroll: otherwise 0.1 s of a knock on the table plus 0.2 s of preroll
    /// passes for a valid utterance and triggers a whole turn.
    voiced: usize,
    noise_floor: f32,
    silence_run: usize,
    speaking: bool,
    gate: Gate,
}

impl Segmenter {
    pub fn new(config: &VadConfig) -> Self {
        let rate = ASR_SAMPLE_RATE as f32;
        Self {
            frame_samples: (rate * config.frame_seconds).max(1.0) as usize,
            preroll_samples: (rate * config.preroll_seconds) as usize,
            silence_hold_samples: (rate * config.silence_hold) as usize,
            min_utterance_samples: (rate * config.min_utterance) as usize,
            max_utterance_samples: (rate * config.max_utterance) as usize,
            config: config.clone(),
            pending: Vec::new(),
            preroll: VecDeque::new(),
            utterance: Vec::new(),
            voiced: 0,
            noise_floor: 0.0,
            silence_run: 0,
            speaking: false,
            gate: Gate::Open,
        }
    }

    /// Opens or closes the microphone depending on whether the assistant talks.
    pub fn set_gate(&mut self, gate: Gate) {
        if self.gate == gate {
            return;
        }
        self.gate = gate;
        // When reopening after an answer, what has accumulated is the tail of
        // the assistant's own speaker: starting a turn with it would be a ghost turn.
        if gate == Gate::Open {
            self.pending.clear();
            self.preroll.clear();
            self.utterance.clear();
            self.voiced = 0;
            self.silence_run = 0;
            self.speaking = false;
        }
    }

    pub fn is_speaking(&self) -> bool {
        self.speaking
    }

    pub fn noise_floor(&self) -> f32 {
        self.noise_floor
    }

    /// Threshold that separates speech from silence right now.
    pub fn threshold(&self) -> f32 {
        let base = (self.noise_floor * self.config.threshold_factor)
            .clamp(self.config.min_threshold, self.config.max_threshold);
        // With the speaker playing you must speak louder to get through: the
        // microphone is hearing itself.
        if self.gate == Gate::Speaking {
            base * self.config.barge_in_factor
        } else {
            base
        }
    }

    /// Forcibly closes the current utterance (program shutdown).
    pub fn flush(&mut self) -> Option<Utterance> {
        if !self.speaking || self.voiced < self.min_utterance_samples {
            return None;
        }
        self.speaking = false;
        self.voiced = 0;
        Some(Utterance {
            samples: std::mem::take(&mut self.utterance),
            sample_rate: ASR_SAMPLE_RATE,
        })
    }

    /// Feeds 16 kHz mono audio and collects whatever needs doing.
    pub fn push(&mut self, samples: &[f32]) -> Vec<VoiceEvent> {
        let mut events = Vec::new();
        // In half duplex the microphone is effectively off: without this, the
        // assistant transcribes itself and answers itself.
        if self.gate == Gate::Speaking && !self.config.barge_in {
            return events;
        }
        self.pending.extend_from_slice(samples);

        while self.pending.len() >= self.frame_samples {
            let frame: Vec<f32> = self.pending.drain(..self.frame_samples).collect();
            let level = rms(&frame);
            self.track_noise_floor(level);
            let threshold = self.threshold();

            if level < threshold && !self.speaking {
                self.preroll.extend(frame.iter().copied());
                while self.preroll.len() > self.preroll_samples {
                    self.preroll.pop_front();
                }
                continue;
            }

            if !self.speaking {
                self.speaking = true;
                self.utterance.clear();
                self.voiced = 0;
                self.utterance.extend(self.preroll.drain(..));
                if self.gate == Gate::Speaking {
                    events.push(VoiceEvent::BargeIn);
                }
                events.push(VoiceEvent::Started);
            }

            if level < threshold {
                self.silence_run += frame.len();
            } else {
                self.silence_run = 0;
                self.voiced += frame.len();
            }
            self.utterance.extend_from_slice(&frame);

            let ended = self.silence_run >= self.silence_hold_samples;
            let too_long = self.utterance.len() >= self.max_utterance_samples;
            if !ended && !too_long {
                events.push(VoiceEvent::Audio(frame));
                continue;
            }

            let long_enough = self.voiced >= self.min_utterance_samples;
            events.push(if long_enough {
                VoiceEvent::Ended(Utterance {
                    samples: std::mem::take(&mut self.utterance),
                    sample_rate: ASR_SAMPLE_RATE,
                })
            } else {
                VoiceEvent::Discarded
            });

            self.utterance.clear();
            self.voiced = 0;
            self.preroll.clear();
            self.silence_run = 0;
            // A length cut lands mid-sentence: keep listening as if the user had
            // not stopped talking, which is the truth.
            self.speaking = too_long && !ended;
            if self.speaking {
                events.push(VoiceEvent::Started);
            }
        }
        events
    }

    /// The noise floor only goes down: sustained speech must not be able to
    /// drag the threshold above itself and stop being detected.
    fn track_noise_floor(&mut self, level: f32) {
        if self.noise_floor == 0.0 {
            self.noise_floor = level;
        } else if level < self.noise_floor * 1.5 {
            self.noise_floor = self.noise_floor * 0.95 + level * 0.05;
        }
    }
}

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

    fn config() -> VadConfig {
        VadConfig {
            frame_seconds: 0.1,
            preroll_seconds: 0.2,
            silence_hold: 0.3,
            min_utterance: 0.3,
            max_utterance: 2.0,
            threshold_factor: 3.0,
            min_threshold: 0.001,
            max_threshold: 0.02,
            barge_in: false,
            barge_in_factor: 4.0,
        }
    }

    fn samples(secs: f32, amplitude: f32) -> Vec<f32> {
        let n = (ASR_SAMPLE_RATE as f32 * secs) as usize;
        // Alternates sign so the RMS is the amplitude and not a DC level.
        (0..n)
            .map(|i| if i % 2 == 0 { amplitude } else { -amplitude })