【问题标题】:SDL_OpenAudioDevice: Continuous play from real time processed source bufferSDL_OpenAudioDevice:从实时处理的源缓冲区连续播放
【发布时间】:2020-04-29 15:29:00
【问题描述】:

我正在编写将模拟器移植到 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 的内容

【问题讨论】:

    标签: c audio sdl-2


    【解决方案1】:

    好的,我找到了解决办法!

    使用开篇文章的代码,我在当前帧实时播放下一帧的样本。错了!

    所以,我实现了一个循环缓冲区,我在其中放置下一个帧的样本,底层代码在每个(当前)帧传递给我。从我在不同位置读取和写入的缓冲区中,请参阅开头的帖子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2013-11-22
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      相关资源
      最近更新 更多