/*
This plugin makes everything above the threshold
louder by the amount of the gain slider.

So adjust the gain and move the threshold slider down.

Tip:
Set attack and release to a high value, 500-1000 ms,
leave env decay at 100 ms for now,
set the gain slider to 60 dB.
Now adjust the threshold slider to barely touch the peaks
of the incoming audio.
Play with the env decay settings. Use the multiplier menu.
Listen...
*/ 

desc: Read info...

slider1:0<0,60,0.1>Gain (dB)
slider2:0<-30,0,0.1>Threshold (dB)
slider3:1<1,1000,1>Attack (ms)
slider4:10<1,1000,1>Release (ms)
slider5:100<10,1000,1>Env Decay (ms)
slider6:3<0,3,1{x 1000,x 100,x 10,x 1}>Env Decay (mult)
slider7:0<0,1,1{off,on}>Sidechain
slider8:0<-24,24,0.1>Output (dB)

in_pin:L in
in_pin:R in
in_pin:L Sidechain
in_pin:R Sidechain
out_pin:L out
out_pin:R out

@init
vol = 1;

@slider
gain = 10^(slider1/20);
thresh = 10^(slider2/20);

attack = exp(-1/(slider3/1000*srate));
release = exp(-1/(slider4/1000*srate));
tmp=10^slider6;
env_decay = exp(-1/(slider5/tmp*srate));

sidechain = slider7;

output = 10^((slider8-slider1)/20);

@sample
inA=max(abs(spl0),abs(spl1));
inB=max(abs(spl0),abs(spl1))*gain;
sgain = (inB/(inA-0.000000000001));

inL = spl0;
inR = spl1;

sidechain == 1 ? (
det = max(abs(spl2),abs(spl3));
):(
det = max(abs(inL),abs(inR));
);
det += 0.000000000001;
env = det >= env ? det : det+env_decay*(env-det);
transfer_gain = env > thresh ? pow(env,0)*sgain : 1;
vol = transfer_gain > vol ? transfer_gain+attack*(vol-transfer_gain) : transfer_gain+release*(vol-transfer_gain);

spl0 = inL * vol * output;
spl1 = inR * vol * output;

spl0 = min(max(spl0,-1),1);
spl1 = min(max(spl1,-1),1);
