Stereo output in getNextAudioBlock() (am I doing this right?)


#1

When I was working on a wavetable demonstration the other day, I used a nested for loop to iterate through the channels and buffer (as in the audio output tutorial), but found I was getting artifacts. I solved the problem by creating a separate write pointer for the left and right channel, but was wondering if I’ve done this the best way?

void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
{
    float* const leftSpeaker = bufferToFill.buffer->getWritePointer(0, bufferToFill.startSample);
    float* const rightSpeaker = bufferToFill.buffer->getWritePointer(1, bufferToFill.startSample);
    
    
    
    for (int sample = 0; sample < bufferToFill.numSamples; ++sample)
    {
        leftSpeaker[sample] = waveTable[(int)phase] * amplitude;
        rightSpeaker[sample] = waveTable[(int)phase] * amplitude;
        updateFrequency();
    }

#2

edit : Realized the loop counter variable name conflicted with the other variable :wink:

If you want to do a nano-optimization :

float audiosample = waveTable[(int)phase] * amplitude;
leftSpeaker[sample] = audiosample;
rightSpeaker[sample] = audiosample;

It probably isn’t necessary at all for release builds, it should be trivial for the compiler to do that optimization anyway.