Skip to content

build_output_stream fails on Windows 10 if the specified sample rate does not match the output device's default sample rate #593

@Herohtar

Description

@Herohtar

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:

image

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:

  1. Change the sample rate in StreamConfig to 192000 to match the device setting
  2. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions