pnmixer-rust/src/audio.rs

338 lines
8.0 KiB
Rust
Raw Normal View History

2017-07-04 19:15:11 +00:00
use alsa_card::*;
2017-06-29 12:25:40 +00:00
use errors::*;
2017-06-29 21:35:39 +00:00
use glib;
2017-07-01 23:35:12 +00:00
use std::cell::Cell;
2017-07-02 16:11:56 +00:00
use std::cell::Ref;
2017-06-30 19:10:33 +00:00
use std::cell::RefCell;
2017-06-30 23:56:32 +00:00
use std::f64;
2017-07-04 19:15:11 +00:00
use std::rc::Rc;
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-07-03 21:38:39 +00:00
#[derive(Clone, Copy, Debug)]
pub enum VolLevel {
Muted,
Low,
Medium,
High,
Off,
}
2017-06-30 20:36:34 +00:00
#[derive(Clone, Copy, Debug)]
pub enum AudioUser {
2017-07-03 21:38:39 +00:00
Unknown,
Popup,
TrayIcon,
Hotkeys,
2017-07-04 19:15:11 +00:00
PrefsWindow,
2017-06-30 20:36:34 +00:00
}
#[derive(Clone, Copy, Debug)]
pub enum AudioSignal {
2017-07-03 21:38:39 +00:00
NoCard,
CardInitialized,
CardCleanedUp,
CardDisconnected,
CardError,
ValuesChanged,
2017-06-30 20:36:34 +00:00
}
2017-07-02 16:11:56 +00:00
#[derive(Clone)]
pub struct Handlers {
inner: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
}
impl Handlers {
fn new() -> Handlers {
return Handlers { inner: Rc::new(RefCell::new(vec![])) };
}
fn borrow(&self) -> Ref<Vec<Box<Fn(AudioSignal, AudioUser)>>> {
return self.inner.borrow();
}
fn add_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
self.inner.borrow_mut().push(cb);
}
}
2017-07-01 22:03:21 +00:00
pub struct Audio {
2017-06-29 12:25:40 +00:00
_cannot_construct: (),
2017-07-01 22:03:21 +00:00
pub acard: RefCell<Box<AlsaCard>>,
2017-07-01 23:45:30 +00:00
pub last_action_timestamp: Rc<RefCell<i64>>,
2017-07-02 16:11:56 +00:00
pub handlers: Handlers,
2017-07-01 23:35:12 +00:00
pub scroll_step: Cell<u32>,
2017-06-29 12:25:40 +00:00
}
2017-06-29 12:55:07 +00:00
2017-07-01 22:03:21 +00:00
impl Audio {
2017-07-06 14:53:19 +00:00
pub fn new(
card_name: Option<String>,
elem_name: Option<String>,
) -> Result<Audio> {
2017-07-01 22:03:21 +00:00
2017-07-02 16:11:56 +00:00
let handlers = Handlers::new();
2017-07-01 23:45:30 +00:00
let last_action_timestamp = Rc::new(RefCell::new(0));
2017-07-01 22:03:21 +00:00
2017-07-02 16:11:56 +00:00
let cb = {
let myhandler = handlers.clone();
let ts = last_action_timestamp.clone();
Rc::new(move |event| {
2017-07-06 14:53:19 +00:00
on_alsa_event(&mut *ts.borrow_mut(), &myhandler.borrow(), event)
})
2017-07-02 16:11:56 +00:00
};
2017-07-01 22:03:21 +00:00
let audio = Audio {
_cannot_construct: (),
acard: RefCell::new(AlsaCard::new(card_name, elem_name, cb)?),
last_action_timestamp: last_action_timestamp.clone(),
handlers: handlers.clone(),
2017-07-01 23:35:12 +00:00
scroll_step: Cell::new(5),
2017-06-29 12:25:40 +00:00
};
2017-06-28 12:55:38 +00:00
2017-07-01 22:03:21 +00:00
return Ok(audio);
}
2017-06-29 12:55:07 +00:00
2017-06-30 23:56:32 +00:00
2017-07-06 14:53:19 +00:00
pub fn switch_acard(
&self,
card_name: Option<String>,
elem_name: Option<String>,
user: AudioUser,
) -> Result<()> {
2017-07-01 23:35:12 +00:00
debug!("Switching cards");
2017-07-06 14:53:19 +00:00
debug!(
"Old card name: {}",
self.acard.borrow().card_name().unwrap()
);
debug!(
"Old chan name: {}",
self.acard.borrow().chan_name().unwrap()
);
let cb = self.acard.borrow().cb.clone();
2017-07-02 16:11:56 +00:00
{
let mut ac = self.acard.borrow_mut();
*ac = AlsaCard::new(card_name, elem_name, cb)?;
}
2017-07-06 14:53:19 +00:00
debug!(
"Old card name: {}",
self.acard.borrow().card_name().unwrap()
);
debug!(
"Old chan name: {}",
self.acard.borrow().chan_name().unwrap()
);
invoke_handlers(
&self.handlers.borrow(),
AudioSignal::CardInitialized,
user,
);
2017-07-04 19:15:11 +00:00
2017-07-01 22:03:21 +00:00
return Ok(());
2017-06-29 12:25:40 +00:00
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn vol(&self) -> Result<f64> {
2017-07-01 22:03:21 +00:00
return self.acard.borrow().get_vol();
2017-06-29 12:25:40 +00:00
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-07-03 21:38:39 +00:00
pub fn vol_level(&self) -> VolLevel {
let muted = self.get_mute().unwrap_or(false);
if muted {
return VolLevel::Muted;
}
let cur_vol = try_r!(self.vol(), VolLevel::Muted);
match cur_vol {
0. => return VolLevel::Off,
0.0...33.0 => return VolLevel::Low,
0.0...66.0 => return VolLevel::Medium,
0.0...100.0 => return VolLevel::High,
_ => return VolLevel::Off,
}
}
2017-06-29 21:35:39 +00:00
pub fn set_vol(&self, new_vol: f64, user: AudioUser) -> Result<()> {
2017-06-30 15:24:26 +00:00
{
let mut rc = self.last_action_timestamp.borrow_mut();
*rc = glib::get_monotonic_time();
}
2017-06-30 20:08:08 +00:00
2017-07-06 14:53:19 +00:00
debug!(
"Setting vol on card {:?} and chan {:?} to {:?} by user {:?}",
self.acard.borrow().card_name().unwrap(),
self.acard.borrow().chan_name().unwrap(),
new_vol,
user
);
self.acard.borrow().set_vol(new_vol)?;
invoke_handlers(
&self.handlers.borrow(),
AudioSignal::ValuesChanged,
user,
);
2017-07-03 21:38:39 +00:00
return Ok(());
2017-06-29 12:25:40 +00:00
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-30 23:56:32 +00:00
pub fn increase_vol(&self, user: AudioUser) -> Result<()> {
{
let mut rc = self.last_action_timestamp.borrow_mut();
*rc = glib::get_monotonic_time();
}
let old_vol = self.vol()?;
2017-07-01 23:35:12 +00:00
let new_vol = f64::ceil(old_vol + (self.scroll_step.get() as f64));
2017-06-30 23:56:32 +00:00
2017-07-06 14:53:19 +00:00
debug!(
"Increase vol on card {:?} and chan {:?} by {:?} to {:?}",
self.acard.borrow().card_name().unwrap(),
self.acard.borrow().chan_name().unwrap(),
(new_vol - old_vol),
new_vol
);
2017-06-30 23:56:32 +00:00
2017-07-03 21:38:39 +00:00
self.set_vol(new_vol, user)?;
2017-07-06 14:53:19 +00:00
invoke_handlers(
&self.handlers.borrow(),
AudioSignal::ValuesChanged,
user,
);
2017-07-03 21:38:39 +00:00
return Ok(());
2017-06-30 23:56:32 +00:00
}
pub fn decrease_vol(&self, user: AudioUser) -> Result<()> {
{
let mut rc = self.last_action_timestamp.borrow_mut();
*rc = glib::get_monotonic_time();
}
let old_vol = self.vol()?;
2017-07-01 23:35:12 +00:00
let new_vol = old_vol - (self.scroll_step.get() as f64);
2017-06-30 23:56:32 +00:00
2017-07-06 14:53:19 +00:00
debug!(
"Decrease vol on card {:?} and chan {:?} by {:?} to {:?}",
self.acard.borrow().card_name().unwrap(),
self.acard.borrow().chan_name().unwrap(),
(new_vol - old_vol),
new_vol
);
2017-06-30 23:56:32 +00:00
2017-07-03 21:38:39 +00:00
self.set_vol(new_vol, user)?;
2017-07-06 14:53:19 +00:00
invoke_handlers(
&self.handlers.borrow(),
AudioSignal::ValuesChanged,
user,
);
2017-07-03 21:38:39 +00:00
return Ok(());
2017-06-30 23:56:32 +00:00
}
2017-06-29 12:25:40 +00:00
pub fn has_mute(&self) -> bool {
2017-07-01 22:03:21 +00:00
return self.acard.borrow().has_mute();
2017-06-29 12:25:40 +00:00
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn get_mute(&self) -> Result<bool> {
2017-07-01 22:03:21 +00:00
return self.acard.borrow().get_mute();
2017-06-26 07:08:37 +00:00
}
2017-06-29 12:55:07 +00:00
2017-06-29 21:35:39 +00:00
pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
let mut rc = self.last_action_timestamp.borrow_mut();
2017-06-30 15:24:26 +00:00
*rc = glib::get_monotonic_time();
2017-07-03 21:38:39 +00:00
2017-07-06 14:53:19 +00:00
debug!(
"Setting mute to {} on card {:?} and chan {:?} by user {:?}",
mute,
self.acard.borrow().card_name().unwrap(),
self.acard.borrow().chan_name().unwrap(),
user
);
self.acard.borrow().set_mute(mute)?;
invoke_handlers(
&self.handlers.borrow(),
AudioSignal::ValuesChanged,
user,
);
2017-07-03 21:38:39 +00:00
return Ok(());
2017-07-01 22:03:21 +00:00
}
2017-07-03 21:52:41 +00:00
pub fn toggle_mute(&self, user: AudioUser) -> Result<()> {
let muted = self.get_mute()?;
return self.set_mute(!muted, user);
}
2017-07-01 22:03:21 +00:00
pub fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
2017-07-02 16:11:56 +00:00
self.handlers.add_handler(cb);
2017-07-01 22:03:21 +00:00
}
2017-07-02 16:11:56 +00:00
}
2017-07-01 22:03:21 +00:00
2017-07-06 14:53:19 +00:00
fn invoke_handlers(
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
signal: AudioSignal,
user: AudioUser,
) {
debug!(
"Invoking handlers for signal {:?} by user {:?}",
signal,
user
);
2017-07-02 16:11:56 +00:00
for handler in handlers {
let unboxed = handler.as_ref();
unboxed(signal, user);
2017-06-29 12:25:40 +00:00
}
2017-07-02 16:11:56 +00:00
}
2017-06-29 21:35:39 +00:00
2017-07-06 14:53:19 +00:00
fn on_alsa_event(
last_action_timestamp: &mut i64,
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
alsa_event: AlsaEvent,
) {
2017-07-02 16:11:56 +00:00
let last: i64 = *last_action_timestamp;
2017-06-30 19:10:33 +00:00
2017-07-02 16:11:56 +00:00
if last != 0 {
let now: i64 = glib::get_monotonic_time();
let delay: i64 = now - last;
if delay < 1000000 {
return;
2017-06-29 21:35:39 +00:00
}
2017-07-02 16:11:56 +00:00
debug!("Discarding last time stamp, too old");
*last_action_timestamp = 0;
}
2017-06-29 21:35:39 +00:00
2017-07-02 16:11:56 +00:00
/* external change */
match alsa_event {
// TODO: invoke handlers with AudioUserUnknown
AlsaEvent::AlsaCardError => debug!("AlsaCardError"),
AlsaEvent::AlsaCardDiconnected => debug!("AlsaCardDiconnected"),
AlsaEvent::AlsaCardValuesChanged => {
debug!("AlsaCardValuesChanged");
2017-07-06 14:53:19 +00:00
invoke_handlers(
handlers,
self::AudioSignal::ValuesChanged,
self::AudioUser::Unknown,
);
2017-07-02 16:11:56 +00:00
}
e => warn!("Unhandled alsa event: {:?}", e),
2017-06-29 21:35:39 +00:00
}
2017-07-02 16:11:56 +00:00
2017-06-28 15:53:19 +00:00
}