Clean up list_compatible_devices

Fix a typo and be a little more forgiving.
This commit is contained in:
JasonLG1979 2022-01-01 17:19:12 -06:00
parent 9202ec01d4
commit 8dfa00d66f

View file

@ -62,8 +62,8 @@ enum AlsaError {
#[error("<AlsaSink> PCM, {0}")] #[error("<AlsaSink> PCM, {0}")]
Pcm(alsa::Error), Pcm(alsa::Error),
#[error("<AlsaSink> Could Not Parse Ouput Name(s) and/or Description(s)")] #[error("<AlsaSink> Could Not Parse Output Name(s) and/or Description(s), {0}")]
Parsing, Parsing(alsa::Error),
#[error("<AlsaSink>")] #[error("<AlsaSink>")]
NotConnected, NotConnected,
@ -107,49 +107,58 @@ pub struct AlsaSink {
} }
fn list_compatible_devices() -> SinkResult<()> { fn list_compatible_devices() -> SinkResult<()> {
let i = HintIter::new_str(None, "pcm").map_err(AlsaError::Parsing)?;
println!("\n\n\tCompatible alsa device(s):\n"); println!("\n\n\tCompatible alsa device(s):\n");
println!("\t------------------------------------------------------\n"); println!("\t------------------------------------------------------\n");
let i = HintIter::new_str(None, "pcm").map_err(|_| AlsaError::Parsing)?;
for a in i { for a in i {
if let Some(Direction::Playback) = a.direction { if let Some(Direction::Playback) = a.direction {
let name = a.name.ok_or(AlsaError::Parsing)?; if let Some(name) = a.name {
let desc = a.desc.ok_or(AlsaError::Parsing)?; if let Ok(pcm) = PCM::new(&name, Direction::Playback, false) {
if let Ok(hwp) = HwParams::any(&pcm) {
// Only show devices that support
// 2 ch 44.1 Interleaved.
if let Ok(pcm) = PCM::new(&name, Direction::Playback, false) { if hwp.set_access(Access::RWInterleaved).is_ok()
if let Ok(hwp) = HwParams::any(&pcm) { && hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
// Only show devices that support && hwp.set_channels(NUM_CHANNELS as u32).is_ok()
// 2 ch 44.1 Interleaved. {
if hwp.set_access(Access::RWInterleaved).is_ok() let mut supported_formats = vec![];
&& hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
&& hwp.set_channels(NUM_CHANNELS as u32).is_ok()
{
println!("\tDevice:\n\n\t\t{}\n", name);
println!("\tDescription:\n\n\t\t{}\n", desc.replace("\n", "\n\t\t"));
let mut supported_formats = vec![]; for f in &[
AudioFormat::S16,
AudioFormat::S24,
AudioFormat::S24_3,
AudioFormat::S32,
AudioFormat::F32,
AudioFormat::F64,
] {
if hwp.test_format(Format::from(*f)).is_ok() {
supported_formats.push(format!("{:?}", f));
}
}
for f in &[ if !supported_formats.is_empty() {
AudioFormat::S16, println!("\tDevice:\n\n\t\t{}\n", name);
AudioFormat::S24,
AudioFormat::S24_3, println!(
AudioFormat::S32, "\tDescription:\n\n\t\t{}\n",
AudioFormat::F32, a.desc.unwrap_or_default().replace("\n", "\n\t\t")
AudioFormat::F64, );
] {
if hwp.test_format(Format::from(*f)).is_ok() { println!(
supported_formats.push(format!("{:?}", f)); "\tSupported Format(s):\n\n\t\t{}\n",
supported_formats.join(" ")
);
println!(
"\t------------------------------------------------------\n"
);
} }
} }
};
println!( }
"\tSupported Format(s):\n\n\t\t{}\n",
supported_formats.join(" ")
);
println!("\t------------------------------------------------------\n");
}
};
} }
} }
} }