cpal icon indicating copy to clipboard operation
cpal copied to clipboard

ALSA function 'snd_pcm_hw_params_set_buffer_size' failed when setting buffer size

Open rMazeiks opened this issue 3 years ago • 3 comments

Hi! I'm having an issue as soon as I try specifying a buffer size for my output stream.

The following code runs:

use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{BufferSize, StreamConfig};

fn main() {
    let host = cpal::default_host();

    let device = host.default_output_device().unwrap();

    let mut supported_configs_range = device.supported_output_configs().unwrap();
    let config = supported_configs_range
        .next()
        .unwrap()
        .with_max_sample_rate();

    dbg!(&config);
    let mut c: StreamConfig = config.into();
    // c.buffer_size = BufferSize::Fixed(64);

    let stream = device
        .build_output_stream(
            &c,
            move |_: &mut [f32], _: &cpal::OutputCallbackInfo| {},
            move |_| {},
        )
        .unwrap();

    stream.play().unwrap();
}

Furthermore, the SupportedStreamConfig I get is as follows:

[src/main.rs:15] &config = SupportedStreamConfig {
    channels: 1,
    sample_rate: SampleRate(
        384000,
    ),
    buffer_size: Range {
        min: 3,
        max: 4194304,
    },
    sample_format: I16,
}

The above leads me to believe that I can specify any buffer_size between 3 and 4194304. Is this correct? Unfortunately, selecting any Fixed buffer size leads to failure. For example, if I uncomment the line c.buffer_size = BufferSize::Fixed(64), I get:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BackendSpecific { err: BackendSpecificError { description: "ALSA function 'snd_pcm_hw_params_set_buffer_size' failed with error 'EINVAL: Invalid argument'" } }', src/main.rs:25:10

Am I doing something wrong or is this a bug? I cant seem to specify any buffer size other than Default.

I tried using other values, such as the minimum 3, and the maximum 4194304, with the same results. I also adapted the code to test it with the master version of cpal, and I got the same error.

I'm very new to the audio processing world so apologies if I omitted anything. Any guidance would be appreciated. Thanks!

rMazeiks avatar Jan 24 '23 17:01 rMazeiks

I have the same problem, may be related to this: https://github.com/RustAudio/cpal/pull/582

I tried to use 1024 as recommended in the PR but no lucky :crying_cat_face:

geovannimp avatar Jan 29 '23 00:01 geovannimp

On macOS here everything runs without crashes, but I just don't get the buffer sizes that I requested (and are well within the range provied by the SupportedStreamConfig.

stijnfrishert avatar Feb 03 '23 14:02 stijnfrishert