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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: use lower RMS threshold for output devices in batch mode
Louis Beaumont791693fc79bb
diff --git a/crates/screenpipe-audio/src/audio_manager/manager.rs b/crates/screenpipe-audio/src/audio_manager/manager.rsindex 9c4e4807b..fd9857122 100644--- a/crates/screenpipe-audio/src/audio_manager/manager.rs+++ b/crates/screenpipe-audio/src/audio_manager/manager.rs@@ -435,13 +435,21 @@ impl AudioManager {while let Ok(audio) = whisper_receiver.recv() {debug!("received audio from device: {:?}", audio.device.name);- // Audio-based call detection: update meeting detector with speech activity+ // Audio-based call detection: update meeting detector with speech activity.+ // Output devices (SCK on macOS) produce much quieter audio than mic input,+ // so we use a lower threshold. Empirical data from real SCK captures:+ // silence = 0.0, audio playing = 0.0028, loud output = 0.06+ // For output we just need "is audio playing?" not "is someone speaking?"if let Some(ref meeting) = meeting_detector {let rms = {let sum_sq: f32 = audio.data.iter().map(|&x| x * x).sum();(sum_sq / audio.data.len() as f32).sqrt()};- meeting.on_audio_activity(&audio.device.device_type, rms > 0.05);+ let has_activity = match audio.device.device_type {+ crate::core::device::DeviceType::Output => rms > 0.001,+ crate::core::device::DeviceType::Input => rms > 0.05,+ };+ meeting.on_audio_activity(&audio.device.device_type, has_activity);}// ALWAYS persist audio to disk immediately, before any deferral.diff --git a/crates/screenpipe-audio/tests/batch_deferral_test.rs b/crates/screenpipe-audio/tests/batch_deferral_test.rsindex a41f92397..1453b9bae 100644--- a/crates/screenpipe-audio/tests/batch_deferral_test.rs+++ b/crates/screenpipe-audio/tests/batch_deferral_test.rs@@ -412,4 +412,123 @@ mod tests {);}}++ // ---------------------------------------------------------------+ // 7. RMS threshold validation with real-world audio levels+ // Empirical data from macOS ScreenCaptureKit captures:+ // SCK silence: RMS = 0.000000+ // SCK audio playing: RMS = 0.002753+ // Loud output device: RMS = 0.061207+ // Mic ambient noise: RMS = 0.004465+ // Output threshold: 0.001 (detects "any audio playing")+ // Input threshold: 0.05 (detects "someone speaking")+ // ---------------------------------------------------------------++ /// Mirrors the threshold logic in manager.rs audio receiver handler.+ fn has_audio_activity(device_type: &DeviceType, rms: f32) -> bool {+ match device_type {+ DeviceType::Output => rms > 0.001,+ DeviceType::Input => rms > 0.05,+ }+ }++ #[test]+ fn output_threshold_rejects_sck_silence() {+ // Real SCK silence: RMS = 0.0+ assert!(+ !has_audio_activity(&DeviceType::Output, 0.0),+ "SCK silence should not trigger activity"+ );+ }++ #[test]+ fn output_threshold_detects_sck_audio_playing() {+ // Real SCK audio (YouTube): RMS = 0.002753+ assert!(+ has_audio_activity(&DeviceType::Output, 0.002753),+ "SCK output with audio playing should trigger activity"+ );+ }++ #[test]+ fn output_threshold_detects_loud_output_device() {+ // Real loud output device: RMS = 0.061+ assert!(+ has_audio_activity(&DeviceType::Output, 0.061),+ "loud output device should trigger activity"+ );+ }++ #[test]+ fn input_threshold_rejects_ambient_noise() {+ // Real mic ambient noise: RMS = 0.004+ assert!(+ !has_audio_activity(&DeviceType::Input, 0.004),+ "mic ambient noise should not trigger activity"+ );+ }++ #[test]+ fn input_threshold_detects_speech() {+ // Typical speech on mic: RMS ~0.08-0.3+ assert!(+ has_audio_activity(&DeviceType::Input, 0.08),+ "mic speech should trigger activity"+ );+ }++ #[tokio::test]+ async fn sck_output_audio_triggers_deferral_in_batch_mode() {+ // End-to-end: SCK output with YouTube audio → session detected → batch defers+ let detector = MeetingDetector::new();++ // Simulate the RMS check + on_audio_activity call as done in manager.rs+ let sck_rms: f32 = 0.002753; // real measured value+ let has_activity = has_audio_activity(&DeviceType::Output, sck_rms);+ assert!(has_activity, "SCK output RMS should pass threshold");++ detector.on_audio_activity(&DeviceType::Output, has_activity);++ // Session should now be active+ assert!(+ detector.is_in_audio_session(),+ "output audio session should be active after SCK audio"+ );++ // Batch mode should defer+ let was = detector.is_in_audio_session();+ detector.check_grace_period().await;+ let now = detector.is_in_audio_session();+ assert_eq!(+ decide(true, was, now),+ BatchDecision::Defer,+ "batch mode should defer when SCK output has audio"+ );+ }++ #[tokio::test]+ async fn sck_silence_does_not_trigger_deferral() {+ // End-to-end: SCK silence → no session → batch transcribes immediately+ let detector = MeetingDetector::new();++ let sck_silence_rms: f32 = 0.0;+ let has_activity = has_audio_activity(&DeviceType::Output, sck_silence_rms);+ assert!(!has_activity, "SCK silence should not pass threshold");++ detector.on_audio_activity(&DeviceType::Output, has_activity);++ assert!(+ !detector.is_in_audio_session(),+ "no session when output is silent"+ );++ let was = detector.is_in_audio_session();+ detector.check_grace_period().await;+ let now = detector.is_in_audio_session();+ assert_eq!(+ decide(true, was, now),+ BatchDecision::TranscribeNow,+ "batch should transcribe immediately when output is silent"+ );+ }}