Skip to content

Commit

Permalink
Merge pull request #20 from xd009642/feat/api-events
Browse files Browse the repository at this point in the history
Add VAD on/off events to API
  • Loading branch information
xd009642 authored Sep 10, 2024
2 parents 09915cf + 80193b8 commit b8bf402
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/api_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub enum Event {
Data(model::Output),
Segment(SegmentOutput),
Error(String),
Active,
Inactive,
Active { time: f32 },
Inactive { time: f32 },
}

#[derive(Serialize, Deserialize)]
Expand Down
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,31 @@ impl StreamingContext {
current_start = Some(*timestamp_ms);
current_end = None;
found_endpoint = false;
if output
.send(Event::Active {
time: *timestamp_ms as f32 / 1000.0,
})
.await
.is_err()
{
error!("Failed to send vad active event");
anyhow::bail!("Output channel closed");
}
}
VadTransition::SpeechEnd { timestamp_ms } => {
info!(time_ms = timestamp_ms, "Detected end of speech");
current_end = Some(*timestamp_ms);
found_endpoint = true;
if output
.send(Event::Inactive {
time: *timestamp_ms as f32 / 1000.0,
})
.await
.is_err()
{
error!("Failed to send vad inactive event");
anyhow::bail!("Output channel closed");
}
}
}
}
Expand All @@ -207,11 +227,22 @@ impl StreamingContext {

// If we're speaking then we haven't endpointed so do the final inference
if vad.is_speaking() {
let session_time = vad.session_time();
if output
.send(Event::Inactive {
time: session_time.as_secs_f32(),
})
.await
.is_err()
{
error!("Failed to send end of audio inactive event");
anyhow::bail!("Output channel closed");
}
let audio = vad.get_current_speech().to_vec();
let msg = self
.spawned_inference(
audio,
current_start.zip(Some(vad.session_time().as_millis() as usize)),
current_start.zip(Some(session_time.as_millis() as usize)),
)
.await;
output.send(msg).await?;
Expand Down

0 comments on commit b8bf402

Please sign in to comment.