为什么声音在 ALSA 中跳跃?

Why does the sound skip in ALSA?

我正在尝试使用下面的程序播放 this sound,但声音有点快,而且会跳过。声音文件的采样率为 11025 Hz,立体声,样本大小为 16 位。由于参数无效,问题似乎是 snd_pcm_hw_params_set_buffer_size()snd_pcm_hw_params_set_period_size() (两者都是我正在研究的函数),但我不知道为什么它们无效并且将它们注释掉并不能解决跳过效果.我做错了什么?

#include <stdlib.h>
#include <stdint.h>
#include <alsa/asoundlib.h>

#define STEREO              2
#define BITS            16 / 8
#define FRAMEBUFFERSIZE   512 // in samples
#define PERIODS             2
#define SAMPLERATE      11025

void snd_error_checker(int error, char *function_name)
{
    if (error)
    {
        printf("Error (%s): %s\n", function_name, snd_strerror(error));
        // exit(EXIT_FAILURE);
    }
}

int main(void)
{
    snd_pcm_t *handle;
    uint32_t channels                   = STEREO;
    uint32_t sample_size                = BITS; // 16 bit
    uint32_t frame_size                 = sample_size * channels;
    snd_pcm_uframes_t frames            = FRAMEBUFFERSIZE / frame_size;
    snd_pcm_uframes_t frames_per_period = frames / PERIODS;
    int32_t dir = 0; // No idea what this does.
    snd_pcm_hw_params_t *params;
    int16_t *buffer;
    FILE *wav;
    int32_t size;
    int error;

    error = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
    snd_error_checker(error, "snd_pcm_open()");
    snd_pcm_hw_params_alloca(&params);
    error = snd_pcm_hw_params_any(handle, params);
    snd_error_checker(error, "snd_pcm_hw_params_any()");
    error = snd_pcm_hw_params_set_buffer_size(handle, params, frames);
    snd_error_checker(error, "snd_pcm_hw_params_set_buffer_size()");
    error = snd_pcm_hw_params_set_period_size(handle, params, frames_per_period, dir);
    snd_error_checker(error, "snd_pcm_hw_params_set_period_size()");
    error = snd_pcm_hw_params_set_rate(handle, params, SAMPLERATE, dir);
    snd_error_checker(error, "snd_pcm_hw_params_set_rate()");
    error = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
    snd_error_checker(error, "snd_pcm_hw_params_set_access()");
    error = snd_pcm_hw_params_set_channels(handle, params, STEREO);
    snd_error_checker(error, "snd_pcm_hw_params_set_channels()");
    error = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
    snd_error_checker(error, "snd_pcm_hw_params_set_format()");
    error = snd_pcm_hw_params(handle, params);
    snd_error_checker(error, "snd_pcm_hw_params()");

    // Insert samples to framebuffer.
    wav = fopen("pcm1611s.wav", "r");
    fseek(wav, 0L, SEEK_END);
    size = ftell(wav);
    fseek(wav, 0L, SEEK_SET);
    buffer = malloc(size);
    fread(buffer, sizeof(int16_t), size, wav);
    fclose(wav);
    size /= sizeof(int16_t);

    // Set ptrbuffer 46 bytes ahead to skip the header.
    for (int16_t *ptrbuffer = buffer + 46; size > ptrbuffer - buffer; ptrbuffer += FRAMEBUFFERSIZE * STEREO * BITS)
    {
        error = snd_pcm_writei(handle, ptrbuffer, frames);
        if (error)
        {
            snd_pcm_recover(handle, error, 0);
        }
    }
    snd_pcm_drain(handle);
    snd_pcm_close(handle);
    exit(EXIT_SUCCESS);
}

正确播放需要考虑三点

  1. 检查snd_pcm_writei的return值,会给出一个清晰的思路,如果有错误是怎么回事。参考alsa-project

  2. while() 循环中读取文件并提供给 snd_pcm_writei 可能会导致在 运行 下跳过音频(-EPIPE 错误),因为 fread 是一个耗时的调用。

  3. 检查所有库调用的 return 值并确保全部成功。 还要确保值在 driver 中设置正确。比如snd_pcm_hw_params_set_ratesnd_pcm_hw_params_get_rate验证后,如果你的驱动不支持采样率11025KHz,我们可以用return的值查出来,再用snd_pcm_hw_params_get_rate

#define FRAMEBUFFERSIZE   512 // in samples

评论在撒谎;该值用作字节计数。

uint32_t sample_size                = BITS; // 16 bit

评论在撒谎;该值实际上以字节为单位测量。 (并且 BITS 命名错误。)

fread(buffer, sizeof(int16_t), size, wav);

size是以字节为单位的,但是你告诉fread()读取16位字。 (这实际上并没有什么坏处,因为它在文件末尾停止读取。但是你应该检查错误。)

int16_t *ptrbuffer = ...

您告诉编译器 ptrbuffer 指向 16 位值。

ptrbuffer += FRAMEBUFFERSIZE * STEREO * BITS

您告诉编译器步过 2048 个 16 位值,即超过 4096 个字节。您实际上想要遍历缓冲区中的所有样本,即 frames * STEREO.

error = snd_pcm_writei(...);
if (error)

snd_pcm_writei() returns 正数表示成功;误差为负。