我正在将仿真器移植到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的任何内容
好,我找到了解决方案!
使用开头帖子的代码,我正在实时播放当前帧的下一帧样本。错了!
因此,我实现了一个循环缓冲区,在其中放置下一帧的样本,基础代码在每个(当前)帧传递给我。我从那个缓冲区中的不同位置读写,请参见开头]