rustvani 0.1.4

Voice AI framework for Rust — real-time speech pipelines with STT, LLM, TTS, and Dhara conversation flows
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
//! VAD processor.
//!
//! `VadProcessor` is a [`FrameHandler`] that:
//! 1. Receives `InputAudioRaw` frames from the transport or directly from the pipeline.
//! 2. Accumulates PCM until a full Silero inference window is ready.
//! 3. Runs inference via the configured `VadAnalyzer` backend.
//! 4. Advances the state machine with the result.
//! 5. Emits `VADUserStartedSpeaking` / `VADUserStoppedSpeaking` on transitions.
//! 6. Optionally integrates with `SmartTurnAnalyzer` for ML-based end-of-turn detection.
//!
//! Supports both `SileroVadNative` (pure Rust) and `SileroVadOrt` (ONNX Runtime)
//! via the `VadAnalyzer` trait.
//!
//! # Usage without a transport
//!
//! ```ignore
//! let vad = VadProcessor::new(16_000, VadParams::default(), VadBackend::Native)?
//!     .into_processor();
//!
//! let task = PipelineTask::new(vec![vad, stt, llm, tts], PipelineParams::default());
//!
//! // Push raw audio directly:
//! let audio = Frame::input_audio_raw(AudioRawData::new(bytes, 16_000, 1));
//! task.push_frame(audio, FrameDirection::Downstream).await?;
//!
//! // Consume VAD events:
//! let mut filter = HashSet::new();
//! filter.insert(FrameKind::VADUserStartedSpeaking);
//! filter.insert(FrameKind::VADUserStoppedSpeaking);
//! task.set_downstream_filter(filter);
//! task.add_on_frame_reached_downstream(|frame| Box::pin(async move {
//!     println!("VAD event: {:?}", frame.kind());
//! }));
//! ```

use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use log;

use crate::error::Result;
use crate::frames::{
    ControlFrame, Frame, FrameDirection, FrameHandler, FrameInner, FrameProcessor, SystemFrame,
};
use crate::turn::{EndOfTurnState, SmartTurnAnalyzer, SmartTurnConfig};

use super::analyzer::VadAnalyzer;
use super::params::VadParams;
use super::state::{StateMachine, VadState};
use super::{VadBackend, create_vad};

// ---------------------------------------------------------------------------
// VadProcessorState
// ---------------------------------------------------------------------------

struct VadProcessorState {
    machine: StateMachine,
    model: Arc<dyn VadAnalyzer>,
    params: VadParams,
    sample_rate: u32,
    last_is_speech: bool,
    emitted_speaking: bool,
    user_speaking: bool,
    bot_speaking: bool,
    turn_analyzer: Option<SmartTurnAnalyzer>,
}

// ---------------------------------------------------------------------------
// VadProcessor
// ---------------------------------------------------------------------------

/// VAD processor — place between transport input and STT in the pipeline,
/// or use without a transport by pushing `InputAudioRaw` frames directly.
pub struct VadProcessor {
    state: Arc<Mutex<VadProcessorState>>,
}

impl VadProcessor {
    /// Create a new VAD processor with the specified backend.
    ///
    /// `sample_rate` must be 8000 or 16000.
    /// `backend` selects Native (pure Rust) or Ort (ONNX Runtime).
    pub fn new(
        sample_rate: u32,
        params: VadParams,
        backend: VadBackend,
    ) -> Result<Self> {
        let model = create_vad(backend, sample_rate)
            .map_err(|e| crate::error::PipecatError::pipeline(e))?;

        let machine = StateMachine::new(sample_rate, params.clone());

        Ok(Self {
            state: Arc::new(Mutex::new(VadProcessorState {
                machine,
                model,
                params,
                sample_rate,
                last_is_speech: false,
                emitted_speaking: false,
                user_speaking: false,
                bot_speaking: false,
                turn_analyzer: None,
            })),
        })
    }

    /// Create with default backend (Native).
    pub fn with_defaults(sample_rate: u32, params: VadParams) -> Result<Self> {
        Self::new(sample_rate, params, VadBackend::default())
    }

    /// Enable ML-based smart end-of-turn detection.
    ///
    /// Requires the VAD analyzer to already be configured (i.e. this processor
    /// was created with a valid backend).  
    /// If `turn_config` is `None` the call is a no-op.
    pub fn with_smart_turn(
        self,
        turn_config: Option<&SmartTurnConfig>,
    ) -> Result<Self> {
        let Some(config) = turn_config else {
            return Ok(self);
        };

        let sample_rate = self.state.lock().unwrap().sample_rate;
        let vad_start_secs = self.state.lock().unwrap().params.start_secs as f64;

        let mut analyzer = SmartTurnAnalyzer::new(config)
            .map_err(|e| crate::error::PipecatError::pipeline(format!("smart turn: {}", e)))?;

        analyzer.set_sample_rate(sample_rate);
        analyzer.update_vad_start_secs(vad_start_secs);

        self.state.lock().unwrap().turn_analyzer = Some(analyzer);

        log::info!("VadProcessor: smart turn analyzer initialized");
        Ok(self)
    }

