voicepipe 0.1.0

A pipeline for live, turn-taking voice calls with a language model: VAD, endpointing, barge-in, sentence-streamed speech
Documentation
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
//! Turn-taking: per-frame speech decisions in, conversation events out.
//!
//! The `Endpointer` (the shipped `TurnDetector`) reports:
//!
//! - `SpeechStarted` after `onset_ms` of voice (a click or a cough is shorter);
//! - `Sustained` once the utterance has `sustained_ms` of voice — the bar a
//!   barge-in must clear, so a backchannel "mm" does not stop the reply;
//! - `PauseBegan` after `pause_ms` of silence — early enough to start
//!   transcribing while the turn may still be going on;
//! - `SpeechResumed` if voice returns before the turn ends;
//! - `EndOfTurn` after `end_of_turn_ms` of silence, carrying the utterance
//!   (with `pre_roll_ms` of audio from before the onset, so the first
//!   syllable is not clipped), or `Discarded` if it held less than
//!   `min_speech_ms` of voice.
//!
//! Pure — samples in, events out — so the rules are tested on synthetic
//! audio without a microphone, a socket or a speech backend.

use crate::audio::SAMPLE_RATE;
use crate::vad::Vad;

/// Turns audio into turn-taking events. The pipeline drives one per call.
pub trait TurnDetector: Send {
    /// Feed audio of any length; returns the events it caused, in order.
    fn push(&mut self, samples: &[i16]) -> Vec<EndpointEvent>;
    /// The reply started or stopped playing (the echo guard).
    fn set_playback(&mut self, playing: bool);
    /// True between `SpeechStarted` and the turn's end.
    fn in_turn(&self) -> bool;
    /// The utterance so far, for an early or partial transcription.
    fn segment(&self) -> &[i16];
    /// Forget the current utterance.
    fn reset(&mut self);
}

/// Turn-taking timings, in milliseconds of audio.
#[derive(Debug, Clone, Copy)]
pub struct EndpointConfig {
    /// Voice needed to start a turn; a click or a cough is shorter.
    pub onset_ms: u32,
    /// Voice in the utterance before `Sustained` is reported: the bar a
    /// barge-in must clear.
    pub sustained_ms: u32,
    /// Silence that counts as a pause. Keep it below `end_of_turn_ms`, or
    /// the turn ends before a pause is ever reported.
    pub pause_ms: u32,
    /// Silence that ends the turn.
    pub end_of_turn_ms: u32,
    /// Voice an utterance needs to be a turn rather than `Discarded`.
    pub min_speech_ms: u32,
    /// The longest turn; at this length it ends whatever the speaker does.
    pub max_turn_ms: u32,
    /// Audio from before the onset kept at the start of the utterance, so
    /// the first syllable is not clipped.
    pub pre_roll_ms: u32,
}

impl Default for EndpointConfig {
    fn default() -> Self {
        Self {
            onset_ms: 100,
            sustained_ms: 500,
            pause_ms: 200,
            end_of_turn_ms: 700,
            min_speech_ms: 250,
            max_turn_ms: 30_000,
            pre_roll_ms: 300,
        }
    }
}

/// What the turn detector reports. See the module docs for when.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum EndpointEvent {
    /// A turn began.
    SpeechStarted,
    /// The utterance has held `sustained_ms` of voice.
    Sustained,
    /// The speaker paused for `pause_ms`.
    PauseBegan,
    /// Voice came back before the turn ended.
    SpeechResumed,
    /// The turn is over.
    EndOfTurn {
        /// The utterance, pre-roll included.
        audio: Vec<i16>,
        /// Milliseconds of voiced audio in the utterance.
        speech_ms: u32,
    },
    /// Speech started but held too little voice to be a turn.
    Discarded,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum Phase {
    Idle,
    Speaking,
    Paused,
}

