Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Nov 30, 2024
1 parent f1a639f commit 741b64b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions packages/desktop/src/providers/audio/audio_provider.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::HashMap;
use std::{
collections::HashMap,
time::{Duration, Instant},
};

use anyhow::Context;
use crossbeam::channel;
Expand Down Expand Up @@ -95,14 +98,15 @@ struct DeviceState {
device_id: String,
device_type: DeviceType,
volume: u32,
com_device: IMMDevice,
com_volume: IAudioEndpointVolume,
com_volume_callback: IAudioEndpointVolumeCallback,
}

pub struct AudioProvider {
common: CommonProviderState,
com_enumerator: Option<IMMDeviceEnumerator>,
last_emit: Instant,
pending_emission: bool,
default_playback_id: Option<String>,
default_recording_id: Option<String>,
device_states: HashMap<String, DeviceState>,
Expand All @@ -120,6 +124,8 @@ impl AudioProvider {
Self {
common,
com_enumerator: None,
last_emit: Instant::now(),
pending_emission: false,
default_playback_id: None,
default_recording_id: None,
device_states: HashMap::new(),
Expand Down Expand Up @@ -191,6 +197,13 @@ impl AudioProvider {
_ => {}
}
}
default(Duration::from_millis(20)) => {
// Batch emissions to reduce overhead.
if self.pending_emission {
self.emit_output();
self.pending_emission = false;
}
}
}
}

Expand Down Expand Up @@ -296,6 +309,8 @@ impl AudioProvider {
}

self.common.emitter.emit_output(Ok(output));
self.last_emit = Instant::now();
self.pending_emission = true;
}

/// Gets the default device ID for the given device type.
Expand Down Expand Up @@ -358,7 +373,6 @@ impl AudioProvider {
device_id: device_id.clone(),
device_type: device_type.clone(),
volume: (volume * 100.0).round() as u32,
com_device,
com_volume,
com_volume_callback,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/desktop/src/providers/provider_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, sync::Arc};

use anyhow::{bail, Context};
use anyhow::Context;
use serde::{ser::SerializeStruct, Serialize};
use tauri::{AppHandle, Emitter};
use tokio::{
Expand Down

0 comments on commit 741b64b

Please sign in to comment.