Merge pull request #922 from JasonLG1979/cleanup_list_compatible_devices

Clean up `list_compatible_devices`
This commit is contained in:
Roderick van Domburg 2022-01-03 22:32:22 +01:00 committed by GitHub
commit 92b8476645
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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,27 +107,23 @@ 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(pcm) = PCM::new(&name, Direction::Playback, false) {
if let Ok(hwp) = HwParams::any(&pcm) { if let Ok(hwp) = HwParams::any(&pcm) {
// Only show devices that support // Only show devices that support
// 2 ch 44.1 Interleaved. // 2 ch 44.1 Interleaved.
if hwp.set_access(Access::RWInterleaved).is_ok() if hwp.set_access(Access::RWInterleaved).is_ok()
&& hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok() && hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
&& hwp.set_channels(NUM_CHANNELS as u32).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![]; let mut supported_formats = vec![];
for f in &[ for f in &[
@ -143,16 +139,29 @@ fn list_compatible_devices() -> SinkResult<()> {
} }
} }
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!( println!(
"\tSupported Format(s):\n\n\t\t{}\n", "\tSupported Format(s):\n\n\t\t{}\n",
supported_formats.join(" ") supported_formats.join(" ")
); );
println!("\t------------------------------------------------------\n");
println!(
"\t------------------------------------------------------\n"
);
}
} }
}; };
} }
} }
} }
}
Ok(()) Ok(())
} }