diff --git a/src/helpers.rs b/src/helpers.rs index 01c5400..8ce9a0d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -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(). */ diff --git a/src/types.rs b/src/types.rs index ea0f4f4..2ce5a72 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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 {