/// Frames incoming audio, asks the `Vad` about each frame, and reports the
/// turn-taking events above.
pub struct Endpointer<V: Vad> {
    config: EndpointConfig,
    vad: V,
    frame_len: usize,
    frame_ms: u32,
    /// Samples waiting to make up a whole frame.
    partial: Vec<i16>,
    /// Recent frames kept while idle, so an utterance starts `pre_roll_ms`
    /// before its onset was confirmed.
    pre_roll: std::collections::VecDeque<Vec<i16>>,
    /// Leaky count of voiced frames while idle: up on voice, down on silence.
    onset_frames: u32,
    phase: Phase,
    segment: Vec<i16>,
    voiced_frames: u32,
    silent_frames: u32,
    sustained_sent: bool,
}

impl<V: Vad> Endpointer<V> {
    /// A detector with these timings, asking `vad` about each frame.
    pub fn new(config: EndpointConfig, vad: V) -> Self {
        let frame_len = vad.frame_len().max(1);
        let frame_ms = (frame_len as u32 * 1000 / SAMPLE_RATE).max(1);
        Self {
            config,
            vad,
            frame_len,
            frame_ms,
            partial: Vec::new(),
            pre_roll: std::collections::VecDeque::new(),
            onset_frames: 0,
            phase: Phase::Idle,
            segment: Vec::new(),
            voiced_frames: 0,
            silent_frames: 0,
            sustained_sent: false,
        }
    }

    fn frames(&self, ms: u32) -> u32 {
        (ms / self.frame_ms).max(1)
    }

    /// The call's reply started or stopped playing.
    pub fn set_playback(&mut self, playing: bool) {
        self.vad.set_playback(playing);
    }

    /// True between `SpeechStarted` and the turn's end.
    pub fn in_turn(&self) -> bool {
        self.phase != Phase::Idle
    }

    /// The utterance so far, for an early or partial transcription.
    pub fn segment(&self) -> &[i16] {
        &self.segment
    }

    /// Milliseconds of voice in the current utterance.
    pub fn speech_ms(&self) -> u32 {
        self.voiced_frames * self.frame_ms
    }

    /// Forget the current utterance and any partial frame — after the call
    /// has decided what to do with it some other way.
    pub fn reset(&mut self) {
        self.partial.clear();
        self.pre_roll.clear();
        self.onset_frames = 0;
        self.phase = Phase::Idle;
        self.segment.clear();
        self.voiced_frames = 0;
        self.silent_frames = 0;
        self.sustained_sent = false;
    }

    /// Feed audio of any length; returns the events it caused, in order.
    pub fn push(&mut self, samples: &[i16]) -> Vec<EndpointEvent> {
        let mut events = Vec::new();
        self.partial.extend_from_slice(samples);
        while self.partial.len() >= self.frame_len {
            let frame: Vec<i16> = self.partial.drain(..self.frame_len).collect();
            self.frame(frame, &mut events);
        }
        events
    }

    fn frame(&mut self, frame: Vec<i16>, events: &mut Vec<EndpointEvent>) {
        let speech = self.vad.is_speech(&frame);
        match self.phase {
            Phase::Idle => {
                self.onset_frames = if speech {
                    self.onset_frames + 1
                } else {
                    self.onset_frames.saturating_sub(1)
                };
                self.pre_roll.push_back(frame);
                let keep = self.frames(self.config.pre_roll_ms) as usize;
                while self.pre_roll.len() > keep {
                    self.pre_roll.pop_front();
                }
                if self.onset_frames >= self.frames(self.config.onset_ms) {
                    self.phase = Phase::Speaking;
                    self.segment = self.pre_roll.drain(..).flatten().collect();
                    self.voiced_frames = self.onset_frames;
                    self.silent_frames = 0;
                    self.onset_frames = 0;
                    events.push(EndpointEvent::SpeechStarted);
                }
            }
            Phase::Speaking | Phase::Paused => {
                self.segment.extend_from_slice(&frame);
                if speech {
                    self.voiced_frames += 1;
                    self.silent_frames = 0;
                    if self.phase == Phase::Paused {
                        self.phase = Phase::Speaking;
                        events.push(EndpointEvent::SpeechResumed);
                    }
                } else {
                    self.silent_frames += 1;
                    if self.phase == Phase::Speaking
                        && self.silent_frames >= self.frames(self.config.pause_ms)
                    {
                        self.phase = Phase::Paused;
                        events.push(EndpointEvent::PauseBegan);
                    }
                }
                if !self.sustained_sent && self.speech_ms() >= self.config.sustained_ms {
                    self.sustained_sent = true;
                    events.push(EndpointEvent::Sustained);
                }
                let turn_ms = self.segment.len() as u32 * 1000 / SAMPLE_RATE;
                let ended = self.silent_frames >= self.frames(self.config.end_of_turn_ms)
                    || turn_ms >= self.config.max_turn_ms;
                if ended {
                    let speech_ms = self.speech_ms();
                    let audio = std::mem::take(&mut self.segment);
                    self.reset();
                    events.push(if speech_ms >= self.config.min_speech_ms {
                        EndpointEvent::EndOfTurn { audio, speech_ms }
                    } else {
                        EndpointEvent::Discarded
                    });
                }
            }
        }
    }
}

