2017-07-14 15:23:42 +00:00
|
|
|
//! Alsa audio helper functions.
|
|
|
|
//!
|
|
|
|
//! This mod wraps around a few low-level alsa functions and abstracts
|
|
|
|
//! out the details we don't care about.
|
|
|
|
|
|
|
|
|
2017-07-19 10:12:08 +00:00
|
|
|
use alsa_lib::card::Card;
|
|
|
|
use alsa_lib::mixer::{Mixer, Selem, Elem};
|
|
|
|
use alsa_lib;
|
2017-06-26 07:08:37 +00:00
|
|
|
use errors::*;
|
|
|
|
use libc::c_int;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the default alsa card. This is the one with the ID `0`.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_default_alsa_card() -> Card {
|
|
|
|
return get_alsa_card_by_id(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get an alsa card corresponding to the given ID.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_alsa_card_by_id(index: c_int) -> Card {
|
|
|
|
return Card::new(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get all available alsa cards.
|
2017-07-19 10:12:08 +00:00
|
|
|
pub fn get_alsa_cards() -> alsa_lib::card::Iter {
|
|
|
|
return alsa_lib::card::Iter::new();
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the first playable alsa card.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_first_playable_alsa_card() -> Result<Card> {
|
|
|
|
for m_card in get_alsa_cards() {
|
|
|
|
match m_card {
|
|
|
|
Ok(card) => {
|
|
|
|
if alsa_card_has_playable_selem(&card) {
|
|
|
|
return Ok(card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("No playable alsa card found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the names of all playable alsa cards.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_playable_alsa_card_names() -> Vec<String> {
|
|
|
|
let mut vec = vec![];
|
|
|
|
for m_card in get_alsa_cards() {
|
|
|
|
match m_card {
|
|
|
|
Ok(card) => {
|
|
|
|
if alsa_card_has_playable_selem(&card) {
|
|
|
|
let m_name = card.get_name();
|
|
|
|
if m_name.is_ok() {
|
|
|
|
vec.push(m_name.unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get an alsa card by the given name.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_alsa_card_by_name(name: String) -> Result<Card> {
|
|
|
|
for r_card in get_alsa_cards() {
|
|
|
|
let card = r_card?;
|
|
|
|
let card_name = card.get_name()?;
|
|
|
|
if name == card_name {
|
|
|
|
return Ok(card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bail!("Not found a matching card named {}", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Check whether the given alsa card as a playable `Selem`.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn alsa_card_has_playable_selem(card: &Card) -> bool {
|
|
|
|
let mixer = try_wr!(get_mixer(&card), false);
|
|
|
|
for selem in get_playable_selems(&mixer) {
|
|
|
|
if selem_is_playable(&selem) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the `Mixer` for the given alsa card.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_mixer(card: &Card) -> Result<Mixer> {
|
2017-09-01 22:54:32 +00:00
|
|
|
return Ok(Mixer::new(&format!("hw:{}", card.get_index()), false)?);
|
2017-06-26 07:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the `Selem` from the given `Elem`.
|
2017-06-26 07:08:37 +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-07-14 15:23:42 +00:00
|
|
|
/// Get all playable `Selem`s.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_playable_selems(mixer: &Mixer) -> Vec<Selem> {
|
|
|
|
let mut v = vec![];
|
|
|
|
for s in mixer.iter().map(get_selem).filter(selem_is_playable) {
|
|
|
|
v.push(s);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the first playable `Selem`.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_first_playable_selem(mixer: &Mixer) -> Result<Selem> {
|
|
|
|
for s in mixer.iter().map(get_selem).filter(selem_is_playable) {
|
|
|
|
return Ok(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
bail!("No playable Selem found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get the names from all playable `Selem`s.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn get_playable_selem_names(mixer: &Mixer) -> Vec<String> {
|
|
|
|
let mut vec = vec![];
|
|
|
|
for selem in get_playable_selems(mixer) {
|
|
|
|
let n = selem.get_id().get_name().map(|y| String::from(y));
|
|
|
|
match n {
|
|
|
|
Ok(name) => vec.push(name),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Get a playable `Selem` by the given name.
|
2017-07-19 10:12:08 +00:00
|
|
|
pub fn get_playable_selem_by_name(
|
|
|
|
mixer: &Mixer,
|
|
|
|
name: String,
|
|
|
|
) -> Result<Selem> {
|
2017-06-26 07:08:37 +00:00
|
|
|
for selem in get_playable_selems(mixer) {
|
2017-07-19 10:12:08 +00:00
|
|
|
let n = selem.get_id().get_name().map(|y| String::from(y))?;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
|
|
|
if n == name {
|
|
|
|
return Ok(selem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bail!("Not found a matching playable selem named {}", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-14 15:23:42 +00:00
|
|
|
/// Check whether the given `Selem` is playable.
|
2017-06-26 07:08:37 +00:00
|
|
|
pub fn selem_is_playable(selem: &Selem) -> bool {
|
|
|
|
return selem.has_playback_volume();
|
|
|
|
}
|