// Stereo Gate.
// Optional: Gates the left and right channel individually.

slider1:-120<-120,0,0.1>Threshold (dB)
slider2:0.5<0.01,10,0.01>Attack (ms)
slider3:100<10,1000,1>Release (ms)
slider4:50<10,1000,1>Env Decay (ms)
slider5:0<-24,24,0.1>Output (dB)
slider6:0<0,1,1{1x envelope,3x envelope}>Env Mode
slider7:0<0,1,1{Mono (Sum of Left+Right),Stereo (Left+Right)}>Detection Mode

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

@init
gain = 1;
env0 = 0;
env1 = 0;
env2 = 0;

gainA = 1;
gainB = 1;
env0A = 0;
env1A = 0;
env2A = 0;
env0B = 0;
env1B = 0;
env2B = 0;

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

attack = exp(-1/(slider2/1000*srate));
release = exp(-1/(slider3/1000*srate));
envelope_decay0 = exp(-1/((10+(slider4*0.9))/1000*srate));
envelope_decay1 = exp(-1/((10+(slider4*0.6))/1000*srate));
envelope_decay2 = exp(-1/((10+(slider4*0.3))/1000*srate));

vol = 10^(slider5/20);

hard = slider6;

stereo = slider7;

@sample
inL = spl0;
inR = spl1;

stereo ? (

detA = max(abs(inL),abs(inL));
detA += 0.000000000001;

hard ? (
env2A = detA >= env2A ? detA : detA+envelope_decay0*(env2A-detA);
):(
env0A = detA >= env0A ? detA : detA+envelope_decay0*(env0A-detA);
env1A = env0A >= env1A ? env0A : env0A+envelope_decay1*(env1A-env0A);
env2A = env1A >= env2A ? env1A : env1A+envelope_decay2*(env2A-env1A);
);

transfer_gainA = env2A > threshold ? pow(env2A,0):0;
gainA = transfer_gainA > gainA ? transfer_gainA+attack*(gainA-transfer_gainA) : transfer_gainA+release*(gainA-transfer_gainA);

detB = max(abs(inR),abs(inR));
detB += 0.000000000001;

hard ? (
env2B = detB >= env2B ? detB : detB+envelope_decay0*(env2B-detB);
):(
env0B = detB >= env0B ? detB : detB+envelope_decay0*(env0B-detB);
env1B = env0B >= env1B ? env0B : env0B+envelope_decay1*(env1B-env0B);
env2B = env1B >= env2B ? env1B : env1B+envelope_decay2*(env2B-env1B);
);

transfer_gainB = env2B > threshold ? pow(env2B,0):0;
gainB = transfer_gainB > gainB ? transfer_gainB+attack*(gainB-transfer_gainB) : transfer_gainB+release*(gainB-transfer_gainB);

spl0 = inL * gainA * vol;
spl1 = inR * gainB * vol;

) : (

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

hard ? (
env2 = det >= env2 ? det : det+envelope_decay0*(env2-det);
):(
env0 = det >= env0 ? det : det+envelope_decay0*(env0-det);
env1 = env0 >= env1 ? env0 : env0+envelope_decay1*(env1-env0);
env2 = env1 >= env2 ? env1 : env1+envelope_decay2*(env2-env1);
);

transfer_gain = env2 > threshold ? pow(env2,0):0;
gain = transfer_gain > gain ? transfer_gain+attack*(gain-transfer_gain) : transfer_gain+release*(gain-transfer_gain);

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

);

@gfx 0 70
x = 40;
y = 40;

function draw_led(x,y,size)(
gfx_r=gfx_b=gfx_g=1; gfx_a=0.25;
gfx_circle(x, y,size,1,1);
gfx_r=gfx_b=gfx_g=0.8;
gfx_a=0.8;
gfx_arc(x, y,size-(size/5),1,4,1);
gfx_a=1;
);

stereo ? (

// LEDs Left
draw_led(x+5, y-5,20);
draw_led(x+50, y-5,20);
draw_led(x+95, y-5,20);

gainA>=0.99 ? (
gfx_g=1; gfx_b=gfx_r=0;
gfx_circle(x+5, y-5,15,1,1);
);
gainA>0.1 && gainA<0.99 ? (
gfx_r=1; gfx_b=0; gfx_g=1;
gfx_circle(x+50, y-5,15,1,1);
);
gainA<=0.1 ? (
gfx_r=1; gfx_b=gfx_g=0;
gfx_circle(x+95, y-5,15,1,1);
);

// LEDs Right
draw_led(x+175, y-5,20);
draw_led(x+220, y-5,20);
draw_led(x+265, y-5,20);

gainB>=0.99 ? (
gfx_g=1; gfx_b=gfx_r=0;
gfx_circle(x+175, y-5,15,1,1);
);
gainB>0.1 && gainB<0.99 ? (
gfx_r=1; gfx_b=0; gfx_g=1;
gfx_circle(x+220, y-5,15,1,1);
);
gainB<=0.1 ? (
gfx_r=1; gfx_b=gfx_g=0;
gfx_circle(x+265, y-5,15,1,1);
);

) : (

// LEDs Mono
draw_led(x+5, y-5,20);
draw_led(x+50, y-5,20);
draw_led(x+95, y-5,20);

gain>=0.99 ? (
gfx_g=1; gfx_b=gfx_r=0;
gfx_circle(x+5, y-5,15,1,1);
);
gain>0.1 && gain<0.99 ? (
gfx_r=1; gfx_b=0; gfx_g=1;
gfx_circle(x+50, y-5,15,1,1);
);
gain<=0.1 ? (
gfx_r=1; gfx_b=gfx_g=0;
gfx_circle(x+95, y-5,15,1,1);
);

);
