0

I am trying to get the minimum and maximum number of channels supported by my device. The results are unexpected and random. They change at each execution. Did I miss something?

// file : capture_nb_chan.c
#include <stdio.h>
#include <alsa/asoundlib.h>

int main(int argc, char* argv[])
{
  int err;
  snd_pcm_t             *capture_handle;
  snd_pcm_hw_params_t   *hw_params;
  unsigned int min, max;

  // Open audio device
  if ((err = snd_pcm_open (&capture_handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0)) < 0)
    exit (1);

  // Allocate hardware parameters
  if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
    exit (1);

  // Initialize parameters
  if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0)
    exit (1);

  // Get min and max number of channels
  err = snd_pcm_hw_params_get_channels_min(hw_params, &min);
  if (err < 0)
    exit (1);
  fprintf(stdout, "Min channels: %u. (%d)\n", min, err);
  err = snd_pcm_hw_params_get_channels_max(hw_params, &max);
  if (err < 0)
    exit (1);
  fprintf(stdout, "Max channels: %u. (%d)\n", max, err);
}

Compile the script with

gcc -o capture_nb_chan capture_nb_chan.c -lasound

Execute it with

./capture_nb_chan

Output is

Min channels: 22031. (2)
Max channels: 681205789. (2)

Output values are random while error code is 2 when it's expected to be 0 or negative according to documentation.

alpereira7
  • 1,522
  • 3
  • 17
  • 30
  • 1
    Works for me. It appears you have current header files, but are actually calling the functions of alsa-lib 0.9.x. Did you manually install anything? – CL. Oct 07 '20 at 14:22
  • @CL., thanks for your suggestion, the issue was indeed a version problem. – alpereira7 Oct 08 '20 at 09:19
  • Similar topic: https://stackoverflow.com/questions/46035886/how-to-query-alsa-channels-period-and-buffer-on-raspberry-pi – alpereira7 Oct 08 '20 at 09:20

1 Answers1

0

As CL. hinted, it was indeed a problem with version.

I initially installed the default alsa version that was 1.1.8 back then.

For another project, to match a buildroot job working with alsa 1.1.7, I manually replaced the file /lib/x86_64-linux-gnu/libasound.so.2.0.0 (v 1.1.8) with the one provided by buildroot (v 1.1.7).

I reverted this change and the OP's script executed properly.

alpereira7
  • 1,522
  • 3
  • 17
  • 30