c - SDL_OpenAudioDevice:从实时处理的源缓冲区连续播放
问题描述
我正在编写将模拟器移植到 SDL。有一个在每一帧调用的方法,它为下一帧传递一个带有新音频样本的缓冲区。
我使用 SDL_OpenAudioDevice 打开了一个设备,并且在每一帧 SDL 回调方法都会从音频缓冲区中再现样本。
它可以工作,但声音并不完美,有些抽搐,有些金属噪音等等。
声音是 16 位签名的。
编辑:好的,我找到了解决方案!
使用开篇的代码,我在当前帧实时播放下一帧的样本。那是错的!
因此,我实现了一个循环缓冲区,在其中放置下一个帧的样本,底层代码在每个(当前)帧传递给我。
在该缓冲区中有 2 个指针,一个用于读取点,另一个用于写入点。SDL 在其音频流上没有更多数据可播放时调用回调函数;因此,当调用回调函数时,我会从循环缓冲区上的读取点播放音频样本,然后更新读取指针。
当底层代码为我提供下一帧的音频样本数据时,我将它们写入写入点的循环缓冲区中,然后更新写入指针。
读取和写入指针按每帧要播放的样本量移动。
代码已更新,当 samplesPerFrame 不是 int 但可以工作时需要进行一些调整;-)
循环缓冲结构:
typedef struct circularBufferStruct
{
short *buffer;
int cells;
short *readPoint;
short *writePoint;
} circularBuffer;
此方法在初始化时调用:
int initialize_audio(int stereo)
{
if (stereo)
channel = 2;
else
channel = 1;
// Check if sound is disabled
if (sampleRate != 0)
{
// Initialize SDL Audio
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
SDL_Log("SDL fails to initialize audio subsystem!\n%s", SDL_GetError());
return 1;
}
// Number of samples per frame
samplesPerFrame = (double)sampleRate / (double)framesPerSecond * channel;
audioSamplesSize = samplesPerFrame * bytesPerSample; // Bytes
audioBufferSize = audioSamplesSize * 10; // Bytes
// Set and clear circular buffer
audioBuffer.buffer = malloc(audioBufferSize); // Bytes, must be a multiple of audioSamplesSize
memset(audioBuffer.buffer, 0, audioBufferSize);
audioBuffer.cells = (audioBufferSize) / sizeof(short); // Cells, not Bytes!
audioBuffer.readPoint = audioBuffer.buffer;
audioBuffer.writePoint = audioBuffer.readPoint + (short)samplesPerFrame;
}
else
samplesPerFrame = 0;
// First frame
return samplesPerFrame;
}
这是来自 want.callback 的 SDL 方法回调:
void audioCallback(void *userdata, uint8_t *stream, int len)
{
SDL_memset(stream, 0, len);
if (audioSamplesSize == 0)
return;
if (len > audioSamplesSize)
{
len = audioSamplesSize;
}
SDL_MixAudioFormat(stream, (const Uint8 *)audioBuffer.readPoint, AUDIO_S16SYS, len, SDL_MIX_MAXVOLUME);
audioBuffer.readPoint += (short)samplesPerFrame;
if (audioBuffer.readPoint >= audioBuffer.buffer + audioBuffer.cells)
audioBuffer.readPoint = audioBuffer.readPoint - audioBuffer.cells;
}
在每一帧调用这个方法(在第一次通过后,我们只需要样本量):
int update_audio(short *buffer)
{
// Check if sound is disabled
if (sampleRate != 0)
{
memcpy(audioBuffer.writePoint, buffer, audioSamplesSize); // Bytes
audioBuffer.writePoint += (short)samplesPerFrame; // Cells
if (audioBuffer.writePoint >= audioBuffer.buffer + audioBuffer.cells)
audioBuffer.writePoint = audioBuffer.writePoint - audioBuffer.cells;
if (firstTime)
{
// Set required audio specs
want.freq = sampleRate;
want.format = AUDIO_S16SYS;
want.channels = channel;
want.samples = samplesPerFrame / channel; // total samples divided by channel count
want.padding = 0;
want.callback = audioCallback;
want.userdata = NULL;
device = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, 0), 0, &want, &have, 0);
SDL_PauseAudioDevice(device, 0);
firstTime = 0;
}
}
else
samplesPerFrame = 0;
// Next frame
return samplesPerFrame;
}
我希望这个问题/答案将来对其他人有用,因为我几乎没有在网上找到任何关于 SDL Audio 的内容
解决方案
好的,我找到了解决方案!
使用开篇的代码,我在当前帧实时播放下一帧的样本。那是错的!
因此,我实现了一个循环缓冲区,在其中放置下一个帧的样本,底层代码在每个(当前)帧传递给我。从那个缓冲区我在不同的位置读写,见开篇
推荐阅读
- python - 如何使用 for 循环将不同的变量分配给类对象?
- javascript - 如何在 nodejs 上使用 imap-simple 和 mailparser 按发件人过滤邮件?
- android - 为什么android studio模拟器不起作用?
- angular - Angular 11 没有运行 ngcc
- javascript - 用于复制文本原始数据的 Javascript 函数
- python-requests - Burpsuite 转发器和 python 请求的不同返回
- c - 这是一个正确的 C 程序吗?
- bash - 获取函数内的最后一个 echo 语句并将变量放入 bash
- powershell - 将值传递给 azure board 任务、用户故事等中的自定义字段
- postgresql - 如何引用或合并可能不存在的列?