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

发布于 2024-08-10 22:08:20 字数 2245 浏览 4 评论 0原文

当我编译 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;
}

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

拥抱, 路易丝

When I compile ALSA's pcm_min.c example with

gcc -Wall -lasound pcm_min.c -o pcm_min

Everything is fine, but running it, I get the white noise as expected, but I also get this warning/error:

Short write (expected 16384, wrote 7616)

Which comes from the last if-statement.

#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;
}

Can someone see why this warning/error occur?

Hugs,
Louise

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

定格我的天空 2024-08-17 22:08:20

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

The snd_pcm_writei() function might return less than sizeof(buffer) when there's either a signal received or an underrun. In your case, it seems that you're mixing bytes and frames. The last parameter of the call is the number of frames that you have in your buffer. Since you're passing the number of bytes in your buffer instead, you're seeing an underrun.

唯憾梦倾城 2024-08-17 22:08:20

我对这个例子也有一些问题。我对其进行了一些修改,现在它可以工作了。

#include <stdio.h>
#include <stdlib.h>
#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;
    snd_pcm_uframes_t bufferSize, periodSize;

    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_S16_LE,
                                  SND_PCM_ACCESS_RW_INTERLEAVED,
                                  1, //channels
                                  44100, //sample rate
                                  1, //allow resampling
                                  500000) //required latency in us
          ) < 0) {
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_prepare(handle)) < 0) {
        printf("Pcm prepare error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_get_params( handle, &bufferSize, &periodSize )) < 0) {
        printf("Pcm get params error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }
    printf("Buffer size:%d, Period size:%d\n", (int)bufferSize, (int)periodSize);

    for (i = 0; i < 16; i++) {
        frames = snd_pcm_writei(handle, buffer, periodSize);
        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)periodSize)
            printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
    }
    snd_pcm_close(handle);
    return 0;
}

I was also having some problems with this example. I modified it a bit and now it works.

#include <stdio.h>
#include <stdlib.h>
#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;
    snd_pcm_uframes_t bufferSize, periodSize;

    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_S16_LE,
                                  SND_PCM_ACCESS_RW_INTERLEAVED,
                                  1, //channels
                                  44100, //sample rate
                                  1, //allow resampling
                                  500000) //required latency in us
          ) < 0) {
        printf("Playback open error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_prepare(handle)) < 0) {
        printf("Pcm prepare error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((err = snd_pcm_get_params( handle, &bufferSize, &periodSize )) < 0) {
        printf("Pcm get params error: %s\n", snd_strerror(err));
        exit(EXIT_FAILURE);
    }
    printf("Buffer size:%d, Period size:%d\n", (int)bufferSize, (int)periodSize);

    for (i = 0; i < 16; i++) {
        frames = snd_pcm_writei(handle, buffer, periodSize);
        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)periodSize)
            printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
    }
    snd_pcm_close(handle);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文