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

n channels

Ezra Ellette78987f8733ce
diff --git a/screenpipe-audio/src/audio_processing.rs b/screenpipe-audio/src/audio_processing.rs
index 52f2bfdee..25011e15d 100644
--- a/screenpipe-audio/src/audio_processing.rs
+++ b/screenpipe-audio/src/audio_processing.rs
@@ -79,18 +79,20 @@ pub fn average_noise_spectrum(audio: &[f32]) -> f32 {
total_sum / audio.len() as f32
}
-pub fn stereo_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>>();
+pub fn audio_to_mono(audio: &[f32], channels: u16) -> Vec<f32> {
+ let mut mono_samples = Vec::with_capacity(audio.len() / channels as usize);
+
+ // Iterate over the audio slice in chunks, each containing `channels` samples
+ for chunk in audio.chunks(channels as usize) {
+ // Sum the samples from all channels in the current chunk
+ let sum: f32 = chunk.iter().sum();
+
+ // Calculate the averagechannelsono sample
+ let mono_sample = sum / channels as f32;
+
+ // Store the computed mono sample
+ mono_samples.push(mono_sample);
}
- mono
+ mono_samples
}
diff --git a/screenpipe-audio/src/core.rs b/screenpipe-audio/src/core.rs
index 16b7c5eea..6271a67a2 100644
--- a/screenpipe-audio/src/core.rs
+++ b/screenpipe-audio/src/core.rs
@@ -1,4 +1,4 @@
-use crate::audio_processing::stereo_to_mono;
+use crate::audio_processing::audio_to_mono;
use crate::AudioInput;
use anyhow::{anyhow, Result};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
@@ -203,7 +203,7 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let mono = stereo_to_mono(bytemuck::cast_slice(data), channels);
+ let mono = audio_to_mono(bytemuck::cast_slice(data), channels);
let _ = audio_queue_clone.push(mono);
}
@@ -218,7 +218,7 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let mono = stereo_to_mono(bytemuck::cast_slice(data), channels);
+ let mono = audio_to_mono(bytemuck::cast_slice(data), channels);
let _ = audio_queue_clone.push(mono);
}
@@ -233,7 +233,7 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let mono = stereo_to_mono(bytemuck::cast_slice(data), channels);
+ let mono = audio_to_mono(bytemuck::cast_slice(data), channels);
let _ = audio_queue_clone.push(mono);
}
@@ -248,7 +248,7 @@ pub async fn record_and_transcribe(
.upgrade()
.map_or(false, |arc| arc.load(Ordering::Relaxed))
{
- let mono = stereo_to_mono(data, channels);
+ let mono = audio_to_mono(data, channels);
let _ = audio_queue_clone.push(mono);
}