Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: audio speech detection consistency across devices

Louis Beaumontee4cd3d2cb5f
diff --git a/crates/screenpipe-audio/src/audio_manager/manager.rs b/crates/screenpipe-audio/src/audio_manager/manager.rs
index d9c339683..e9dfe3a7b 100644
--- a/crates/screenpipe-audio/src/audio_manager/manager.rs
+++ b/crates/screenpipe-audio/src/audio_manager/manager.rs
@@ -41,7 +41,7 @@ use crate::{
stt::{process_audio_input, SAMPLE_RATE},
whisper::model::{create_whisper_context_parameters, download_whisper_model},
},
- utils::{audio::resample, ffmpeg::{get_new_file_path, write_audio_to_file}},
+ utils::{audio::{normalize_v2, resample}, ffmpeg::{get_new_file_path, write_audio_to_file}},
vad::{silero::SileroVad, webrtc::WebRtcVad, VadEngine, VadEngineEnum},
AudioInput, TranscriptionResult,
};
@@ -462,13 +462,15 @@ impl AudioManager {
while let Ok(audio) = whisper_receiver.recv() {
info!("Received audio from device: {:?}", audio.device.name);
- // Audio-based call detection: update meeting detector with speech activity
+ // Audio-based call detection: normalize first so the threshold works
+ // consistently across devices with different native gain levels
if let Some(ref meeting) = meeting_detector {
+ let normalized = normalize_v2(&audio.data);
let rms = {
- let sum_sq: f32 = audio.data.iter().map(|&x| x * x).sum();
- (sum_sq / audio.data.len() as f32).sqrt()
+ let sum_sq: f32 = normalized.iter().map(|&x| x * x).sum();
+ (sum_sq / normalized.len() as f32).sqrt()
};
- meeting.on_audio_activity(&audio.device.device_type, rms > 0.05);
+ meeting.on_audio_activity(&audio.device.device_type, rms > 0.08);
}
// ALWAYS persist audio to disk immediately, before any deferral.
diff --git a/crates/screenpipe-audio/src/vad/silero.rs b/crates/screenpipe-audio/src/vad/silero.rs
index ba179b4e9..43663c469 100644
--- a/crates/screenpipe-audio/src/vad/silero.rs
+++ b/crates/screenpipe-audio/src/vad/silero.rs
@@ -174,47 +174,67 @@ impl SileroVad {
impl VadEngine for SileroVad {
fn is_voice_segment(&mut self, audio_chunk: &[f32]) -> anyhow::Result<bool> {
- // Silero VAD v5 requires fixed 512 samples for 16kHz (~32ms)
+ // Silero VAD v5 requires fixed 512 samples for 16kHz (~32ms).
+ // Process all 512-sample windows in the chunk instead of truncating.
const CHUNK_SIZE: usize = 512;
let threshold = SPEECH_THRESHOLD;
+ let mut max_prob: f32 = 0.0;
- let mut chunk_data: Vec<f32> = audio_chunk.to_vec();
- chunk_data.resize(CHUNK_SIZE, 0.0);
+ for window in audio_chunk.chunks(CHUNK_SIZE) {
+ let mut chunk_data = window.to_vec();
+ chunk_data.resize(CHUNK_SIZE, 0.0);
- let result = self.vad.compute(&chunk_data).map_err(|e| {
- debug!("SileroVad Error computing VAD: {}", e);
- anyhow::anyhow!("Vad compute error: {}", e)
- })?;
+ let result = self.vad.compute(&chunk_data).map_err(|e| {
+ debug!("SileroVad Error computing VAD: {}", e);
+ anyhow::anyhow!("Vad compute error: {}", e)
+ })?;
- let status = self.update_status(result.prob);
+ self.update_status(result.prob);
+ if result.prob > max_prob {
+ max_prob = result.prob;
+ }
+ }
+
+ let status = if max_prob > threshold {
+ VadStatus::Speech
+ } else {
+ VadStatus::Silence
+ };
- Ok(status == VadStatus::Speech && result.prob > threshold)
+ Ok(status == VadStatus::Speech)
}
fn audio_type(&mut self, audio_chunk: &[f32]) -> anyhow::Result<VadStatus> {
- // Silero VAD v5 requires fixed 512 samples for 16kHz (~32ms)
+ // Silero VAD v5 requires fixed 512 samples for 16kHz (~32ms).
+ // Process all 512-sample windows in the chunk instead of truncating.
const CHUNK_SIZE: usize = 512;
let threshold = SPEECH_THRESHOLD;
+ let mut max_prob: f32 = 0.0;
+ let mut last_status = VadStatus::Unknown;
- let mut chunk_data: Vec<f32> = audio_chunk.to_vec();
- chunk_data.resize(CHUNK_SIZE, 0.0);
+ for window in audio_chunk.chunks(CHUNK_SIZE) {
+ let mut chunk_data = window.to_vec();
+ chunk_data.resize(CHUNK_SIZE, 0.0);
- let result = self.vad.compute(&chunk_data).map_err(|e| {
- debug!("SileroVad Error computing VAD: {}", e);
- anyhow::anyhow!("Vad compute error: {}", e)
- })?;
+ let result = self.vad.compute(&chunk_data).map_err(|e| {
+ debug!("SileroVad Error computing VAD: {}", e);
+ anyhow::anyhow!("Vad compute error: {}", e)
+ })?;
- let status = self.update_status(result.prob);
+ last_status = self.update_status(result.prob);
+ if result.prob > max_prob {
+ max_prob = result.prob;
+ }
+ }
- if status == VadStatus::Speech && result.prob > threshold {
+ if max_prob > threshold {
return Ok(VadStatus::Speech);
}
- match status {
+ match last_status {
VadStatus::Unknown => Ok(VadStatus::Unknown),
- // this is super misleading
_ => Ok(VadStatus::Silence),
}
}