Skip to content

Commit 4f85762

Browse files
authored
Merge pull request #8 from second-state/voice-int2
Voice interrupt 2
2 parents 7093b40 + 2bfba01 commit 4f85762

File tree

9 files changed

+165
-138
lines changed

9 files changed

+165
-138
lines changed

src/app.rs

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub enum Event {
1414
ServerEvent(ServerEvent),
1515
MicAudioChunk(Vec<i16>),
1616
MicAudioEnd,
17-
MicInterrupt(Vec<i16>),
1817
MicInterruptWaitTimeout,
1918
#[cfg_attr(not(feature = "extra_server"), allow(unused))]
2019
ServerUrl(String),
@@ -83,9 +82,6 @@ async fn select_evt(
8382
Event::ServerEvent(_) => {
8483
log::info!("[Select] Received ServerEvent: {:?}", evt);
8584
},
86-
Event::MicInterrupt(data) => {
87-
log::info!("[Select] Received MicInterrupt with {} samples", data.len());
88-
},
8985
Event::MicInterruptWaitTimeout => {
9086
log::info!("[Select] Received MicInterruptWaitTimeout");
9187
}
@@ -292,11 +288,7 @@ pub async fn main_work<'d>(
292288
Event::Event(evt) => {
293289
log::info!("Received event: {:?}", evt);
294290
}
295-
Event::MicAudioChunk(data) => {
296-
if state != State::Listening {
297-
log::debug!("Received MicAudioChunk while no Listening state, ignoring");
298-
continue;
299-
}
291+
Event::MicAudioChunk(data) if state == State::Listening => {
300292
submit_audio += data.len() as f32 / 16000.0;
301293
audio_buffer.extend_from_slice(&data);
302294

@@ -313,12 +305,60 @@ pub async fn main_work<'d>(
313305
audio_buffer = Vec::with_capacity(8192);
314306
}
315307
}
308+
Event::MicAudioChunk(data) if state == State::Speaking && allow_interrupt => {
309+
submit_audio += data.len() as f32 / 16000.0;
310+
audio_buffer.extend_from_slice(&data);
311+
if audio_buffer.len() == 0 {
312+
player_tx
313+
.send(AudioEvent::StopSpeech)
314+
.map_err(|_| anyhow::anyhow!("Error sending stop"))?;
315+
}
316+
317+
if submit_audio > 0.6 {
318+
state = State::Listening;
319+
gui.state = "Listening...".to_string();
320+
gui.display_flush().unwrap();
321+
322+
server.reconnect_with_retry(3).await?;
323+
324+
start_submit = true;
325+
server
326+
.send_client_command(protocol::ClientCommand::StartChat)
327+
.await?;
328+
329+
player_tx
330+
.send(AudioEvent::ClearSpeech)
331+
.map_err(|_| anyhow::anyhow!("Error sending clear"))?;
332+
}
333+
}
334+
Event::MicAudioChunk(_) => {
335+
log::debug!("Received MicAudioChunk while no Listening/Speaking state, ignoring");
336+
}
316337
Event::MicAudioEnd => {
317338
log::info!("Received MicAudioEnd");
318-
if state != State::Listening {
319-
log::debug!("Received MicAudioEnd while no Listening state, ignoring");
339+
if state != State::Listening && state != State::Speaking {
340+
log::debug!("Received MicAudioEnd while no Listening/Speaking state, ignoring");
320341
continue;
321342
}
343+
344+
if state == State::Speaking {
345+
if !allow_interrupt {
346+
log::info!("Interrupt not allowed, ignoring MicAudioEnd");
347+
continue;
348+
}
349+
log::info!("resuming to Listening state due to MicAudioEnd");
350+
player_tx
351+
.send(AudioEvent::StartSpeech)
352+
.map_err(|_| anyhow::anyhow!("Error sending stop"))?;
353+
log::info!("Waiting for stop speech response");
354+
submit_audio = 0.0;
355+
start_submit = false;
356+
audio_buffer.clear();
357+
continue;
358+
}
359+
360+
log::info!("submit_audio = {}", submit_audio);
361+
322362
if submit_audio > 0.5 {
323363
if !audio_buffer.is_empty() {
324364
server.send_client_audio_chunk_i16(audio_buffer).await?;
@@ -338,51 +378,6 @@ pub async fn main_work<'d>(
338378
gui.display_flush().unwrap();
339379
}
340380
}
341-
Event::MicInterrupt(interrupt_data) => {
342-
log::info!(
343-
"Received MicInterrupt with {} samples",
344-
interrupt_data.len()
345-
);
346-
if !(state == State::Listening || state == State::Speaking) {
347-
log::debug!(
348-
"Received MicInterrupt while no Listening or Speaking state, ignoring"
349-
);
350-
continue;
351-
}
352-
353-
if !allow_interrupt {
354-
log::info!("Interrupts are disabled, ignoring MicInterrupt");
355-
continue;
356-
}
357-
358-
let interrupt_audio_sec = interrupt_data.len() as f32 / 16000.0;
359-
if interrupt_audio_sec < 1.2 {
360-
log::info!(
361-
"Interrupt audio too short ({} s), ignoring",
362-
interrupt_audio_sec
363-
);
364-
continue;
365-
}
366-
367-
let int_notify = Arc::new(tokio::sync::Notify::new());
368-
player_tx
369-
.send(AudioEvent::Interrupt(int_notify.clone()))
370-
.map_err(|e| anyhow::anyhow!("Error sending interrupt: {e:?}"))?;
371-
log::info!("Waiting for interrupt response");
372-
let _ = int_notify.notified().await;
373-
log::info!("Interrupt response received");
374-
375-
server.reconnect_with_retry(3).await?;
376-
377-
start_submit = false;
378-
submit_audio = interrupt_audio_sec;
379-
audio_buffer = interrupt_data;
380-
381-
state = State::Listening;
382-
gui.state = "Listening...".to_string();
383-
gui.display_flush().unwrap();
384-
timeout = INTERNAL_TIMEOUT;
385-
}
386381
Event::MicInterruptWaitTimeout => {
387382
log::info!("Received MicInterruptWaitTimeout");
388383
timeout = NORMAL_TIMEOUT;

0 commit comments

Comments
 (0)