Generating a sine wave: incorrect output

726 Views Asked by At

I'm trying to generate a simple sine wave in rtaudio to verify I understand what's going on. However, it is coming out wrong.

I have a global float timec, and a callback invoked with openStream which should fill a buffer with samples.

float freq = 440; // center frequency
int SAMPLE_RATE = 44100;
for (int i = 0; i < numFrames; i++) {
    float v = sin(2 * M_PI * freq * (timec / SAMPLE_RATE));
    outputbuffer[i] = v;
    timec++;
}

What have I done wrong? Instead of a sine wave, I hear a low grinding sound.

1

There are 1 best solutions below

0
On

Answering my own question.

Rtaudio handles the output buffer in a tricky way. The array of floats is not for a single mono channel, it contains slots representing one frame for each channel, followed by several slots for the next frame, etc. The key correction, since this output has 2 channels, is:

outputbuffer[2 * i] = v;

to output on just one channel.