在 Linux 上播放 PCM 音频 (wav)

时间:2021-07-16 19:16:29

标签: c linux audio alsa pcm

我正在尝试编写一个可以直接通过 linux 声音缓冲区播放 PCM wav 文件的 C 程序。这样做的用途是将此音频与视频帧同步。例如,对于 48kHz 音频,对于 24FPS 的视频,我必须每帧播放 2000 个样本,因此我需要对播放进行这种控制。

我研究了不同的库,但我想问社区,对于这个应用程序来说,哪个是理想的、有点简单的库?如果您能够指出示例代码的方向,那也会非常有帮助。谢谢。

编辑:到目前为止我所拥有的是基于我在网上找到的示例代码,我稍微修改了一下以读取 .wav 文件作为参数而不是标准输入。无论哪种方式,音频都在不断地嗡嗡作响,显然没有播放没错。

 * Simple sound playback using ALSA API and libasound.
 *
 * Compile:
 * $ cc -o play sound_playback.c -lasound
 * 
 * Usage:
 * $ ./play <sample_rate> <channels> <seconds> < <file>
 * 
 * Examples:
 * $ ./play 44100 2 5 < /dev/urandom
 * $ ./play 22050 1 8 < /path/to/file.wav
 *
 * Copyright (C) 2009 Alessandro Ghedini <al3xbio@gmail.com>
 * --------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * Alessandro Ghedini wrote this file. As long as you retain this
 * notice you can do whatever you want with this stuff. If we
 * meet some day, and you think this stuff is worth it, you can
 * buy me a beer in return.
 * --------------------------------------------------------------
 */

#include <alsa/asoundlib.h>
#include <stdio.h>

#define PCM_DEVICE "default"

int main(int argc, char **argv) {
    unsigned int pcm, tmp, dir;
    int rate, channels, seconds;
    snd_pcm_t *pcm_handle;
    snd_pcm_hw_params_t *params;
    snd_pcm_uframes_t frames;
    char *buff;
    int buff_size, loops;
    FILE *fp;

    if (argc < 5) {
        printf("Usage: %s <sample_rate> <channels> <seconds> <filename>\n",
                                argv[0]);
        return -1;
    }

    rate     = atoi(argv[1]);
    channels = atoi(argv[2]);
    seconds  = atoi(argv[3]);

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

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

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

    snd_pcm_hw_params_get_period_time(params, &tmp, NULL);

    fp = fopen(argv[4], "rb");
    fseek(fp, 44, SEEK_SET);

    for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) {

        if (pcm = fgets(buff, buff_size, fp) == 0) {
            printf("Early end of file.\n");
            return 0;
        }

        if (pcm = snd_pcm_writei(pcm_handle, buff, frames) == -EPIPE) {
            printf("XRUN.\n");
            snd_pcm_prepare(pcm_handle);
        } else if (pcm < 0) {
            printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
        }

    }

    snd_pcm_drain(pcm_handle);
    snd_pcm_close(pcm_handle);
    free(buff);

    return 0;
}

我传入的音频文件预计在 16 个声道上以 48kHz 的速率播放。

2 个答案:

答案 0 :(得分:1)

这是一个杂耍答案,旨在让您意识到您最终会遇到的问题,基于您(完全合理!)的假设,即音频采样与视频的整数比帧。

<块引用>

例如,对于 48kHz 音频,对于 24FPS 视频,我必须每帧播放 2000 个样本,因此我需要对播放进行这种控制。

您将不可避免地遇到的问题是,在 PC 中,如果有单独的音频和视频设备,则声卡和显卡各有其自己的时钟振荡器,并且这些振荡器不会相互锁定其他。这意味着在 PC 上,对于 24FPS 视频和 48kHz 采样率,声卡实际上不会播放(或录制)2000 个样本到一帧。

这只是PC架构的一个根本缺陷,不同的设备覆盖不同的媒体。

当然,如果您通过与视频相同的设备发送音频,例如使用同时传输视频和音频的 HDMI、DisplayPort 或 SDI,则不会出现这个基本问题。 但是对于通用硬件,您必须准备好应对它!

答案 1 :(得分:0)

要从 wav 文件中读取 PCM 音频,最好使用库来解码文件中的音频数据。由于您的代码没有对音频数据进行解码,因此 wav 文件的数据听起来很嘈杂!

gtkIOStream 使用 libsox 来解码许多不同类型的音频文件。如果您要使用 C++,那么打开文件进行读取并找到它的重要信息,例如通道、采样率等,就像这样:

int res=sox.openRead(name);
if (res<0 && res!=SOX_READ_MAXSCALE_ERROR)
    return SoxDebug().evaluateError(res);

unsigned int fs;
if (sox.getFSIn()!=Playback::getSampleRate()){
    cout<<"sample rate mismatch, file = "<<sox.getFSIn()<<" Hz and ALSA = "<<Playback::getSampleRate()<<endl;
    cout<<"fixing sample rate mismatch"<<endl;
    if ((res=setSampleRate(sox.getFSIn()))<0) // set ALSA's playback saple rate
        return ALSADebug().evaluateError(res);
    fs=Playback::getSampleRate(); // read back ALSA's sample rate
}
cout<<"rates are now, file = "<<sox.getFSIn()<<" Hz and ALSA = "<<fs<<endl;

int ch=sox.getChCntIn();
cout<<"setting ALSA channels to " <<ch<<endl;
// use the ALSA function to set the channels here.

在 C 中,它更像是以下内容:

sox_format_t *in = sox_open_read(fileName.c_str(), NULL, NULL, NULL);
if (!in)
    return -1; // some error like this would be better SOX_READ_FILE_OPEN_ERROR;

可以像这样找到文件的采样率和各种其他声音文件参数:

in->signal.rate
in->signal.channels

现在 sox 使用 32 位字,因此您应该将 ALSA 设置为使用以下格式的 32 位字:

SND_PCM_FORMAT_S32_LE

现在使用 sox 读取“计数”数量的数据,如下所示:

sox_sample_t buf[count*ch];
size_t readCount=sox_read(in, buf, count);

然后您可以使用 snd_pcm_writei 将该缓冲区写入 ALSA。 不要忘记为播放设置 ALSA 的通道和采样率。

gtkIOStream ALSAPlaybackTest.C file 中提供了从许多不同类型的音频文件中播放 C++ 的完整示例。

如果您想要一个好的 C 设置源,请参考 ALSA's aplay.c code