impl<V: Vad> TurnDetector for Endpointer<V> {
    fn push(&mut self, samples: &[i16]) -> Vec<EndpointEvent> {
        Endpointer::push(self, samples)
    }
    fn set_playback(&mut self, playing: bool) {
        Endpointer::set_playback(self, playing)
    }
    fn in_turn(&self) -> bool {
        Endpointer::in_turn(self)
    }
    fn segment(&self) -> &[i16] {
        Endpointer::segment(self)
    }
    fn reset(&mut self) {
        Endpointer::reset(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vad::{EnergyVad, EnergyVadConfig};

    const FRAME: usize = 320; // 20 ms at 16 kHz

    /// `ms` of a 220 Hz tone at `amplitude` — a stand-in for voice.
    fn tone(ms: u32, amplitude: f32) -> Vec<i16> {
        let n = (SAMPLE_RATE * ms / 1000) as usize;
        (0..n)
            .map(|i| {
                let t = i as f32 / SAMPLE_RATE as f32;
                (amplitude * 32767.0 * (2.0 * std::f32::consts::PI * 220.0 * t).sin()) as i16
            })
            .collect()
    }

    /// `ms` of low pseudo-random noise — a quiet room.
    fn noise(ms: u32, amplitude: i16) -> Vec<i16> {
        let n = (SAMPLE_RATE * ms / 1000) as usize;
        let mut x: u32 = 0x1234_5678;
        (0..n)
            .map(|_| {
                x ^= x << 13;
                x ^= x >> 17;
                x ^= x << 5;
                ((x % (2 * amplitude as u32 + 1)) as i32 - amplitude as i32) as i16
            })
            .collect()
    }

    fn endpointer() -> Endpointer<EnergyVad> {
        Endpointer::new(
            EndpointConfig::default(),
            EnergyVad::new(EnergyVadConfig::default()),
        )
    }

    fn feed(ep: &mut Endpointer<EnergyVad>, audio: &[i16]) -> Vec<EndpointEvent> {
        // In 20 ms pieces, as the client sends them.
        audio
            .chunks(FRAME)
            .flat_map(|chunk| ep.push(chunk))
            .collect()
    }

    fn kinds(events: &[EndpointEvent]) -> Vec<&'static str> {
        events
            .iter()
            .map(|e| match e {
                EndpointEvent::SpeechStarted => "started",
                EndpointEvent::Sustained => "sustained",
                EndpointEvent::PauseBegan => "pause",
                EndpointEvent::SpeechResumed => "resumed",
                EndpointEvent::EndOfTurn { .. } => "end",
                EndpointEvent::Discarded => "discarded",
            })
            .collect()
    }

    #[test]
    fn one_utterance_in_a_quiet_room_is_one_turn_with_its_lead_in() {
        let mut ep = endpointer();
        let mut audio = noise(500, 30);
        audio.extend(tone(800, 0.3));
        audio.extend(noise(900, 30));
        let events = feed(&mut ep, &audio);
        assert_eq!(kinds(&events), vec!["started", "sustained", "pause", "end"]);
        let EndpointEvent::EndOfTurn { audio, speech_ms } = events.last().unwrap() else {
            unreachable!()
        };
        assert!((700..=900).contains(speech_ms), "speech_ms {speech_ms}");
        // Pre-roll + speech + the silence that ended it.
        let ms = audio.len() as u32 * 1000 / SAMPLE_RATE;
        assert!(
            ms >= 800 + 700,
            "the utterance keeps its lead-in, got {ms} ms"
        );
    }

    #[test]
    fn a_short_pause_inside_a_sentence_does_not_end_the_turn() {
        let mut ep = endpointer();
        let mut audio = noise(300, 30);
        audio.extend(tone(600, 0.3));
        audio.extend(noise(400, 30)); // a breath, shorter than end_of_turn
        audio.extend(tone(600, 0.3));
        audio.extend(noise(900, 30));
        let events = feed(&mut ep, &audio);
        assert_eq!(
            kinds(&events),
            vec!["started", "sustained", "pause", "resumed", "pause", "end"]
        );
    }

    #[test]
    fn a_click_is_not_speech_and_a_short_sound_is_discarded() {
        let mut ep = endpointer();
        let mut audio = noise(300, 30);
        audio.extend(tone(40, 0.5)); // under onset_ms
        audio.extend(noise(900, 30));
        assert!(feed(&mut ep, &audio).is_empty());

        let mut audio = tone(160, 0.5); // over onset, under min_speech
        audio.extend(noise(900, 30));
        assert_eq!(
            kinds(&feed(&mut ep, &audio)),
            vec!["started", "pause", "discarded"]
        );
    }

    #[test]
    fn a_steady_noise_becomes_the_floor_and_speech_above_it_still_counts() {
        let mut ep = endpointer();
        // A loud fan: well above the default floor, but steady.
        let fan = noise(3000, 600);
        let events = feed(&mut ep, &fan);
        assert!(
            events.iter().all(|e| *e != EndpointEvent::Sustained),
            "a steady noise must not read as a sustained voice: {events:?}"
        );
        ep.reset();
        let mut audio = noise(500, 600);
        audio.extend(tone(800, 0.4));
        audio.extend(noise(900, 600));
        let kinds = kinds(&feed(&mut ep, &audio));
        assert!(kinds.contains(&"end"), "{kinds:?}");
    }

    #[test]
    fn quiet_echo_that_is_speech_while_idle_is_not_speech_while_the_reply_plays() {
        let quiet_voice = 0.009; // about -44 dBFS
        let mut idle = endpointer();
        let mut audio = noise(300, 10);
        audio.extend(tone(700, quiet_voice));
        audio.extend(noise(900, 10));
        assert!(kinds(&feed(&mut idle, &audio)).contains(&"started"));

        let mut playing = endpointer();
        playing.set_playback(true);
        assert!(feed(&mut playing, &audio).is_empty());
    }

    #[test]
    fn a_turn_that_never_pauses_is_cut_at_the_maximum() {
        let mut ep = Endpointer::new(
            EndpointConfig {
                max_turn_ms: 2_000,
                ..EndpointConfig::default()
            },
            EnergyVad::new(EnergyVadConfig::default()),
        );
        let mut audio = noise(300, 30);
        audio.extend(tone(3_000, 0.3));
        let events = feed(&mut ep, &audio);
        assert!(kinds(&events).contains(&"end"));
    }

    #[test]
    fn the_segment_so_far_is_readable_mid_turn() {
        let mut ep = endpointer();
        feed(&mut ep, &noise(200, 30));
        feed(&mut ep, &tone(400, 0.3));
        assert!(ep.in_turn());
        assert!(ep.segment().len() >= (SAMPLE_RATE as usize * 400 / 1000));
        ep.reset();
        assert!(!ep.in_turn());
        assert!(ep.segment().is_empty());
    }
}