X-Git-Url: https://code.octet-stream.net/m17rt/blobdiff_plain/1f1869d3e36f7192892fad069ef159faba208ccd..2c7d53c113f4e19f24c928b2c5eca1c3f6f799af:/m17codec2/src/soundcards.rs diff --git a/m17codec2/src/soundcards.rs b/m17codec2/src/soundcards.rs new file mode 100644 index 0000000..24cff0a --- /dev/null +++ b/m17codec2/src/soundcards.rs @@ -0,0 +1,61 @@ +//! Utilities for selecting suitable sound cards. + +use cpal::{ + traits::{DeviceTrait, HostTrait}, + SampleFormat, +}; + +/// List sound cards supported for audio output. +/// +/// M17RT will handle any card with 1 or 2 channels and 16-bit output. +pub fn supported_output_cards() -> Vec { + let mut out = vec![]; + let host = cpal::default_host(); + let Ok(output_devices) = host.output_devices() else { + return out; + }; + for d in output_devices { + let Ok(mut configs) = d.supported_output_configs() else { + continue; + }; + if configs.any(|config| { + (config.channels() == 1 || config.channels() == 2) + && config.sample_format() == SampleFormat::I16 + }) { + let Ok(name) = d.name() else { + continue; + }; + out.push(name); + } + } + out.sort(); + out +} + +/// List sound cards supported for audio input. +/// +/// +/// M17RT will handle any card with 1 or 2 channels and 16-bit output. +pub fn supported_input_cards() -> Vec { + let mut out = vec![]; + let host = cpal::default_host(); + let Ok(input_devices) = host.input_devices() else { + return out; + }; + for d in input_devices { + let Ok(mut configs) = d.supported_input_configs() else { + continue; + }; + if configs.any(|config| { + (config.channels() == 1 || config.channels() == 2) + && config.sample_format() == SampleFormat::I16 + }) { + let Ok(name) = d.name() else { + continue; + }; + out.push(name); + } + } + out.sort(); + out +}