    /// Build a `FrameProcessor` wrapping this handler.
    pub fn into_processor(self) -> FrameProcessor {
        FrameProcessor::new("VadProcessor", Box::new(self), false)
    }

    fn reset_state(&self) {
        let mut guard = self.state.lock().unwrap();
        guard.emitted_speaking = false;
        guard.user_speaking = false;
        guard.bot_speaking = false;
        guard.last_is_speech = false;
        guard.machine = StateMachine::new(guard.sample_rate, guard.params.clone());
        if let Some(ref mut ta) = guard.turn_analyzer {
            ta.clear();
        }
        log::debug!("VadProcessor: state reset");
    }
}

// ---------------------------------------------------------------------------
// FrameHandler impl
// ---------------------------------------------------------------------------

#[async_trait]
impl FrameHandler for VadProcessor {
    async fn on_process_frame(
        &self,
        processor: &FrameProcessor,
        frame: Frame,
        direction: FrameDirection,
    ) -> Result<()> {
        match &frame.inner {
            // -----------------------------------------------------------------
            // Lifecycle
            // -----------------------------------------------------------------
            FrameInner::System(SystemFrame::Start(_)) => {
                processor.push_frame(frame, direction).await?;
                self.reset_state();
            }
            FrameInner::System(SystemFrame::Stop { .. }) => {
                processor.push_frame(frame, direction).await?;
            }
            FrameInner::Control(ControlFrame::End { .. }) => {
                processor.push_frame(frame, direction).await?;
                self.reset_state();
            }
            FrameInner::System(SystemFrame::Cancel { .. }) => {
                processor.push_frame(frame, direction).await?;
                self.reset_state();
            }

            // -----------------------------------------------------------------
            // Speaking state tracking (mirrors BaseInputTransport behaviour)
            // -----------------------------------------------------------------
            FrameInner::System(SystemFrame::BotStartedSpeaking) => {
                self.state.lock().unwrap().bot_speaking = true;
                processor.push_frame(frame, direction).await?;
            }
            FrameInner::System(SystemFrame::BotStoppedSpeaking) => {
                self.state.lock().unwrap().bot_speaking = false;
                processor.push_frame(frame, direction).await?;
            }
            FrameInner::System(SystemFrame::VADUserStartedSpeaking { .. }) => {
                self.state.lock().unwrap().user_speaking = true;
                processor.push_frame(frame, direction).await?;
            }
            FrameInner::System(SystemFrame::VADUserStoppedSpeaking { .. }) => {
                self.state.lock().unwrap().user_speaking = false;
                processor.push_frame(frame, direction).await?;
            }

            // -----------------------------------------------------------------
            // Core VAD + SmartTurn
            // -----------------------------------------------------------------
            FrameInner::System(SystemFrame::InputAudioRaw(ref audio_data)) => {
                if direction == FrameDirection::Downstream {
                    // Always pass audio through so STT still gets it.
                    processor.push_frame(frame.clone(), direction).await?;

                    // --- VAD window processing ---
                    let window_opt = {
                        let mut guard = self.state.lock().unwrap();
                        guard.machine.next_window(&audio_data.audio)
                    };

                    let mut vad_quiet_transition = false;

                    if let Some(window) = window_opt {
                        let model = {
                            self.state.lock().unwrap().model.clone()
                        };

                        let confidence = model.voice_confidence(window.clone()).await;

                        let (prev_state, new_state) = {
                            let mut guard = self.state.lock().unwrap();
                            guard.last_is_speech = confidence >= guard.params.confidence;
                            let prev = guard.machine.state;
                            let next = guard.machine.advance(confidence, &window);
                            (prev, next)
                        };

                        let ts = SystemTime::now()
                            .duration_since(UNIX_EPOCH)
                            .unwrap_or_default()
                            .as_secs_f64();

                        match (prev_state, new_state) {
                            // Quiet/Starting → Speaking: speech confirmed
                            (s, VadState::Speaking) if s != VadState::Speaking => {
                                let maybe_start = {
                                    let mut guard = self.state.lock().unwrap();
                                    if !guard.emitted_speaking {
                                        guard.emitted_speaking = true;
                                        guard.user_speaking = true;
                                        Some(guard.params.start_secs)
                                    } else {
                                        None
                                    }
                                };

                                if let Some(start_secs) = maybe_start {
                                    log::info!(
                                        "VAD: → Speaking (confidence={:.3})",
                                        confidence
                                    );
                                    let vad_frame =
                                        Frame::vad_user_started_speaking(start_secs, ts);
                                    processor
                                        .push_frame(vad_frame, FrameDirection::Downstream)
                                        .await?;
                                }
                            }

                            // Speaking/Stopping → Quiet: silence confirmed
                            (s, VadState::Quiet) if s != VadState::Quiet => {
                                let maybe_stop = {
                                    let mut guard = self.state.lock().unwrap();
                                    if guard.emitted_speaking {
                                        if guard.turn_analyzer.is_some() {
                                            log::info!(
                                                "VAD: → Quiet (confidence={:.3}), \
                                                 deferring to SmartTurn",
                                                confidence
                                            );
                                            vad_quiet_transition = true;
                                            None
                                        } else {
                                            guard.emitted_speaking = false;
                                            guard.user_speaking = false;
                                            Some(guard.params.stop_secs)
                                        }
                                    } else {
                                        None
                                    }
                                };

                                if let Some(stop_secs) = maybe_stop {
                                    log::info!(
                                        "VAD: → Quiet (confidence={:.3})",
                                        confidence
                                    );
                                    let vad_frame =
                                        Frame::vad_user_stopped_speaking(stop_secs, ts);
                                    processor
                                        .push_frame(vad_frame, FrameDirection::Downstream)
                                        .await?;
                                }
                            }

                            _ => {}
                        }
                    }

                    // --- Smart turn processing ---
                    let (last_is_speech, has_smart_turn) = {
                        let guard = self.state.lock().unwrap();
                        (guard.last_is_speech, guard.turn_analyzer.is_some())
                    };

                    if has_smart_turn {
                        let turn_state = {
                            let mut guard = self.state.lock().unwrap();
                            if let Some(ref mut ta) = guard.turn_analyzer {
                                ta.append_audio(&audio_data.audio, last_is_speech)
                            } else {
                                EndOfTurnState::Incomplete
                            }
                        };

                        if turn_state == EndOfTurnState::Complete {
                            let maybe_stop = {
                                let mut guard = self.state.lock().unwrap();
                                if guard.emitted_speaking {
                                    guard.emitted_speaking = false;
                                    guard.user_speaking = false;
                                    Some(guard.params.stop_secs)
                                } else {
                                    None
                                }
                            };

                            if let Some(stop_secs) = maybe_stop {
                                log::info!("SmartTurn: → Complete (silence timeout)");
                                let ts = SystemTime::now()
                                    .duration_since(UNIX_EPOCH)
                                    .unwrap_or_default()
                                    .as_secs_f64();
                                let vad_frame = Frame::vad_user_stopped_speaking(stop_secs, ts);
                                processor
                                    .push_frame(vad_frame, FrameDirection::Downstream)
                                    .await?;
                            }
                        }

                        if vad_quiet_transition {
                            let (result, metrics) = {
                                let mut guard = self.state.lock().unwrap();
                                if let Some(ref mut ta) = guard.turn_analyzer {
                                    ta.analyze_end_of_turn()
                                } else {
                                    (EndOfTurnState::Incomplete, None)
                                }
                            };

                            if let Some(ref m) = metrics {
                                log::info!(
                                    "SmartTurn: prob={:.3} complete={} time={:.1}ms",
                                    m.probability,
                                    m.is_complete,
                                    m.e2e_processing_time_ms
                                );
                            }

                            if result == EndOfTurnState::Complete {
                                let maybe_stop = {
                                    let mut guard = self.state.lock().unwrap();
                                    if guard.emitted_speaking {
                                        guard.emitted_speaking = false;
                                        guard.user_speaking = false;
                                        Some(guard.params.stop_secs)
                                    } else {
                                        None
                                    }
                                };

                                if let Some(stop_secs) = maybe_stop {
                                    log::info!("SmartTurn: → Complete (ML prediction)");
                                    let ts = SystemTime::now()
                                        .duration_since(UNIX_EPOCH)
                                        .unwrap_or_default()
                                        .as_secs_f64();
                                    let vad_frame =
                                        Frame::vad_user_stopped_speaking(stop_secs, ts);
                                    processor
                                        .push_frame(vad_frame, FrameDirection::Downstream)
                                        .await?;
                                }
                            } else {
                                log::info!("SmartTurn: → Incomplete, waiting for more audio");
                            }
                        }
                    }

                    return Ok(());
                }

                // Upstream audio: just pass through.
                processor.push_frame(frame, direction).await?;
            }

            // -----------------------------------------------------------------
            // Default pass-through
            // -----------------------------------------------------------------
            _ => {
                processor.push_frame(frame, direction).await?;
            }
        }

        Ok(())
    }
}