From 5c47bc3e9449b4774cfb98887bd069d2f662871c Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Tue, 9 May 2023 19:12:02 +0900 Subject: [PATCH] Automatically select the sample rate if zero This allows the sense PCM to be opened at the correct current rate if playback is active, or otherwise arbitrarily picks the lowest rate. Signed-off-by: Hector Martin --- src/helpers.rs | 10 +++++++++- src/main.rs | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 77ddf91..d139701 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -40,11 +40,19 @@ pub fn open_card(card: &str) -> alsa::ctl::Ctl { return ctldev; } -pub fn open_pcm(dev: &str, chans: u32, sample_rate: u32) -> alsa::pcm::PCM { +pub fn open_pcm(dev: &str, chans: u32, mut sample_rate: u32) -> alsa::pcm::PCM { let pcm = alsa::pcm::PCM::new(dev, alsa::Direction::Capture, false).unwrap(); { let params = alsa::pcm::HwParams::any(&pcm).unwrap(); + let rate_max = params.get_rate_max().unwrap(); + let rate_min = params.get_rate_min().unwrap(); + println!("PCM rate: {}..{}", rate_min, rate_max); + + if sample_rate == 0 { + sample_rate = rate_min; + } + params.set_channels(chans).unwrap(); params .set_rate(sample_rate, alsa::ValueOr::Nearest) diff --git a/src/main.rs b/src/main.rs index 3a673f7..6ecf795 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,8 +137,7 @@ fn main() { let pcm_name = format!("{},{}", device, globals.visense_pcm); // Set up PCM to buffer in V/ISENSE - let pcm: alsa::pcm::PCM = - helpers::open_pcm(&pcm_name, globals.channels.try_into().unwrap(), 48000); + let pcm: alsa::pcm::PCM = helpers::open_pcm(&pcm_name, globals.channels.try_into().unwrap(), 0); let mut buf = Vec::new(); buf.resize(globals.period * globals.channels, 0i16);