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: converge interleaved audio samples

Ezra Ellette23be7622a024
diff --git a/screenpipe-audio/src/audio_processing.rs b/screenpipe-audio/src/audio_processing.rs
index 16ab789f1..cdb417547 100644
--- a/screenpipe-audio/src/audio_processing.rs
+++ b/screenpipe-audio/src/audio_processing.rs
@@ -78,3 +78,19 @@ pub fn average_noise_spectrum(audio: &[f32]) -> f32 {
total_sum / audio.len() as f32
}
+
+pub fn converge_to_mono(audio: &[f32], channels: u16) -> Vec<f32> {
+ let mut mono = audio.to_vec();
+
+ if channels > 1 {
+ mono = audio
+ .chunks(2)
+ .map(|x| {
+ let (left, right) = (x[0], x[1]);
+ (left + right) / 2.0
+ })
+ .collect::<Vec<f32>>();
+ }
+
+ mono
+}
diff --git a/screenpipe-audio/src/core.rs b/screenpipe-audio/src/core.rs
index f7e8bcd8d..9e233a6a2 100644
--- a/screenpipe-audio/src/core.rs
+++ b/screenpipe-audio/src/core.rs
@@ -1,3 +1,4 @@
+use crate::audio_processing::converge_to_mono;
use crate::AudioInput;
use anyhow::{anyhow, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
@@ -126,7 +127,7 @@ async fn get_device_and_config(
#[cfg(target_os = "macos")]
{
- if audio_device.device_type == DeviceType::Output {
+ if is_output_device {
if let Ok(screen_capture_host) = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)
{
devices = screen_capture_host.input_devices()?;
@@ -165,7 +166,8 @@ pub async fn record_and_transcribe(
) -> Result<()> {
let (cpal_audio_device, config) = get_device_and_config(&audio_device).await?;
let sample_rate = config.sample_rate().0;
- let channels = config.channels() as u16;
+ let channels = config.channels();
+
debug!(
"Audio device config: sample_rate={}, channels={}",
sample_rate, channels
@@ -201,7 +203,9 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let _ = audio_queue_clone.push(bytemuck::cast_slice(data).to_vec());
+ let mono = converge_to_mono(bytemuck::cast_slice(data), channels);
+
+ let _ = audio_queue_clone.push(mono);
}
},
error_callback,
@@ -214,7 +218,9 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let _ = audio_queue_clone.push(bytemuck::cast_slice(data).to_vec());
+ let mono = converge_to_mono(bytemuck::cast_slice(data), channels);
+
+ let _ = audio_queue_clone.push(mono);
}
},
error_callback,
@@ -227,7 +233,9 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let _ = audio_queue_clone.push(bytemuck::cast_slice(data).to_vec());
+ let mono = converge_to_mono(bytemuck::cast_slice(data), channels);
+
+ let _ = audio_queue_clone.push(mono);
}
},
error_callback,
@@ -240,7 +248,9 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let _ = audio_queue_clone.push(data.to_vec());
+ let mono = converge_to_mono(data, channels);
+
+ let _ = audio_queue_clone.push(mono);
}
},
error_callback,
diff --git a/screenpipe-audio/src/stt.rs b/screenpipe-audio/src/stt.rs
index d08f36fa0..14d4e86f9 100644
--- a/screenpipe-audio/src/stt.rs
+++ b/screenpipe-audio/src/stt.rs
@@ -315,12 +315,7 @@ pub async fn stt(
audio_input.data.as_ref().to_vec()
};
- let audio_data = if audio_input.device.device_type == DeviceType::Input {
- normalize_v2(&audio_data)
- } else {
- // ! for some reason buggy on output devices
- audio_data
- };
+ let audio_data = normalize_v2(&audio_data);
let frame_size = 1600; // 100ms frame size for 16kHz audio
let mut speech_frames = Vec::new();