Skip to content

Commit

Permalink
chore: fix audio output on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Aug 4, 2024
1 parent fa97e66 commit 1524582
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
8 changes: 4 additions & 4 deletions screenpipe-audio/src/bin/screenpipe-audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Args {
list_audio_devices: bool,

#[clap(long, help = "Disable cloud audio processing")]
cloud_audio_off: bool,
use_cloud_audio: bool,
}

fn print_devices(devices: &[AudioDevice]) {
Expand Down Expand Up @@ -77,8 +77,8 @@ async fn main() -> Result<()> {

let chunk_duration = Duration::from_secs(5);
let output_path = PathBuf::from("output.mp4");
let cloud_audio = !args.cloud_audio_off;
let (whisper_sender, mut whisper_receiver) = create_whisper_channel(cloud_audio).await?;
let (whisper_sender, mut whisper_receiver) =
create_whisper_channel(args.use_cloud_audio).await?;
// Spawn threads for each device
let recording_threads: Vec<_> = devices
.into_iter()
Expand Down Expand Up @@ -132,4 +132,4 @@ async fn main() -> Result<()> {
}

Ok(())
}
}
30 changes: 11 additions & 19 deletions screenpipe-audio/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,10 @@ fn get_device_and_config(
) -> Result<(cpal::Device, cpal::SupportedStreamConfig)> {
let host = match audio_device.device_type {
#[cfg(target_os = "macos")]
DeviceType::Output => cpal::default_host(),
DeviceType::Output => cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?,
_ => cpal::default_host(),
};

info!("device: {:?}", audio_device.to_string());

let audio_device = if audio_device.to_string() == "default" {
match audio_device.device_type {
DeviceType::Input => host.default_input_device(),
Expand All @@ -103,6 +101,9 @@ fn get_device_and_config(
DeviceType::Input => host.input_devices()?,
DeviceType::Output => host.output_devices()?,
};
if cfg!(target_os = "macos") && audio_device.device_type == DeviceType::Output {
devices = host.input_devices()?;
}

devices.find(|x| {
x.name()
Expand Down Expand Up @@ -154,16 +155,6 @@ async fn run_ffmpeg(
.stdout(Stdio::piped())
.stderr(Stdio::piped());

// ! tmp hack shouldnt be needed
// Explicitly set the library paths for the FFmpeg command
// if let Ok(ld_library_path) = std::env::var("LD_LIBRARY_PATH") {
// command.env("LD_LIBRARY_PATH", ld_library_path);
// }
// #[cfg(target_os = "macos")]
// if let Ok(dyld_library_path) = std::env::var("DYLD_LIBRARY_PATH") {
// command.env("DYLD_LIBRARY_PATH", dyld_library_path);
// }

debug!("FFmpeg command: {:?}", command);

let mut ffmpeg: tokio::process::Child =
Expand Down Expand Up @@ -326,9 +317,9 @@ pub fn list_audio_devices() -> Result<Vec<AudioDevice>> {
{
// !HACK macos is suppoed to use special macos feature "display capture"
// ! see https://github.com/RustAudio/cpal/pull/894
let host = cpal::default_host();
// let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
for device in host.output_devices()? {
// let host = cpal::default_host();
let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
for device in host.input_devices()? {
if let Ok(name) = device.name() {
devices.push(AudioDevice::new(name, DeviceType::Output));
}
Expand Down Expand Up @@ -359,10 +350,11 @@ pub fn default_output_device() -> Result<AudioDevice> {
{
// !HACK macos is suppoed to use special macos feature "display capture"
// ! see https://github.com/RustAudio/cpal/pull/894
let host = cpal::default_host();
// let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
// let host = cpal::default_host();
let host = cpal::host_from_id(cpal::HostId::ScreenCaptureKit)?;
let device = host
.default_output_device()
.default_input_device()
// .default_output_device()
.ok_or_else(|| anyhow!("No default input device found"))?;
info!("Using display capture device: {}", device.name()?);
return Ok(AudioDevice::new(device.name()?, DeviceType::Output));
Expand Down
12 changes: 6 additions & 6 deletions screenpipe-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,12 @@ impl Server {
// https://github.com/tokio-rs/axum/blob/main/examples/tracing-aka-logging/src/main.rs
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().include_headers(true))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(
DefaultOnResponse::new()
.level(Level::INFO)
.latency_unit(LatencyUnit::Micros),
),
// .on_request(DefaultOnRequest::new().level(Level::INFO))
// .on_response(
// DefaultOnResponse::new()
// .level(Level::INFO)
// .latency_unit(LatencyUnit::Micros),
// ),
)
.with_state(app_state);

Expand Down

0 comments on commit 1524582

Please sign in to comment.