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}")]
Pcm(alsa::Error),
#[error("<AlsaSink> Could Not Parse Ouput Name(s) and/or Description(s)")]
Parsing,
#[error("<AlsaSink> Could Not Parse Output Name(s) and/or Description(s), {0}")]
Parsing(alsa::Error),
#[error("<AlsaSink>")]
NotConnected,
@ -107,49 +107,58 @@ pub struct AlsaSink {
}
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!("\t------------------------------------------------------\n");
let i = HintIter::new_str(None, "pcm").map_err(|_| AlsaError::Parsing)?;
for a in i {
if let Some(Direction::Playback) = a.direction {
let name = a.name.ok_or(AlsaError::Parsing)?;
let desc = a.desc.ok_or(AlsaError::Parsing)?;
if let Some(name) = a.name {
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 let Ok(hwp) = HwParams::any(&pcm) {
// Only show devices that support
// 2 ch 44.1 Interleaved.
if hwp.set_access(Access::RWInterleaved).is_ok()
&& 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"));
if hwp.set_access(Access::RWInterleaved).is_ok()
&& hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
&& hwp.set_channels(NUM_CHANNELS as u32).is_ok()
{
let mut supported_formats = vec![];
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 &[
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));
if !supported_formats.is_empty() {
println!("\tDevice:\n\n\t\t{}\n", name);
println!(
"\tDescription:\n\n\t\t{}\n",
a.desc.unwrap_or_default().replace("\n", "\n\t\t")
);
println!(
"\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");
}
};
};
}
}
}
}