librespot/playback/src/mixer/alsamixer.rs

86 lines
2.4 KiB
Rust
Raw Normal View History

2018-03-20 15:32:43 +00:00
use super::AudioFilter;
2018-09-01 00:42:50 +00:00
use super::Mixer;
use std::env;
use std::error::Error;
2018-03-20 15:32:43 +00:00
use alsa;
#[derive(Clone)]
pub struct AlsaMixer {
2018-03-21 21:18:37 +00:00
card: String,
mixer: String,
2018-09-01 00:42:50 +00:00
index: u32,
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<Error>> {
let mixer = alsa::mixer::Mixer::new(&self.card, false)?;
let sid = alsa::mixer::SelemId::new(&*self.mixer, self.index);
let selem = mixer.find_selem(&sid).expect("Coundn't find SelemId");
let (min, max) = selem.get_playback_volume_range();
let cur_vol = selem.get_playback_volume(alsa::mixer::SelemChannelId::mono()).expect("Couldn't get current volume");
let range = (max - min) as f64;
let new_vol:u16;
if let Some(vol) = set_volume {
let alsa_volume:i64 = ((vol as f64 / 0xFFFF as f64) * range) as i64 + min;
debug!("Maping volume {:?} [u16] ->> Alsa {:?} [i64]",vol,alsa_volume);
selem.set_playback_volume_all(alsa_volume).expect("Couldn't set alsa volume");
new_vol = vol; // Meh
} else {
new_vol = (((cur_vol - min) as f64 / range) * 0xFFFF as f64) as u16;
debug!("Maping volume {:?} [u16] <<- Alsa {:?} [i64]",new_vol, cur_vol);
}
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(device: Option<String>) -> AlsaMixer {
2018-09-01 00:42:50 +00:00
let card = env::var("LIBRESPOT_CARD").unwrap_or(device.unwrap_or(String::from("default")));
let mixer = env::var("LIBRESPOT_MIXER").unwrap_or(String::from("PCM"));
let index: u32 = 0;
info!(
"Setting up new mixer: card:{} mixer:{} index:{}",
card, mixer, index
);
2018-03-20 15:32:43 +00:00
AlsaMixer {
2018-03-21 21:18:37 +00:00
card: card,
mixer: mixer,
2018-09-01 00:42:50 +00:00
index: index,
2018-03-20 15:32:43 +00:00
}
}
fn start(&self) {
}
fn stop(&self) {
}
fn volume(&self) -> u16 {
2018-09-01 00:42:50 +00:00
match self.map_volume(None){
Ok(vol) => vol,
Err(e) => {
error!("Error getting volume for <{}>, {:?}",self.card, e);
0 }
}
2018-03-20 15:32:43 +00:00
}
fn set_volume(&self, volume: u16) {
2018-09-01 00:42:50 +00:00
match self.map_volume(Some(volume)){
Ok(_) => (),
Err(e) => error!("Error setting volume for <{}>, {:?}",self.card, e),
}
2018-03-20 15:32:43 +00:00
}
fn get_audio_filter(&self) -> Option<Box<AudioFilter + Send>> {
None
}
}