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
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tracing::info;
use super::WhisperSTT;
// Speech processing state machine
#[derive(Debug, Clone, PartialEq)]
pub enum ConversationState {
Idle,
UserSpeaking,
UserPausedBriefly, // Short pause, might continue
UserFinished, // Long pause, likely done
AssistantSpeaking,
AssistantInterrupted,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpeechSegment {
pub start_time: f32,
pub end_time: f32,
pub text: String,
pub is_final: bool,
pub confidence: f32,
}
#[derive(Debug, Clone)]
pub struct ConversationTurn {
pub speaker: String, // "user" or "assistant"
pub segments: Vec<SpeechSegment>,
pub started_at: Instant,
pub ended_at: Option<Instant>,
pub was_interrupted: bool,
}
pub struct SpeechProcessor {
state: Arc<Mutex<ConversationState>>,
current_turn: Arc<Mutex<Option<ConversationTurn>>>,
conversation_history: Arc<Mutex<Vec<ConversationTurn>>>,
// Timing parameters (in milliseconds)
brief_pause_threshold: u64, // 500ms - might continue speaking
turn_end_threshold: u64, // 1500ms - probably done speaking
interruption_threshold: u64, // 200ms - minimum overlap to count as interruption
// Audio buffer for partial recognition
audio_buffer: Arc<Mutex<Vec<f32>>>,
last_speech_time: Arc<Mutex<Instant>>,
// Whisper for speech-to-text
whisper_stt: Option<Arc<WhisperSTT>>,
sample_rate: u32,
}
impl SpeechProcessor {
pub fn new() -> Self {
Self {
state: Arc::new(Mutex::new(ConversationState::Idle)),
current_turn: Arc::new(Mutex::new(None)),
conversation_history: Arc::new(Mutex::new(Vec::new())),
brief_pause_threshold: 500,
turn_end_threshold: 1500,
interruption_threshold: 200,
audio_buffer: Arc::new(Mutex::new(Vec::new())),
last_speech_time: Arc::new(Mutex::new(Instant::now())),
whisper_stt: None,
sample_rate: 16000,
}
}
pub fn set_whisper(&mut self, whisper: Arc<WhisperSTT>) {
self.whisper_stt = Some(whisper);
}
pub fn set_sample_rate(&mut self, sample_rate: u32) {
self.sample_rate = sample_rate;
}
// Process incoming audio with VAD results
pub async fn process_audio_chunk(
&self,
audio_data: &[f32],
has_speech: bool,
_sample_rate: u32,
) -> Result<Option<String>> {
let now = Instant::now();
// Add to buffer
self.audio_buffer.lock().unwrap().extend_from_slice(audio_data);
// Get current state and decide what to do
let should_process_turn = {
let mut state = self.state.lock().unwrap();
match *state {
ConversationState::Idle => {
if has_speech {
info!("User started speaking");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
// Start new turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
}
false
}
ConversationState::UserSpeaking => {
if has_speech {
*self.last_speech_time.lock().unwrap() = now;
} else {
let silence_duration = now.duration_since(*self.last_speech_time.lock().unwrap());
if silence_duration > Duration::from_millis(self.brief_pause_threshold) {
info!("User paused briefly");
*state = ConversationState::UserPausedBriefly;
}
}
false
}
ConversationState::UserPausedBriefly => {
if has_speech {
// User resumed speaking
info!("User resumed speaking");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
false
} else {
let silence_duration = now.duration_since(*self.last_speech_time.lock().unwrap());
if silence_duration > Duration::from_millis(self.turn_end_threshold) {
info!("User finished speaking");
*state = ConversationState::UserFinished;
true // Process the turn
} else {
false
}
}
}
ConversationState::UserFinished => {
if has_speech {
// User started speaking again before assistant responded
info!("User started new turn");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
// Start new turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
}
false
}
ConversationState::AssistantSpeaking => {
if has_speech {
// User interrupted the assistant
info!("User interrupted assistant");
*state = ConversationState::AssistantInterrupted;
// Mark current assistant turn as interrupted
if let Some(ref mut turn) = *self.current_turn.lock().unwrap() {
turn.was_interrupted = true;
turn.ended_at = Some(now);
}
// Start new user turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
*state = ConversationState::UserSpeaking;
}
false
}
ConversationState::AssistantInterrupted => {
// Handled by assistant stopping its speech
*state = ConversationState::UserSpeaking;
false
}
}
}; // Lock is dropped here
// Process turn if needed (outside of lock)
if should_process_turn {
return self.process_complete_turn().await;
}
Ok(None)
}
// Process complete audio buffer when turn ends
async fn process_complete_turn(&self) -> Result<Option<String>> {
let audio_buffer = self.audio_buffer.lock().unwrap().clone();
if audio_buffer.is_empty() {
return Ok(None);
}
info!("Processing {} audio samples for speech-to-text", audio_buffer.len());
// Clear buffer for next turn
self.audio_buffer.lock().unwrap().clear();
// Use Whisper for speech-to-text if available
if let Some(ref whisper) = self.whisper_stt {
match whisper.transcribe(&audio_buffer, self.sample_rate).await {
Ok(text) => {
if !text.is_empty() {
info!("Transcribed: {}", text);
Ok(Some(text))
} else {
Ok(None)
}
}
Err(e) => {
info!("Transcription error: {}", e);
Ok(None)
}
}
} else {
// Fallback if Whisper not loaded
Ok(Some("[Whisper not loaded - please wait for model download]".to_string()))
}
}
// Called when assistant starts speaking
pub fn assistant_started_speaking(&self) {
let mut state = self.state.lock().unwrap();
*state = ConversationState::AssistantSpeaking;
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "assistant".to_string(),
segments: Vec::new(),
started_at: Instant::now(),
ended_at: None,
was_interrupted: false,
});
}
// Called when assistant stops speaking
pub fn assistant_stopped_speaking(&self) {
let mut state = self.state.lock().unwrap();
// Only go to idle if not interrupted
if *state == ConversationState::AssistantSpeaking {
*state = ConversationState::Idle;
}
// End current turn
if let Some(ref mut turn) = *self.current_turn.lock().unwrap() {
if turn.speaker == "assistant" {
turn.ended_at = Some(Instant::now());
// Save to history
let turn_clone = turn.clone();
self.conversation_history.lock().unwrap().push(turn_clone);
}
}
}
// Get conversation state
pub fn get_state(&self) -> ConversationState {
self.state.lock().unwrap().clone()
}
// Check if we should interrupt the assistant
pub fn should_interrupt_assistant(&self) -> bool {
matches!(
*self.state.lock().unwrap(),
ConversationState::AssistantInterrupted
)
}
// Get conversation history
pub fn get_conversation_history(&self) -> Vec<ConversationTurn> {
self.conversation_history.lock().unwrap().clone()
}
// Synchronous version that doesn't do transcription
pub fn process_audio_chunk_sync(
&mut self,
audio_data: &[f32],
has_speech: bool,
_sample_rate: u32,
) -> bool {
let now = Instant::now();
// Add to buffer
self.audio_buffer.lock().unwrap().extend_from_slice(audio_data);
// Get current state and decide what to do
let mut state = self.state.lock().unwrap();
match *state {
ConversationState::Idle => {
if has_speech {
info!("User started speaking");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
// Start new turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
}
false
}
ConversationState::UserSpeaking => {
if has_speech {
*self.last_speech_time.lock().unwrap() = now;
} else {
let silence_duration = now.duration_since(*self.last_speech_time.lock().unwrap());
if silence_duration > Duration::from_millis(self.brief_pause_threshold) {
info!("User paused briefly");
*state = ConversationState::UserPausedBriefly;
}
}
false
}
ConversationState::UserPausedBriefly => {
if has_speech {
// User resumed speaking
info!("User resumed speaking");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
false
} else {
let silence_duration = now.duration_since(*self.last_speech_time.lock().unwrap());
if silence_duration > Duration::from_millis(self.turn_end_threshold) {
info!("User finished speaking");
*state = ConversationState::UserFinished;
true // Should transcribe
} else {
false
}
}
}
ConversationState::UserFinished => {
if has_speech {
// User started speaking again before assistant responded
info!("User started new turn");
*state = ConversationState::UserSpeaking;
*self.last_speech_time.lock().unwrap() = now;
// Start new turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
}
false
}
ConversationState::AssistantSpeaking => {
if has_speech {
// User interrupted the assistant
info!("User interrupted assistant");
*state = ConversationState::AssistantInterrupted;
// Mark current assistant turn as interrupted
if let Some(ref mut turn) = *self.current_turn.lock().unwrap() {
turn.was_interrupted = true;
turn.ended_at = Some(now);
}
// Start new user turn
let mut current_turn = self.current_turn.lock().unwrap();
*current_turn = Some(ConversationTurn {
speaker: "user".to_string(),
segments: Vec::new(),
started_at: now,
ended_at: None,
was_interrupted: false,
});
*state = ConversationState::UserSpeaking;
}
false
}
ConversationState::AssistantInterrupted => {
// Handled by assistant stopping its speech
*state = ConversationState::UserSpeaking;
false
}
}
}
// Get and clear the audio buffer
pub fn get_and_clear_audio_buffer(&mut self) -> Vec<f32> {
let mut buffer = self.audio_buffer.lock().unwrap();
let audio_data = buffer.clone();
buffer.clear();
// Reset state to idle
*self.state.lock().unwrap() = ConversationState::Idle;
audio_data
}
}
// Whisper and Silero VAD are now in whisper.rs