From 22579ced3d1d847a14683fe3b47fa2076df01751 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Thu, 5 Nov 2020 00:44:04 +0100 Subject: add mute feature --- mumd/src/audio.rs | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'mumd/src/audio.rs') diff --git a/mumd/src/audio.rs b/mumd/src/audio.rs index 9f837f7..05e3ff5 100644 --- a/mumd/src/audio.rs +++ b/mumd/src/audio.rs @@ -21,13 +21,13 @@ pub struct Audio { output_volume_sender: watch::Sender, - user_volumes: Arc>>, + user_volumes: Arc>>, client_streams: Arc>>, } impl Audio { - pub fn new() -> Self { + pub fn new(input_volume: f32, output_volume: f32) -> Self { let sample_rate = SampleRate(48000); let host = cpal::default_host(); @@ -70,7 +70,7 @@ impl Audio { let err_fn = |err| error!("An error occurred on the output audio stream: {}", err); let user_volumes = Arc::new(Mutex::new(HashMap::new())); - let (output_volume_sender, output_volume_receiver) = watch::channel::(1.0); + let (output_volume_sender, output_volume_receiver) = watch::channel::(output_volume); let client_streams = Arc::new(Mutex::new(HashMap::new())); let output_stream = match output_supported_sample_format { @@ -119,7 +119,7 @@ impl Audio { .unwrap(); let (input_sender, input_receiver) = mpsc::channel(100); - let (input_volume_sender, input_volume_receiver) = watch::channel::(1.0); + let (input_volume_sender, input_volume_receiver) = watch::channel::(input_volume); let input_stream = match input_supported_sample_format { SampleFormat::F32 => input_device.build_input_stream( @@ -230,6 +230,24 @@ impl Audio { } pub fn set_user_volume(&self, id: u32, volume: f32) { - self.user_volumes.lock().unwrap().insert(id, volume); + match self.user_volumes.lock().unwrap().entry(id) { + Entry::Occupied(mut entry) => { + entry.get_mut().0 = volume; + } + Entry::Vacant(entry) => { + entry.insert((volume, false)); + } + } + } + + pub fn set_mute(&self, id: u32, mute: bool) { + match self.user_volumes.lock().unwrap().entry(id) { + Entry::Occupied(mut entry) => { + entry.get_mut().1 = mute; + } + Entry::Vacant(entry) => { + entry.insert((1.0, mute)); + } + } } } -- cgit v1.2.1