我们正在运行带有 ALSA 的 Debian,我们需要从我们的音频输入线中提取音频样本。我们希望能够用这些样本填充缓冲区,以便在它们到来时对它们执行操作。
我们已经尝试直接查看 ALSA 调用,但我们遇到了折旧函数的问题,因为那里的很多资源都非常陈旧。
关于这个问题的方向有什么好的引用或建议吗?
最佳答案
使用一些最新的例子,像这样:
#include <alsa/asoundlib.h>
static char *device = "default"; /* capture device */
unsigned short buffer[2 * 24000];
int main(void)
{
int err;
snd_pcm_t *handle;
snd_pcm_sframes_t frames;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
printf("open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
if ((err = snd_pcm_set_params(handle,
SND_PCM_FORMAT_S16,
SND_PCM_ACCESS_RW_INTERLEAVED,
2,
48000,
1,
500000)) < 0) { /* 0.5 sec */
printf(" open error: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
for (;;) {
frames = snd_pcm_readi(handle, buffer, 24000);
if (frames < 0)
frames = snd_pcm_recover(handle, frames, 0);
if (frames < 0) {
printf("snd_pcm_readi failed: %s\n", snd_strerror(err));
break;
}
// ...
}
snd_pcm_close(handle);
return 0;
}
关于c++ - Debian 上的音频采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15961343/