snd_pcm_hw_params_get_channels_min 和 snd_pcm_hw_params_get_channels_max 结果不一致

snd_pcm_hw_params_get_channels_min 和 snd_pcm_hw_params_get_channels_max 结果不一致

我正在尝试获取我的设备支持的最小和最大通道数。结果是意想不到的和随机的。它们在每次执行时都会发生变化。我错过了什么?

// 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);
}

编译脚本

gcc -o capture_nb_chan capture_nb_chan.c -lasound

执行它

./capture_nb_chan

输出是

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

输出值是随机的,而错误代码是2当它预计为0或负时文档

相关内容