Skip to content

Commit 911e187

Browse files
committed
cpal: Simplify enumerate_devices_sync()
1 parent 0e147b9 commit 911e187

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

src/io/cpal.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -397,46 +397,31 @@ impl AudioBackendManager for CpalBackend {
397397
where
398398
Self: Sized,
399399
{
400-
let mut index = 0;
401400
let host = get_host();
402401

403-
let mut inputs: Vec<MediaDeviceInfo> = host
404-
.devices()
402+
let input_devices = host
403+
.input_devices()
405404
.unwrap()
406-
.filter(|d| d.default_input_config().is_ok())
407-
.map(|d| {
408-
index += 1;
405+
.map(|d| (d, MediaDeviceInfoKind::AudioInput));
409406

410-
MediaDeviceInfo::new(
411-
format!("{}", index),
412-
None,
413-
MediaDeviceInfoKind::AudioInput,
414-
d.name().unwrap(),
415-
Box::new(d),
416-
)
417-
})
418-
.collect();
419-
420-
let mut outputs: Vec<MediaDeviceInfo> = host
421-
.devices()
407+
let output_devices = host
408+
.output_devices()
422409
.unwrap()
423-
.filter(|d| d.default_output_config().is_ok())
424-
.map(|d| {
425-
index += 1;
410+
.map(|d| (d, MediaDeviceInfoKind::AudioOutput));
426411

412+
input_devices
413+
.chain(output_devices)
414+
.enumerate()
415+
.map(|(index, (device, kind))| {
427416
MediaDeviceInfo::new(
428-
format!("{}", index),
417+
(index + 1).to_string(),
429418
None,
430-
MediaDeviceInfoKind::AudioOutput,
431-
d.name().unwrap(),
432-
Box::new(d),
419+
kind,
420+
device.name().unwrap(),
421+
Box::new(device),
433422
)
434423
})
435-
.collect();
436-
437-
inputs.append(&mut outputs);
438-
439-
inputs
424+
.collect()
440425
}
441426
}
442427

0 commit comments

Comments
 (0)