]> code.octet-stream.net Git - m17rt/blob - m17codec2/src/soundcards.rs
Voice UDP to RF conversion, and better sample rate management in codec2
[m17rt] / m17codec2 / src / soundcards.rs
1 //! Utilities for selecting suitable sound cards.
2
3 use cpal::{
4 traits::{DeviceTrait, HostTrait},
5 SampleFormat,
6 };
7
8 /// List sound cards supported for audio output.
9 ///
10 /// M17RT will handle any card with 1 or 2 channels and 16-bit output.
11 pub fn supported_output_cards() -> Vec<String> {
12 let mut out = vec![];
13 let host = cpal::default_host();
14 let Ok(output_devices) = host.output_devices() else {
15 return out;
16 };
17 for d in output_devices {
18 let Ok(mut configs) = d.supported_output_configs() else {
19 continue;
20 };
21 if configs.any(|config| {
22 (config.channels() == 1 || config.channels() == 2)
23 && config.sample_format() == SampleFormat::I16
24 }) {
25 let Ok(name) = d.name() else {
26 continue;
27 };
28 out.push(name);
29 }
30 }
31 out.sort();
32 out
33 }
34
35 /// List sound cards supported for audio input.
36 ///
37 ///
38 /// M17RT will handle any card with 1 or 2 channels and 16-bit output.
39 pub fn supported_input_cards() -> Vec<String> {
40 let mut out = vec![];
41 let host = cpal::default_host();
42 let Ok(input_devices) = host.input_devices() else {
43 return out;
44 };
45 for d in input_devices {
46 let Ok(mut configs) = d.supported_input_configs() else {
47 continue;
48 };
49 if configs.any(|config| {
50 (config.channels() == 1 || config.channels() == 2)
51 && config.sample_format() == SampleFormat::I16
52 }) {
53 let Ok(name) = d.name() else {
54 continue;
55 };
56 out.push(name);
57 }
58 }
59 out.sort();
60 out
61 }