RustAudio / cpal

Cross-platform audio I/O library in pure Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ALSA function 'snd_pcm_hw_params_set_rate' failed with error 'EINVAL: Invalid argument'

oneElectron opened this issue · comments

I am creating a Rust project for a raspberry pi with cpal, which I develop from MacOS.
On MacOS cpal works as expected, however on the raspberry pi running Raspbian (Debian 12 bookworm) I get the error:
called `Result::unwrap()` on an `Err` value: BackendSpecific { err: BackendSpecificError { description: "ALSA function 'snd_pcm_hw_params_set_rate' failed with error 'EINVAL: Invalid argument'" } }

Here is the code snippet:

Code
let (device, config, format) = Self::choose_input_device();
let (audio_sender, audio_receiver) = std::sync::mpsc::channel();

let audio_stream = device
    .build_input_stream(
        &config,
        move |d: &[f32], _x| {
            audio_sender.send(d.to_owned()).unwrap();
        },
        |err| {
            println!("Error: {:?}", err);
            panic!();
        },
        None,
    )
    .unwrap(); // <-- Panic occurs here

audio_stream.play().unwrap();

Here is the output:

Output
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
  hw:CARD=U192k,DEV=0
  plughw:CARD=U192k,DEV=0
  default:CARD=U192k
  sysdefault:CARD=U192k
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dsnoop.c:540:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
  dsnoop:CARD=U192k,DEV=0
ALSA lib pcm_asym.c:105:(_snd_pcm_asym_open) capture slave is not defined
ALSA lib pcm_asym.c:105:(_snd_pcm_asym_open) capture slave is not defined
ALSA lib pcm_dmix.c:999:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Choose device:
1
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
thread 'main' panicked at src/recorder/mod.rs:45:14:
called `Result::unwrap()` on an `Err` value: BackendSpecific { err: BackendSpecificError { description: "ALSA function 'snd_pcm_hw_params_set_rate' failed with error 'EINVAL: Invalid argument'" } }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

If there is anything else that might help let me know, but I don't know much about how the audio stack works on linux.

update using a different device I get this:

Output
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
  hw:CARD=U192k,DEV=0
  plughw:CARD=U192k,DEV=0
  default:CARD=U192k
  sysdefault:CARD=U192k
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dsnoop.c:540:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream
  dsnoop:CARD=U192k,DEV=0
ALSA lib pcm_asym.c:105:(_snd_pcm_asym_open) capture slave is not defined
ALSA lib pcm_asym.c:105:(_snd_pcm_asym_open) capture slave is not defined
ALSA lib pcm_dmix.c:999:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Choose device:
0
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:972:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(44100), max_sample_rate: SampleRate(44100), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(48000), max_sample_rate: SampleRate(48000), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(88200), max_sample_rate: SampleRate(88200), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(96000), max_sample_rate: SampleRate(96000), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(176400), max_sample_rate: SampleRate(176400), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
SupportedStreamConfigRange { channels: 2, min_sample_rate: SampleRate(192000), max_sample_rate: SampleRate(192000), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
supported config: SupportedStreamConfig { channels: 2, sample_rate: SampleRate(44100), buffer_size: Range { min: 16, max: 384000 }, sample_format: I32 }
Some(BackendSpecific { err: BackendSpecificError { description: "ALSA function 'snd_pcm_hw_params_set_format' failed with error 'EINVAL: Invalid argument'" } })
thread 'main' panicked at src/recorder/mod.rs:48:13:
explicit panic
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace