c - 来自 ALSA 的 pcm_min.c 示例的警告/错误。可能的问题?

标签 c alsa

当我编译 ALSA 的 pcm_min.c 时例子

gcc -Wall -lasound pcm_min.c -o pcm_min

一切都很好,但是运行它,我得到了预期的白噪声,但我也收到了这个警告/错误:

Short write (expected 16384, wrote 7616)

来自最后一个 if 语句。

#include <alsa/asoundlib.h>

static char *device = "default";                        /* playback device */

snd_output_t *output = NULL;
unsigned char buffer[16*1024];                          /* some random data */

int main(void)
{
        int err;
        unsigned int i;
        snd_pcm_t *handle;
        snd_pcm_sframes_t frames;

        for (i = 0; i < sizeof(buffer); i++)
                buffer[i] = random() & 0xff;

        if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
                printf("Playback open error: %s\n", snd_strerror(err));
                exit(EXIT_FAILURE);
        }
        if ((err = snd_pcm_set_params(handle,
                                      SND_PCM_FORMAT_U8,
                                      SND_PCM_ACCESS_RW_INTERLEAVED,
                                      1,
                                      48000,
                                      1,
                                      500000)) < 0) {   /* 0.5sec */
                printf("Playback open error: %s\n", snd_strerror(err));
                exit(EXIT_FAILURE);
        }

        for (i = 0; i < 16; i++) {
                frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
                if (frames < 0)
                        frames = snd_pcm_recover(handle, frames, 0);
                if (frames < 0) {
                        printf("snd_pcm_writei failed: %s\n", snd_strerror(err));
                        break;
                }
                if (frames > 0 && frames < (long)sizeof(buffer))
                        printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
        }

        snd_pcm_close(handle);
        return 0;
}

有人能看出为什么会出现此警告/错误吗?

拥抱, 路易丝

最佳答案

snd_pcm_writei() 函数返回的值可能小于 sizeof(buffer) 当接收到信号或欠载时。在您的情况下,您似乎在混合字节和帧。调用的最后一个参数是缓冲区中的帧数。由于您传递的是缓冲区中的字节数,因此您会看到数据不足。

关于c - 来自 ALSA 的 pcm_min.c 示例的警告/错误。可能的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1740359/

相关文章:

c - 将字符附加到 C 中的字符串?

c - c 中的矩阵和对角线任务

audio - 如何在 Mac OS Yosemite 上的 Docker 容器中播放声音

audio - 在 .asoundrc 中存储参数不适用

c - 从哪里可以获得这些头文件?

c - GTK : Visual Studio Issue

c - 读取大小无效 - 查找命令行平均值

c - 使用#define 创建与 C89 std 兼容的虚拟记录

alsa - 如何将 X 个 channel 混合成一个 channel

python - PyAudio 工作,但每次都吐出错误消息