如何使用 SDL_audio 生成音调?

How do I generate a tone using SDL_audio?

我正在尝试使用 SDL_audio 生成简单、恒定的正弦波。我有一个小帮手class,可以调用它来改变音调on/off,改变频率,改变波形。我遵循了一些我可以在网上找到的示例并得到了以下内容:

beeper.h

#pragma once

#include <SDL.h>
#include <SDL_audio.h>
#include <cmath>
#include "logger.h"

class Beeper {
private:
    //Should there be sound right now
    bool soundOn = true;
    //Type of wave that should be generated
    int waveType = 0;
    //Tone that the wave will produce (may or may not be applicable based on wave type)
    float waveTone = 440;
    //Running index for sampling
    float samplingIndex = 0;

    //These are useful variables that cannot be changed outside of this file:
    //Volume
    const Sint16 amplitude = 32000;
    //Sampling rate
    const int samplingRate = 44100;
    //Buffer size
    const int bufferSize = 1024;

    //Samples a sine wave at a given index
    float sampleSine(float index);
    //Samples a square wave at a given index
    float sampleSquare(float index);
public:
    //Initializes SDL audio, audio device, and audio specs
    void initializeAudio();
    //Function called by SDL audio_callback that fills stream with samples
    void generateSamples(short* stream, int length);
    //Turn sound on or off
    void setSoundOn(bool soundOnOrOff);
    //Set timbre of tone produced by beeper
    void setWaveType(int waveTypeID);
    //Set tone (in Hz) produced by beeper
    void setWaveTone(int waveHz);
};

beeper.cpp

#include <beeper.h>

void fillBuffer(void* userdata, Uint8* _stream, int len) {
    short * stream = reinterpret_cast<short*>(_stream);
    int length = len;
    Beeper* beeper = (Beeper*)userdata;

    beeper->generateSamples(stream, length);
}

void Beeper::initializeAudio() {
    SDL_AudioSpec desired, returned;
    SDL_AudioDeviceID devID;

    SDL_zero(desired);
    desired.freq = samplingRate;
    desired.format = AUDIO_S16SYS; //16-bit audio
    desired.channels = 1;
    desired.samples = bufferSize;
    desired.callback = &fillBuffer;
    desired.userdata = this;

    devID = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0,0), 0, &desired, &returned, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
    SDL_PauseAudioDevice(devID, 0);
}

void Beeper::generateSamples(short *stream, int length) {
    int samplesToWrite = length / sizeof(short);
    for (int i = 0; i < samplesToWrite; i++) {
        if (soundOn) {
            if (waveType == 0) {
                stream[i] = (short)(amplitude * sampleSine(samplingIndex));
            }
            else if (waveType == 1) {
                stream[i] = (short)(amplitude * 0.8 * sampleSquare(samplingIndex));
            }
        }
        else {
            stream[i] = 0;
        }
        //INFO << "Sampling index: " << samplingIndex;
        samplingIndex += (waveTone * M_PI * 2) / samplingRate;
        //INFO << "Stream input: " << stream[i];
        if (samplingIndex >= (M_PI*2)) {
            samplingIndex -= M_PI * 2;
        }
    }
}

void Beeper::setSoundOn(bool soundOnOrOff) {
    soundOn = soundOnOrOff;
    //if (soundOnOrOff) {
    //  samplingIndex = 0;
    //}
}

void Beeper::setWaveType(int waveTypeID) {
    waveType = waveTypeID;
    //samplingIndex = 0;
}

void Beeper::setWaveTone(int waveHz) {
    waveTone = waveHz;
    //samplingIndex = 0;
}

float Beeper::sampleSine(float index) {
    double result = sin((index));
    //INFO << "Sine result: " << result;
    return result;
}

float Beeper::sampleSquare(float index)
{
    int unSquaredSin = sin((index));
    if (unSquaredSin >= 0) {
        return 1;
    }
    else {
        return -1;
    }
}

正在调用回调函数,并且 generateSamples 函数正在将数据加载到流中,但除了不规则的轻微咔嗒声外,我什么也听不到。我查看了流中的数据,它遵循我期望的频率为 440 Hz 的缩放正弦波的模式。有什么明显的我想念的吗?我确实注意到流的大小是我在声明 SDL_AudioSpec 和调用 SDL_OpenAudioDevice 时输入的大小的两倍。这是为什么?

回答了我自己的问题!打开音频设备时,我使用了标志 SDL_AUDIO_ALLOW_FORMAT_CHANGE,这意味着 SDL 实际上使用的是浮动缓冲区,而不是我预期的短缓冲区。这在几个难以检测的地方引起了问题(流的字节数是我预期的两倍应该让我失望)。我将 SDL_OpenAudioDevice() 中的参数更改为 0 并且它按预期工作!