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 <marcan@marcan.st>
This commit is contained in:
Hector Martin 2023-05-09 19:12:02 +09:00
parent 21b3e49456
commit 5c47bc3e94
2 changed files with 10 additions and 3 deletions

View file

@ -40,11 +40,19 @@ pub fn open_card(card: &str) -> alsa::ctl::Ctl {
return ctldev; 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 pcm = alsa::pcm::PCM::new(dev, alsa::Direction::Capture, false).unwrap();
{ {
let params = alsa::pcm::HwParams::any(&pcm).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_channels(chans).unwrap();
params params
.set_rate(sample_rate, alsa::ValueOr::Nearest) .set_rate(sample_rate, alsa::ValueOr::Nearest)

View file

@ -137,8 +137,7 @@ fn main() {
let pcm_name = format!("{},{}", device, globals.visense_pcm); let pcm_name = format!("{},{}", device, globals.visense_pcm);
// Set up PCM to buffer in V/ISENSE // Set up PCM to buffer in V/ISENSE
let pcm: alsa::pcm::PCM = let pcm: alsa::pcm::PCM = helpers::open_pcm(&pcm_name, globals.channels.try_into().unwrap(), 0);
helpers::open_pcm(&pcm_name, globals.channels.try_into().unwrap(), 48000);
let mut buf = Vec::new(); let mut buf = Vec::new();
buf.resize(globals.period * globals.channels, 0i16); buf.resize(globals.period * globals.channels, 0i16);