librespot/playback/src/mixer/alsamixer.rs

83 lines
2.4 KiB
Rust
Raw Normal View History

2018-03-20 15:32:43 +00:00
use super::AudioFilter;
use super::{Mixer, MixerConfig};
2018-09-01 00:42:50 +00:00
use std::error::Error;
2018-03-20 15:32:43 +00:00
use alsa;
#[derive(Clone)]
pub struct AlsaMixer {
config: MixerConfig,
2018-03-20 15:32:43 +00:00
}
2018-09-01 00:42:50 +00:00
impl AlsaMixer {
fn map_volume(&self, set_volume: Option<u16>) -> Result<(u16), Box<dyn Error>> {
2018-10-16 14:46:26 +00:00
let mixer = alsa::mixer::Mixer::new(&self.config.card, false)?;
let sid = alsa::mixer::SelemId::new(&*self.config.mixer, self.config.index);
2018-09-01 00:42:50 +00:00
let selem = mixer.find_selem(&sid).expect(
format!(
"Couldn't find simple mixer control for {}",
self.config.mixer
)
.as_str(),
);
2018-09-01 00:42:50 +00:00
let (min, max) = selem.get_playback_volume_range();
let range = (max - min) as f64;
2018-10-16 14:46:26 +00:00
let new_vol: u16;
2018-09-01 00:42:50 +00:00
if let Some(vol) = set_volume {
2018-10-16 14:46:26 +00:00
let alsa_volume: i64 = ((vol as f64 / 0xFFFF as f64) * range) as i64 + min;
2018-11-01 16:40:42 +00:00
debug!("Mapping volume {:?} ->> alsa {:?}", vol, alsa_volume);
2018-10-16 14:46:26 +00:00
selem
.set_playback_volume_all(alsa_volume)
.expect("Couldn't set alsa volume");
new_vol = vol;
2018-09-01 00:42:50 +00:00
} else {
2018-11-01 16:40:42 +00:00
let cur_vol = selem
.get_playback_volume(alsa::mixer::SelemChannelId::mono())
.expect("Couldn't get current volume");
2018-10-16 14:46:26 +00:00
new_vol = (((cur_vol - min) as f64 / range) * 0xFFFF as f64) as u16;
2018-11-01 16:40:42 +00:00
debug!("Mapping volume {:?} <<- alsa {:?}", new_vol, cur_vol);
2018-09-01 00:42:50 +00:00
}
Ok(new_vol)
}
}
2018-03-21 21:18:37 +00:00
2018-03-20 15:32:43 +00:00
impl Mixer for AlsaMixer {
fn open(config: Option<MixerConfig>) -> AlsaMixer {
let config = config.unwrap_or_default();
2018-09-01 00:42:50 +00:00
info!(
"Setting up new mixer: card:{} mixer:{} index:{}",
config.card, config.mixer, config.index
2018-09-01 00:42:50 +00:00
);
AlsaMixer { config: config }
2018-03-20 15:32:43 +00:00
}
2018-10-16 14:46:26 +00:00
fn start(&self) {}
2018-03-20 15:32:43 +00:00
2018-10-16 14:46:26 +00:00
fn stop(&self) {}
2018-03-20 15:32:43 +00:00
fn volume(&self) -> u16 {
2018-10-16 14:46:26 +00:00
match self.map_volume(None) {
Ok(vol) => vol,
Err(e) => {
error!("Error getting volume for <{}>, {:?}", self.config.card, e);
0
}
2018-09-01 00:42:50 +00:00
}
2018-03-20 15:32:43 +00:00
}
fn set_volume(&self, volume: u16) {
2018-10-16 14:46:26 +00:00
match self.map_volume(Some(volume)) {
Ok(_) => (),
Err(e) => error!("Error setting volume for <{}>, {:?}", self.config.card, e),
2018-09-01 00:42:50 +00:00
}
2018-03-20 15:32:43 +00:00
}
fn get_audio_filter(&self) -> Option<Box<dyn AudioFilter + Send>> {
2018-03-20 15:32:43 +00:00
None
}
}