diff --git a/src/main.rs b/src/main.rs index 6ecf795..1ac16e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -144,14 +144,29 @@ fn main() { let io = pcm.io_i16().unwrap(); let hwp = pcm.hw_params_current().unwrap(); - let sample_rate = hwp.get_rate().unwrap(); - info!("Sample rate: {}", sample_rate); + let mut sample_rate_elem = types::Elem::new( + "Speaker Sample Rate".to_string(), + &ctl, + alsa::ctl::ElemType::Integer, + ); + let mut sample_rate = sample_rate_elem.read_int(&ctl); loop { // Block while we're reading into the buffer io.readi(&mut buf).unwrap(); + let cur_sample_rate = sample_rate_elem.read_int(&ctl); + + if cur_sample_rate != 0 { + sample_rate = cur_sample_rate; + } + + if (sample_rate == 0) { + panic!("Invalid sample rate"); + } + + info!("Sample rate: {}", sample_rate); for (idx, group) in groups.iter_mut() { let gain = group .speakers diff --git a/src/types.rs b/src/types.rs index 2571b1e..704f416 100644 --- a/src/types.rs +++ b/src/types.rs @@ -14,14 +14,14 @@ use crate::helpers; The val field is created using a wrapper so that we can handle any errors. */ -struct Elem { +pub struct Elem { elem_name: String, id: alsa::ctl::ElemId, val: alsa::ctl::ElemValue, } impl Elem { - fn new(name: String, card: &Ctl, t: alsa::ctl::ElemType) -> Elem { + pub fn new(name: String, card: &Ctl, t: alsa::ctl::ElemType) -> Elem { // CString::new() cannot borrow a String. We want name for the elem // for error identification though, so it can't consume name directly. let borrow: String = name.clone(); @@ -43,6 +43,14 @@ impl Elem { return new_elem; } + + pub fn read_int(&mut self, card: &Ctl) -> i32 { + helpers::read_ev(card, &mut self.val, &self.elem_name); + + self.val + .get_integer(0) + .expect(&format!("Could not read {}", self.elem_name)) + } } /**