applyGainRamp is not working for me

Hi,

I have written a small plugin that includes mute buttons for the left and right channels. You can mute the left channel or the right channel, but not both at the same time.

I use AudioBuffer’s clear method to mute the audio.

//works, but sometimes generates tiny pops
        if (leftChannel) {
            buffer.clear (1, 0, buffer.getNumSamples());
        } else if (rightChannel) {
            buffer.clear (0, 0, buffer.getNumSamples());
        }

When I mute a channel, sometimes there is a tiny pop in the audio. It is not big and I could live with it, but I want the muting to happen smoothly so that there is no pop, ever.

I have been looking at applyGainRamp, but I cannot make it work, and I don’t know if it is even the right move.

//fail, makes digital noise
        if (leftChannel) {
            buffer.applyGainRamp (1, 0, buffer.getNumSamples(), buffer.getMagnitude (1, 0,  buffer.getNumSamples()), 0.0);
        } else if (rightChannel) {
            buffer.applyGainRamp (0, 0, buffer.getNumSamples(), buffer.getMagnitude (0, 0,  buffer.getNumSamples()), 0.0);
        }

Can you please put me on the right track?

Best regards,
Fred

Here is how it looks with an IIR solution

float leftChannelGainState=1; 
float rightChannelGainState = 1; 

void process(...)
{
auto l=buffer.getWritePointer(0);
 auto r=buffer.getWritePointer(1);
 int    numS=buffer.getNumSamples()
 
float  factor=0.05; // higer values will make faster ramps

float targL  = leftChannel ? 0 : 1;
float targR  = rightChannel ? 1 : 0;
{
  for(int s=0; s<numS;s++)
  {
     leftChannelGainState   +=  factor*( targL - leftChannelGainState); // gently un/mute left
     rightChannelGainState +=  factor*( targR - rightChannelGainState); //gently un/mute right
     l[s]*= leftChannelGainState; // apply
     r[s]*= rightChannelGainState; //aply
  }
}
}