@@ -27,7 +27,7 @@ impl AudioOutput {
27
27
28
28
// Get the default device config, so we know what sample format and sample rate
29
29
// the device supports.
30
- let supported = device . default_output_config ( ) ?;
30
+ let supported = Self :: preferred_output_config ( & device ) ?;
31
31
32
32
let ( callback_send, callback_recv) = bounded ( 16 ) ;
33
33
@@ -48,6 +48,24 @@ impl AudioOutput {
48
48
} )
49
49
}
50
50
51
+ fn preferred_output_config (
52
+ device : & cpal:: Device ,
53
+ ) -> Result < cpal:: SupportedStreamConfig , Error > {
54
+ const PREFERRED_SAMPLE_RATE : cpal:: SampleRate = cpal:: SampleRate ( 44_100 ) ;
55
+
56
+ let mut configs: Vec < _ > = device. supported_output_configs ( ) ?. collect ( ) ;
57
+ configs. sort_by ( |a, b| a. cmp_default_heuristics ( b) ) ;
58
+
59
+ for range in configs {
60
+ let r = range. min_sample_rate ( ) ..=range. max_sample_rate ( ) ;
61
+ if r. contains ( & PREFERRED_SAMPLE_RATE ) {
62
+ return Ok ( range. with_sample_rate ( PREFERRED_SAMPLE_RATE ) ) ;
63
+ }
64
+ }
65
+
66
+ Ok ( device. default_output_config ( ) ?)
67
+ }
68
+
51
69
pub fn sink ( & self ) -> AudioSink {
52
70
self . sink . clone ( )
53
71
}
@@ -237,6 +255,12 @@ impl From<cpal::DefaultStreamConfigError> for Error {
237
255
}
238
256
}
239
257
258
+ impl From < cpal:: SupportedStreamConfigsError > for Error {
259
+ fn from ( err : cpal:: SupportedStreamConfigsError ) -> Error {
260
+ Error :: AudioOutputError ( Box :: new ( err) )
261
+ }
262
+ }
263
+
240
264
impl From < cpal:: BuildStreamError > for Error {
241
265
fn from ( err : cpal:: BuildStreamError ) -> Error {
242
266
Error :: AudioOutputError ( Box :: new ( err) )
0 commit comments