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(sdk): adapt mic stream for macos cpal

Louis Beaumont91c20af7fa1f
diff --git a/ee/sdk/Cargo.lock b/ee/sdk/Cargo.lock
index c4ae07e3e..661799241 100644
--- a/ee/sdk/Cargo.lock
+++ b/ee/sdk/Cargo.lock
@@ -5753,10 +5753,8 @@ dependencies = [
"screenpipe-a11y",
"screenpipe-core",
"screenpipe-screen",
- "thiserror 1.0.69",
"tokio",
"tracing",
- "tracing-subscriber",
]
[[package]]
diff --git a/ee/sdk/src/audio.rs b/ee/sdk/src/audio.rs
index 39ee7fb26..be9e4c1ae 100644
--- a/ee/sdk/src/audio.rs
+++ b/ee/sdk/src/audio.rs
@@ -75,29 +75,29 @@ fn build_and_run_stream(level_bits: &'static AtomicU32) -> Result<cpal::Stream>
let err_fn = |e| tracing::warn!("screenpipe-sdk mic stream err: {e}");
let stream = match sample_format {
- SampleFormat::F32 => device.build_input_stream(
+ SampleFormat::F32 => build_level_input_stream::<f32, _, _>(
+ &device,
&stream_config,
move |data: &[f32], _| push_rms(data, level_bits),
err_fn,
- None,
),
- SampleFormat::I16 => device.build_input_stream(
+ SampleFormat::I16 => build_level_input_stream::<i16, _, _>(
+ &device,
&stream_config,
move |data: &[i16], _| {
let f: Vec<f32> = data.iter().map(|s| s.to_sample::<f32>()).collect();
push_rms(&f, level_bits);
},
err_fn,
- None,
),
- SampleFormat::U16 => device.build_input_stream(
+ SampleFormat::U16 => build_level_input_stream::<u16, _, _>(
+ &device,
&stream_config,
move |data: &[u16], _| {
let f: Vec<f32> = data.iter().map(|s| s.to_sample::<f32>()).collect();
push_rms(&f, level_bits);
},
err_fn,
- None,
),
other => return Err(anyhow!("unsupported sample format: {other:?}")),
}
@@ -106,6 +106,36 @@ fn build_and_run_stream(level_bits: &'static AtomicU32) -> Result<cpal::Stream>
Ok(stream)
}
+#[cfg(target_os = "macos")]
+fn build_level_input_stream<T, D, E>(
+ device: &cpal::Device,
+ stream_config: &cpal::StreamConfig,
+ data_callback: D,
+ error_callback: E,
+) -> std::result::Result<cpal::Stream, cpal::BuildStreamError>
+where
+ T: cpal::SizedSample,
+ D: FnMut(&[T], &cpal::InputCallbackInfo) + Send + 'static,
+ E: FnMut(cpal::StreamError) + Send + 'static,
+{
+ device.build_input_stream(stream_config, data_callback, error_callback, None, None)
+}
+
+#[cfg(not(target_os = "macos"))]
+fn build_level_input_stream<T, D, E>(
+ device: &cpal::Device,
+ stream_config: &cpal::StreamConfig,
+ data_callback: D,
+ error_callback: E,
+) -> std::result::Result<cpal::Stream, cpal::BuildStreamError>
+where
+ T: cpal::SizedSample,
+ D: FnMut(&[T], &cpal::InputCallbackInfo) + Send + 'static,
+ E: FnMut(cpal::StreamError) + Send + 'static,
+{
+ device.build_input_stream(stream_config, data_callback, error_callback, None)
+}
+
fn push_rms(samples: &[f32], level_bits: &AtomicU32) {
if samples.is_empty() {
return;
@@ -179,10 +209,7 @@ mod tests {
push_rms(&samples, &bits);
let l = level(&bits);
let expected = 0.7 * std::f32::consts::FRAC_1_SQRT_2;
- assert!(
- (l - expected).abs() < 0.02,
- "got {l}, expected {expected}"
- );
+ assert!((l - expected).abs() < 0.02, "got {l}, expected {expected}");
}
#[test]
@@ -230,7 +257,10 @@ mod tests {
prev = now;
}
// After many iterations we should be very close to the true RMS (0.3).
- assert!((prev - 0.3).abs() < 0.01, "converged to {prev}, expected ~0.3");
+ assert!(
+ (prev - 0.3).abs() < 0.01,
+ "converged to {prev}, expected ~0.3"
+ );
}
#[test]
@@ -245,4 +275,3 @@ mod tests {
assert!(diff < 1e-4, "±dc produced different RMS: {diff}");
}
}
-