Non-silent audio when panning


#1

Probably a simple answer to this, but I can’t seem to find it. When using the code below to do some super simple panning, when a channel reaches hard left or hard right, there is still a very small audio output on the opposite channel still. Debugging shows that the value for currentSample drifts between 0 and -0. Any suggestions? C

 const float currentSample = (float) std::sin (currentAngle);
 buffer->addSample (leftChannel, sampleIndex, currentSample * (1.0f - pan));
 buffer->addSample (rightChannel, sampleIndex, currentSample * pan);;


void AudioEngine::panLeft()
{
    if (pan > 0)
    {
        pan -= 0.1;
    }
    if (pan > 0 && pan < 0.1)
    {
        pan = 0;
    }
}

#2

Try removing the "pan > 0 && " from the second if statement. It’s not needed because if pan is negative you still want it set to zero. You may be getting a slightly negative pan value due to floating point precision error.