// (C) 2007, Michael Gruhn.

// NO WARRANTY IS GRANTED. THIS PLUG-IN IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
// WARRANTY OF ANY KIND. NO LIABILITY IS GRANTED, INCLUDING, BUT NOT LIMITED TO,
// ANY DIRECT OR INDIRECT,  SPECIAL,  INCIDENTAL OR CONSEQUENTIAL DAMAGE ARISING
// OUT OF  THE  USE  OR INABILITY  TO  USE  THIS PLUG-IN,  COMPUTER FAILTURE  OF
// MALFUNCTION INCLUDED.  THE USE OF THE SOURCE CODE,  EITHER  PARTIALLY  OR  IN
// TOTAL, IS ONLY GRANTED,  IF USED IN THE SENSE OF THE AUTHOR'S INTENTION,  AND
// USED WITH ACKNOWLEDGEMENT OF THE AUTHOR. FURTHERMORE IS THIS PLUG-IN A  THIRD
// PARTY CONTRIBUTION,  EVEN IF INCLUDED IN REAPER(TM),  COCKOS INCORPORATED  OR
// ITS AFFILIATES HAVE NOTHING TO DO WITH IT.  LAST BUT NOT LEAST, BY USING THIS
// PLUG-IN YOU RELINQUISH YOUR CLAIM TO SUE IT'S AUTHOR, AS WELL AS THE CLAIM TO
// ENTRUST SOMEBODY ELSE WITH DOING SO.

// Further more is this released under the GPL License:
// http://www.gnu.org/licenses/gpl.html

desc:Transient Controller

slider1:0<-100,100,1>Attack (%)
slider2:0<-100,100,1>Sustain (%)
slider3:0<-12,6,.1>Output (dB)

@init
b1Env1 = -exp(-30 / srate );
a0Env1 = 1.0 + b1Env1;
b1Env2 = -exp(-1250 / srate );
a0Env2 = 1.0 + b1Env2;
b1Env3 = -exp(-3 / srate );
a0Env3 = 1.0 + b1Env3;

@slider
attack=slider1/100;
sustain=slider2/50;
vol = 2^(slider3/6);

@sample
maxSpls = max(abs(spl0),abs(spl1));
env1 = sqrt(tmpEnv1 = a0Env1*maxSpls - b1Env1*tmpEnv1);
env2 = sqrt(tmpEnv2 = a0Env2*maxSpls - b1Env2*tmpEnv2);
env3 = sqrt(tmpEnv3 = a0Env3*maxSpls - b1Env3*tmpEnv3);

gain = exp(log(max(env2/env1,1))*attack) * exp( log( max(env3/env1,1))*sustain);

spl0 *= (gain *= vol);
spl1 *= gain;
