/*
This plugin can add some punch to single drum hits (like kicks or snares...)

Based on a tutorial i saw on youtube where someone achieved the same result
by using several tracks and some complicated track routings...
Now you can do similar stuff with just a single plugin...

Internal routing:
-------------------
Wet : Dry (phase inverted) + Dry (compressed)
Output : Dry (dry mix gain) + Wet

How to:
----------
- If the 'Dry Mix Gain' slider is at -6dB (which is -inf)
  and the 'Threshold' slider is at +0dB, you will hear nothing.
- If the 'Dry Mix Gain' slider is at +0dB
  this plugin acts as a normal compressor.

So set the 'Dry Mix Gain' slider to -6dB and adjust the 'Threshold'.
Adjust the 'Ratio', 'Attack', 'Release' and 'Env Decay' sliders.
Now bring in the 'Dry Mix Gain' slider again...
Find the sweet spot where you get the optimal punch...

Tip:
-----
This plugin can also act as a gate with the right settings.
(e.g. to reduce cymbals bleeding into a kick/snare bus)

- Set the 'Dry Mix Gain' slider to +0dB
- Set a high 'Ratio' (20)
- Adjust the 'Threshold'
- Move down the 'Dry Mix Gain' slider
- Play with the 'Release' time + 'Env Decay' time sliders
  and try to find the sweet spot for the 'Dry Mix Gain' slider
The result will be that you will hear less cymbals and that
the kick/snare will have more punch.
...you really need to find the right balance for all the sliders
to get the best results...
*/

slider1:0<-30,0,0.1>Threshold (dB)
slider2:2<1,20,0.1>Ratio
slider3:3<1,20,0.1>Attack (ms)
slider4:100<20,1000,1>Release (ms)
slider5:50<10,100,1>Env Decay (ms)
slider6:0<-6,0,0.1>Dry Mix Gain (dB)
slider7:0<-24,24,0.1>Output (dB)

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

@init
output = 1;
transfer_A = 0;
transfer_B = 1;
gain = 1;
env = 0;

log2dB = 8.6858896380650365530225783783321;
db2log = 0.11512925464970228420089957273422;
gr_meter = 1;
gr_meter_decay = exp(1/(1*srate));

@slider
threshold = 10^(slider1/20);

transfer_A = (1/slider2)-1;
transfer_B = output * pow(threshold,-transfer_A);

attack = exp(-1/(slider3/1000*srate));
release = exp(-1/(slider4/1000*srate));
envelope_decay = exp(-1/(slider5/1000*srate));

drygain = slider6 <= -6 ? 0.0 : 10^(slider6/20);
outgain = 10^(slider7/20);

@sample
inL = spl0;
inR = spl1;

det = max(abs(inL),abs(inR));
det += 0.000000000001;

env = det >= env ? det : det+envelope_decay*(env-det);
transfer_gain = env > threshold ? pow(env,transfer_A)*transfer_B:output;
gain = transfer_gain < gain ? transfer_gain+attack*(gain-transfer_gain) : transfer_gain+release*(gain-transfer_gain);

outL = inL*-1 + inL * gain;
outR = inR*-1 + inR * gain;

spl0 = (inL*drygain + outL)*outgain;
spl1 = (inR*drygain + outR)*outgain;

gr = log(gain)*log2dB;
grv = exp(gr*db2log);
grv < gr_meter ? gr_meter=grv : ( gr_meter*=gr_meter_decay; gr_meter>1?gr_meter=1; );
grmin = min( min(gr,gr) , grmin);

@gfx 0 36
gfx_r=1; gfx_g=gfx_b=0; gfx_a=0.8;

meter_bot=21;
meter_h=min(gfx_h,gfx_h-4);
xscale=gfx_w*20/meter_bot;

gfx_y=0;
gfx_x=gfx_w + log10(gr_meter)*xscale;
gfx_rectto(gfx_w,meter_h);

gfx_r=gfx_g=gfx_b=1; gfx_a=0.6;

s2=sqrt(2)/2;
g = s2;
while(
gfx_x=gfx_w + log10(g)*xscale;
gfx_x >= 0 ? (
gfx_y=0;
gfx_lineto(gfx_x,meter_h,0);
gfx_y=meter_h-gfx_texth;
gfx_x+=2;
gfx_drawnumber(log10(g)*20,0);
gfx_drawchar($'d');
gfx_drawchar($'B');
);
g*=s2;
gfx_x >=0;
);

(mouse_cap) ? (grmin=0;);
gfx_x=2;
gfx_y=meter_h/2 - gfx_texth/2;
gfx_drawnumber(grmin,1);
