gpt4 book ai didi

c - Operation not Permitted on ALSA get hw parameters 函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:03 26 4
gpt4 key购买 nike

我正在尝试播放从服务器接收的 pcm 音频缓冲区。我下载了一个示例 alsa 播放文件,将录制的文件作为输入,它工作正常,但我在我的 SIP 客户端应用程序中添加的相同代码出现不允许操作错误。打开设备并设置设备配置没问题,但尝试获取我配置的参数时出现Operation not permitted error。谁能告诉我出现此错误的原因?

/* Open the PCM device in playback mode */
if (pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
SND_PCM_STREAM_PLAYBACK, 0) < 0)
printf("ERROR: Can't open \"%s\" PCM device. %s\n",
PCM_DEVICE, snd_strerror(pcm));

/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(&params);

snd_pcm_hw_params_any(pcm_handle, params);

/* Set parameters */
if (pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));

if (pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
//SND_PCM_FORMAT_S16_LE) < 0)
SND_PCM_FORMAT_MU_LAW) < 0)
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));

if (pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0)
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));

if (pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0)
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));

/* Write parameters */
if (pcm = snd_pcm_hw_params(pcm_handle, params) < 0)
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));

/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));

printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));

pcm = snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i %d", tmp, pcm);

if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");

snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);

printf("seconds: %d\n", seconds);

/* Allocate buffer to hold single period */
snd_pcm_hw_params_get_period_size(params, &frames, 0);

buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);

printf("buffsize: %d\n", buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

最佳答案

你所有的错误检查都是错误的。

<运算符绑定(bind)强于 = , 所以在这样一行中:

if (err = snd_something(...) < 0)

将函数的返回值与零进行比较,并将比较结果(假或真,0 或 1)分配给变量。

要使其正常工作,您必须在赋值周围添加括号:

if ((err = snd_something(...)) < 0)

但最好不要尝试将所有内容都放在一个表达式中:

err = snd_something(...);
if (err < 0)

关于c - Operation not Permitted on ALSA get hw parameters 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22830540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com