【发布时间】:2021-09-25 13:38:54
【问题描述】:
我正在尝试编写一个可以直接通过 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(¶ms);
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 的速率播放。
【问题讨论】:
-
您是在播放视频还是来自其他程序的视频?看来您需要从某个地方获取帧同步信号,然后播放样本。
-
我打算在同一个程序中播放视频。理想情况下,循环将显示帧,播放 N 个(在示例 2000 中)音频样本,然后休眠一段时间 (1/FPS) 并重复。我不确定这是否可能,但这就是我一直在考虑的方式。
-
这个问题太宽泛了,需要建议,所以离题了。如果您开始并有一个特定的编程问题,您可以回来问它。你的计划听起来可行。
-
我明白,我是新手。感谢您的帮助。
-
我已经用一些我一直在玩的示例代码编辑了这个问题。如果它在你的球场,你介意看看吗?