Skip to content

Commit 92b8476

Browse files
authored
Merge pull request #922 from JasonLG1979/cleanup_list_compatible_devices
Clean up `list_compatible_devices`
2 parents 9202ec0 + 8dfa00d commit 92b8476

1 file changed

Lines changed: 47 additions & 38 deletions

File tree

playback/src/audio_backend/alsa.rs

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ enum AlsaError {
6262
#[error("<AlsaSink> PCM, {0}")]
6363
Pcm(alsa::Error),
6464

65-
#[error("<AlsaSink> Could Not Parse Ouput Name(s) and/or Description(s)")]
66-
Parsing,
65+
#[error("<AlsaSink> Could Not Parse Output Name(s) and/or Description(s), {0}")]
66+
Parsing(alsa::Error),
6767

6868
#[error("<AlsaSink>")]
6969
NotConnected,
@@ -107,49 +107,58 @@ pub struct AlsaSink {
107107
}
108108

109109
fn list_compatible_devices() -> SinkResult<()> {
110+
let i = HintIter::new_str(None, "pcm").map_err(AlsaError::Parsing)?;
111+
110112
println!("\n\n\tCompatible alsa device(s):\n");
111113
println!("\t------------------------------------------------------\n");
112114

113-
let i = HintIter::new_str(None, "pcm").map_err(|_| AlsaError::Parsing)?;
114-
115115
for a in i {
116116
if let Some(Direction::Playback) = a.direction {
117-
let name = a.name.ok_or(AlsaError::Parsing)?;
118-
let desc = a.desc.ok_or(AlsaError::Parsing)?;
119-
120-
if let Ok(pcm) = PCM::new(&name, Direction::Playback, false) {
121-
if let Ok(hwp) = HwParams::any(&pcm) {
122-
// Only show devices that support
123-
// 2 ch 44.1 Interleaved.
124-
if hwp.set_access(Access::RWInterleaved).is_ok()
125-
&& hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
126-
&& hwp.set_channels(NUM_CHANNELS as u32).is_ok()
127-
{
128-
println!("\tDevice:\n\n\t\t{}\n", name);
129-
println!("\tDescription:\n\n\t\t{}\n", desc.replace("\n", "\n\t\t"));
130-
131-
let mut supported_formats = vec![];
132-
133-
for f in &[
134-
AudioFormat::S16,
135-
AudioFormat::S24,
136-
AudioFormat::S24_3,
137-
AudioFormat::S32,
138-
AudioFormat::F32,
139-
AudioFormat::F64,
140-
] {
141-
if hwp.test_format(Format::from(*f)).is_ok() {
142-
supported_formats.push(format!("{:?}", f));
117+
if let Some(name) = a.name {
118+
if let Ok(pcm) = PCM::new(&name, Direction::Playback, false) {
119+
if let Ok(hwp) = HwParams::any(&pcm) {
120+
// Only show devices that support
121+
// 2 ch 44.1 Interleaved.
122+
123+
if hwp.set_access(Access::RWInterleaved).is_ok()
124+
&& hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok()
125+
&& hwp.set_channels(NUM_CHANNELS as u32).is_ok()
126+
{
127+
let mut supported_formats = vec![];
128+
129+
for f in &[
130+
AudioFormat::S16,
131+
AudioFormat::S24,
132+
AudioFormat::S24_3,
133+
AudioFormat::S32,
134+
AudioFormat::F32,
135+
AudioFormat::F64,
136+
] {
137+
if hwp.test_format(Format::from(*f)).is_ok() {
138+
supported_formats.push(format!("{:?}", f));
139+
}
143140
}
144-
}
145141

146-
println!(
147-
"\tSupported Format(s):\n\n\t\t{}\n",
148-
supported_formats.join(" ")
149-
);
150-
println!("\t------------------------------------------------------\n");
151-
}
152-
};
142+
if !supported_formats.is_empty() {
143+
println!("\tDevice:\n\n\t\t{}\n", name);
144+
145+
println!(
146+
"\tDescription:\n\n\t\t{}\n",
147+
a.desc.unwrap_or_default().replace("\n", "\n\t\t")
148+
);
149+
150+
println!(
151+
"\tSupported Format(s):\n\n\t\t{}\n",
152+
supported_formats.join(" ")
153+
);
154+
155+
println!(
156+
"\t------------------------------------------------------\n"
157+
);
158+
}
159+
}
160+
};
161+
}
153162
}
154163
}
155164
}

0 commit comments

Comments
 (0)