@@ -204,19 +204,36 @@ impl AudioRecorder {
204204 device : & cpal:: Device ,
205205 ) -> Result < cpal:: SupportedStreamConfig , Box < dyn std:: error:: Error > > {
206206 let supported_configs = device. supported_input_configs ( ) ?;
207+ let mut best_config: Option < cpal:: SupportedStreamConfigRange > = None ;
207208
208- // Try to find a config that supports 16kHz
209+ // Try to find a config that supports 16kHz, prioritizing better formats
209210 for config_range in supported_configs {
210211 if config_range. min_sample_rate ( ) . 0 <= constants:: WHISPER_SAMPLE_RATE
211212 && config_range. max_sample_rate ( ) . 0 >= constants:: WHISPER_SAMPLE_RATE
212213 {
213- // Found a config that supports 16kHz, use it
214- return Ok (
215- config_range. with_sample_rate ( cpal:: SampleRate ( constants:: WHISPER_SAMPLE_RATE ) )
216- ) ;
214+ match best_config {
215+ None => best_config = Some ( config_range) ,
216+ Some ( ref current) => {
217+ // Prioritize F32 > I16 > I32 > others
218+ let score = |fmt : cpal:: SampleFormat | match fmt {
219+ cpal:: SampleFormat :: F32 => 4 ,
220+ cpal:: SampleFormat :: I16 => 3 ,
221+ cpal:: SampleFormat :: I32 => 2 ,
222+ _ => 1 ,
223+ } ;
224+
225+ if score ( config_range. sample_format ( ) ) > score ( current. sample_format ( ) ) {
226+ best_config = Some ( config_range) ;
227+ }
228+ }
229+ }
217230 }
218231 }
219232
233+ if let Some ( config) = best_config {
234+ return Ok ( config. with_sample_rate ( cpal:: SampleRate ( constants:: WHISPER_SAMPLE_RATE ) ) ) ;
235+ }
236+
220237 // If no config supports 16kHz, fall back to default
221238 Ok ( device. default_input_config ( ) ?)
222239 }
0 commit comments