// midi triggered gate

slider1:0<0,20,0.1>Attack (ms)
slider2:10<0,500,0.1>Release (ms)
slider3:0<-24,24,0.1>Output (dB)
slider4:0<0,1,1{open gate,close gate}>Trigger Mode
slider5:36<0,127,1>Low Note
slider6:47<0,127,1>High Note

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

@init
ext_noinit = 1;

silentcnt = 0;
seekv = 1;
seekto = 1;
sillen = 0;

noteOn = 9*16;
amount = 1;

@slider
att = max(min((slider1),20),0.1);
rel = max(min((slider2),500),0.1);
attack = 1/pow(10,1/(srate*att/1000));
release = 1/pow(10,1/(srate*rel/1000));

output = 10^(slider3/20);

notelow = slider5;
notehigh = slider6;

@block
while (
midirecv(offset,msg1,msg23) ? (
status = msg1&240;
velocity = ((msg23/256) & 127);
note = msg23&127;
(note >= notelow) && (note <= notehigh) ? (
status == noteOn && velocity ? (
lower = amount;
): status != noteOn || (status == noteOn && !velocity) ? (
lower = 0;
);
);
midisend(offset,msg1,msg23); // pass MIDI events
);
);

@sample
in0=min(max(spl0,-1),1);
in1=min(max(spl1,-1),1);

slider4 == 1 ? (
a=abs(in0) > lower || abs(in1) > lower;
):(
a=abs(in0) < lower || abs(in1) < lower;
);

a ? (
silentcnt=0;
seekto=1;
):(
(silentcnt+=1) > sillen ? seekto=0;
);

seekto > 0.5 ? ( // fading in
seekv=seekv*attack + (1-attack);
):( // fading out
seekv=seekv*release;
);

spl0*=seekv*output;
spl1*=seekv*output;
