I wonder why some tutorials show storing a previousLevel variable and comparing it to see if it has changed, and then deciding whether to applyGain or applyGainRamp, like this:
if (currentGain == previousGain)
{
buffer.applyGain (currentGain);
}
else
{
buffer.applyGainRamp (0, buffer.getNumSamples(), previousGain, currentGain);
previousGain = currentGain;
}
… when the applyGainRamp function checks to see if the startGain and endGain are the same and then just calls applyGain?:
void applyGainRamp (int channel, int startSample, int numSamples,
Type startGain, Type endGain) noexcept
{
if (! isClear)
{
if (startGain == endGain)
{
applyGain (channel, startSample, numSamples, startGain);
}
else
{
jassert (isPositiveAndBelow (channel, numChannels));
jassert (startSample >= 0 && numSamples >= 0 && startSample + numSamples <= size);
const auto increment = (endGain - startGain) / (float) numSamples;
auto* d = channels[channel] + startSample;
while (--numSamples >= 0)
{
*d++ *= startGain;
startGain += increment;
}
}
}
}
It seems you can just call applyGainRamp() with the previousLevel and level and let the function handle it.