Here's how I would approach it:
Step 1: add a slider for it
We define a new slider, up at the top near the others.
Code:
slider4:gain_db=0<-60,12,0.1>Gain (dB)
-60 isn't the same as Infinity, but it's close enough.
If it says -60, we're going to round it down to silence.
Step 2: calculate a gain factor from that slider
We add this code anywhere in the
@slider block. It converts from decibels to a linear multiplication factor:
Code:
gain_factor = gain_db > -60 ? pow(10, gain_db/20) : 0;
As you can (hopefully) see, if the slider is at -60, then it rounds that down to silence.
Step 3: apply this gain to the input or output
We add this code to the
@sample block:
Code:
spl0 *= gain_factor;
spl1 *= gain_factor;
This could go at the very beginning of @sample (i.e. applied to the input), or at the end (i.e. applied to the output).
At this point it should basically work, but it will produce subtle clicks when changing/automating the slider, because the gain is changing too fast, soooo...
Step 4: smooth out the gain changes
The simplest form of smoothing is exponential. First let's calculate an update-factor, based on a 10ms fade - this should go in
@init:
Code:
fade_duration_samples = 0.01*srate;
fade_update_factor = 1/fade_duration_samples; // Accurate enough :)
Then, we add some code to
@sample. This line constantly nudges gain_factor_smoothed
towards gain_factor, at a rate determined by fade_update_factor.
Code:
gain_factor_smoothed += (gain_factor - gain_factor_smoothed)*fade_update_factor;
We then update the code we added in Step 3, so that it uses gain_factor_smoothed instead:
Code:
spl0 *= gain_factor_smoothed;
spl1 *= gain_factor_smoothed;
Step 5: set the initial gain correctly
At the moment, gain_factor_smoothed starts at 0, which means it will fade in at the beginning of playback.
This isn't ideal, so we add the following code to
@init:
Code:
gain_factor_smoothed = gain_db > -60 ? pow(10, gain_db/20) : 0;
This should match the code we added to @slider in Step 2, but it's giving a value to gain_factor_smoothed instead, so that it starts at the same value as gain_factor will have.
I think that should work! I can't test it out right now, because I'm not near my setup, so I apologise for any typos.