2017-06-26 07:08:37 +00:00
|
|
|
extern crate alsa;
|
2017-06-26 15:56:09 +00:00
|
|
|
extern crate std;
|
|
|
|
extern crate libc;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
2017-06-26 21:52:21 +00:00
|
|
|
use self::alsa::card::Card;
|
2017-06-26 15:56:09 +00:00
|
|
|
use self::alsa::mixer::{Mixer, Selem, Elem};
|
|
|
|
use alsa::mixer::SelemChannelId::*;
|
|
|
|
use std::iter::Map;
|
|
|
|
use self::libc::c_int;
|
2017-06-27 13:56:33 +00:00
|
|
|
use errors::*;
|
|
|
|
use std::convert::From;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_default_alsa_card() -> Card {
|
2017-06-26 15:56:09 +00:00
|
|
|
return get_alsa_card_by_id(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_alsa_card_by_id(index: c_int) -> Card {
|
|
|
|
return alsa::Card::new(index);
|
|
|
|
}
|
2017-06-26 07:08:37 +00:00
|
|
|
|
2017-06-26 15:56:09 +00:00
|
|
|
pub fn get_alsa_cards() -> alsa::card::Iter {
|
|
|
|
return alsa::card::Iter::new();
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
pub fn get_mixer(card: Card) -> Result<Mixer> {
|
|
|
|
return Mixer::new(&format!("hw:{}", card.get_index()), false).cherr();
|
|
|
|
}
|
2017-06-26 07:08:37 +00:00
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
pub fn get_selem(elem: Elem) -> Selem {
|
|
|
|
/* in the ALSA API, there are currently only simple elements,
|
|
|
|
* so this unwrap() should be safe.
|
|
|
|
*http://www.alsa-project.org/alsa-doc/alsa-lib/group___mixer.html#enum-members */
|
|
|
|
return Selem::new(elem).unwrap();
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 15:56:09 +00:00
|
|
|
pub fn get_selems(mixer: &Mixer) -> Map<alsa::mixer::Iter, fn(Elem) -> Selem> {
|
|
|
|
return mixer.iter().map(get_selem);
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
pub fn get_selem_by_name<'a>(mixer: &'a Mixer, name: String) -> Result<Selem> {
|
2017-06-26 15:56:09 +00:00
|
|
|
for selem in get_selems(mixer) {
|
2017-06-27 13:56:33 +00:00
|
|
|
let n = selem.get_id().get_name().map(|y| String::from(y))?;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
if n == name {
|
|
|
|
return Ok(selem);
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-27 13:56:33 +00:00
|
|
|
bail!("Not found a matching selem named {}", name);
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
pub fn get_vol(selem: Selem) -> Result<f64> {
|
2017-06-26 15:56:09 +00:00
|
|
|
let (min, max) = selem.get_playback_volume_range();
|
|
|
|
let volume = selem.get_playback_volume(FrontRight).map(|v| {
|
|
|
|
return ((v - min) as f64) / ((max - min) as f64) * 100.0;
|
|
|
|
});
|
|
|
|
|
2017-06-27 13:56:33 +00:00
|
|
|
return volume.cherr();
|
2017-06-26 15:56:09 +00:00
|
|
|
}
|