cpal
cpal copied to clipboard
`build_output_stream` fails on Windows 10 if the specified sample rate does not match the output device's default sample rate
For context, the Windows audio device properties allow you to configure the default sample rate and bit depth that should be used when an audio device is running in shared mode:
If you try to create an output stream in cpal
using the build_output_stream
method and the specified sample rate does not match the sample rate defined in the device properties, the method fails.
Example code:
use cpal::traits::{DeviceTrait, HostTrait};
fn main() {
let host = cpal::default_host();
let device = host.default_output_device().unwrap();
device.build_output_stream(
&cpal::StreamConfig {
channels: cpal::ChannelCount::from(2u16),
sample_rate: cpal::SampleRate(48000), // Audio device default sample rate is set to 192000
buffer_size: cpal::BufferSize::Default,
},
move |_: &mut [f32], _| {},
move |_| {},
).unwrap();
}
Result:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: StreamConfigNotSupported', src\main.rs:16:7
The error can be resolved in one of two ways:
- Change the sample rate in
StreamConfig
to 192000 to match the device setting - Change the device setting to 48000 to match the sample rate in the code
Am I correct in believing that this is a bug, and you should be able to create an output stream using any (supported) sample rate, or is there some limitation that prevents that from being possible?
I'm seeing a very similar situation on Mac OS- the only supported formats are high sample rate floating point formats. My use case needs a much lower sample rate, and it doesn't appear to be available through this API. Here's what my configuration looks like:
Supported hosts:
[CoreAudio]
Available hosts:
[CoreAudio]
CoreAudio
Default Input Device:
Some("Scarlett Solo USB")
Default Output Device:
Some("Scarlett Solo USB")
Devices:
1. "LG HDR 4K"
Error getting supported output configs: BackendSpecific { err: BackendSpecificError { description: "Invalid property value" } }
2. "Scarlett Solo USB"
Default input stream config:
SupportedStreamConfig { channels: 2, sample_rate: SampleRate(192000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
All supported input stream configs:
2.1. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.2. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.3. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.4. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.5. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(176400), max_sample_rate: SampleRate(176400), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.6. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(192000), max_sample_rate: SampleRate(192000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
Default output stream config:
SupportedStreamConfig { channels: 2, sample_rate: SampleRate(192000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
All supported output stream configs:
2.1. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.2. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.3. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.4. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.5. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(176400), max_sample_rate: SampleRate(176400), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
2.6. SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(192000), max_sample_rate: SampleRate(192000), buffer_size: Range { min: 58, max: 4096 }, sample_format: F32 }
3. "MacBook Pro Microphone"
Default input stream config:
SupportedStreamConfig { channels: 1, sample_rate: SampleRate(48000), buffer_size: Range { min: 15, max: 4096 }, sample_format: F32 }
All supported input stream configs:
3.1. SupportedStreamConfigRange { channels: 1, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), buffer_size: Range { min: 15, max: 4096 }, sample_format: F32 }
3.2. SupportedStreamConfigRange { channels: 1, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), buffer_size: Range { min: 15, max: 4096 }, sample_format: F32 }
3.3. SupportedStreamConfigRange { channels: 1, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), buffer_size: Range { min: 15, max: 4096 }, sample_format: F32 }
3.4. SupportedStreamConfigRange { channels: 1, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), buffer_size: Range { min: 15, max: 4096 }, sample_format: F32 }
4. "MacBook Pro Speakers"
Error getting supported output configs: BackendSpecific { err: BackendSpecificError { description: "Invalid property value" } }
you should be able to create an output stream using any (supported) sample rate
In the case of Windows, it appears that the device's configured sample rate is the only supported one. In general, it is the application's responsibility to query supported_output_configs and select a valid configuration.
@leecbaker
I'm seeing a very similar situation on Mac OS- the only supported formats are high sample rate floating point formats
The output you pasted includes sample rates of 48000 and below. These are not at all high sample rates. If you want to work in terms of any specific sample rate on a general purpose OS then it's generally your responsibility to resample to suit what the environment wants, but e.g. 48kHz is a very common and reasonable default regardless.
I'm encountering a similar issue: running device.supported_input_configs()
only returns the default configuration even though the device supports a wide selection of sample rates. I noticed that if I go and change the default configuration in windows then device.supported_input_configs()
return the new updated default. This seems to be a cpal
because I tried listing supported configurations using PortAudio and it listed them correctly.