types: Always set amp gain to max on startup

The kernel should clamp this to the correct value. We still read it back
later, to make sure our amp output calculations are correct.

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2023-10-29 00:11:17 +09:00
parent 071830b040
commit f8d9c55cbb
2 changed files with 40 additions and 2 deletions

View file

@ -131,6 +131,26 @@ pub fn write_ev(card: &alsa::ctl::Ctl, ev: &alsa::ctl::ElemValue, name: &str) {
};
}
/**
Wrapper for alsa::ctl::Ctl::elem_write().
*/
pub fn get_range_db(
card: &alsa::ctl::Ctl,
el: &alsa::ctl::ElemId,
name: &str,
) -> (MilliBel, MilliBel) {
match card.get_db_range(el) {
// alsa:Result<()>
Ok(val) => val,
Err(e) => {
panic!(
"Could not get elem db range {}. alsa-lib error: {:?}",
name, e
);
}
}
}
/**
Wrapper for alsa::ctl::Ctl::elem_read().
*/

View file

@ -107,7 +107,7 @@ impl Mixer {
helpers::read_ev(card, &mut vs.val, &vs.elem_name);
assert!(vs.val.get_boolean(0).unwrap());
Mixer {
let mut ret = Mixer {
drv: name.to_owned(),
level: Elem::new(
prefix.clone() + &globals.ctl_volume,
@ -119,7 +119,25 @@ impl Mixer {
card,
alsa::ctl::ElemType::Integer,
),
}
};
/*
* Set amp gain to max available (kernel should've clamped).
* alsa-rs only has bindings for range in dB, so we go through
* that.
*/
let (_min, max) =
helpers::get_range_db(card, &mut ret.amp_gain.id, &ret.amp_gain.elem_name);
let max_int = card
.convert_from_db(&mut ret.amp_gain.id, max, alsa::Round::Floor)
.unwrap();
ret.amp_gain.val.set_integer(0, max_int.try_into().unwrap());
helpers::write_ev(card, &ret.amp_gain.val, &ret.amp_gain.elem_name);
ret
}
fn get_amp_gain(&mut self, card: &Ctl) -> f32 {