feat(audio): play audio-volume-change sound on volume change

This commit is contained in:
Michael Aaron Murphy 2024-02-06 05:29:25 +01:00 committed by Ashley Wulber
parent a1f2630462
commit 847db3a4f2
3 changed files with 35 additions and 1 deletions

View file

@ -39,6 +39,7 @@ use mpris_subscription::MprisUpdate;
mod config; mod config;
mod mpris_subscription; mod mpris_subscription;
mod pipewire;
mod pulse; mod pulse;
const VERSION: &str = env!("CARGO_PKG_VERSION"); const VERSION: &str = env!("CARGO_PKG_VERSION");

View file

@ -0,0 +1,21 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use std::path::Path;
use std::process::Stdio;
/// Plays an audio file.
pub fn play(path: &Path) {
let _result = tokio::process::Command::new("pw-play")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.arg(path)
.spawn();
}
pub fn play_audio_volume_change() {
play(Path::new(
"/usr/share/sounds/freedesktop/stereo/audio-volume-change.oga",
));
}

View file

@ -2,10 +2,11 @@ use std::cell::RefCell;
use std::{rc::Rc, thread}; use std::{rc::Rc, thread};
extern crate libpulse_binding as pulse; extern crate libpulse_binding as pulse;
use cosmic::iced::{self, subscription}; use cosmic::iced::{self, subscription};
use cosmic::iced_futures::futures::{self, SinkExt}; use cosmic::iced_futures::futures::{self, SinkExt};
use cosmic_time::once_cell::sync::Lazy; use cosmic_time::once_cell::sync::Lazy;
//use futures::channel::mpsc;
use libpulse_binding::{ use libpulse_binding::{
callbacks::ListResult, callbacks::ListResult,
context::{ context::{
@ -17,6 +18,9 @@ use libpulse_binding::{
proplist::Proplist, proplist::Proplist,
volume::ChannelVolumes, volume::ChannelVolumes,
}; };
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, Mutex}; use tokio::sync::{mpsc, Mutex};
pub static FROM_PULSE: Lazy<Mutex<Option<(mpsc::Receiver<Message>, mpsc::Sender<Message>)>>> = pub static FROM_PULSE: Lazy<Mutex<Option<(mpsc::Receiver<Message>, mpsc::Sender<Message>)>>> =
@ -386,6 +390,7 @@ struct PulseServer {
mainloop: Rc<RefCell<Mainloop>>, mainloop: Rc<RefCell<Mainloop>>,
context: Rc<RefCell<Context>>, context: Rc<RefCell<Context>>,
introspector: Introspector, introspector: Introspector,
last_playback: Instant,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -432,6 +437,7 @@ impl PulseServer {
mainloop, mainloop,
context, context,
introspector, introspector,
last_playback: Instant::now(),
}) })
} }
@ -631,6 +637,12 @@ impl PulseServer {
.introspector .introspector
.set_sink_volume_by_name(name, volume, None); .set_sink_volume_by_name(name, volume, None);
self.wait_for_result(op).ok(); self.wait_for_result(op).ok();
let now = Instant::now();
if now.duration_since(self.last_playback) > Duration::from_millis(250) {
self.last_playback = now;
crate::pipewire::play_audio_volume_change();
}
} }
fn set_source_volume_by_name(&mut self, name: &str, volume: &ChannelVolumes) { fn set_source_volume_by_name(&mut self, name: &str, volume: &ChannelVolumes) {