Grab current sample rate from mixer control

This commit is contained in:
Hector Martin 2023-05-09 19:12:43 +09:00
parent 5c47bc3e94
commit b07584e1fd
2 changed files with 27 additions and 4 deletions

View file

@ -144,14 +144,29 @@ fn main() {
let io = pcm.io_i16().unwrap(); let io = pcm.io_i16().unwrap();
let hwp = pcm.hw_params_current().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 { loop {
// Block while we're reading into the buffer // Block while we're reading into the buffer
io.readi(&mut buf).unwrap(); 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() { for (idx, group) in groups.iter_mut() {
let gain = group let gain = group
.speakers .speakers

View file

@ -14,14 +14,14 @@ use crate::helpers;
The val field is created using a wrapper so that we can handle The val field is created using a wrapper so that we can handle
any errors. any errors.
*/ */
struct Elem { pub struct Elem {
elem_name: String, elem_name: String,
id: alsa::ctl::ElemId, id: alsa::ctl::ElemId,
val: alsa::ctl::ElemValue, val: alsa::ctl::ElemValue,
} }
impl Elem { 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 // CString::new() cannot borrow a String. We want name for the elem
// for error identification though, so it can't consume name directly. // for error identification though, so it can't consume name directly.
let borrow: String = name.clone(); let borrow: String = name.clone();
@ -43,6 +43,14 @@ impl Elem {
return new_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))
}
} }
/** /**