// Copyright 2006, Thomas Scott Stillwell
// All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are permitted 
//provided that the following conditions are met:
//
//Redistributions of source code must retain the above copyright notice, this list of conditions 
//and the following disclaimer. 
//
//Redistributions in binary form must reproduce the above copyright notice, this list of conditions 
//and the following disclaimer in the documentation and/or other materials provided with the distribution. 
//
//The name of Thomas Scott Stillwell may not be used to endorse or 
//promote products derived from this software without specific prior written permission. 
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
//IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
//BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
//STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
//THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
desc:compressor/limiter, similar to F670

slider1:0<-60,0,0.1>Threshold (dB)
slider2:70<0.1,100,0.1>Bias
slider3:0<-30,30,0.1>Makeup gain
slider4:0<0,1,1{Left/Right,Lat/Vert}>AGC
slider5:1<1,6,1>Time Constant
slider6:100<1,10000,1>Level detector RMS window
//slider7:1<1,50,0.1>Current comp. ratio
//slider8:0<-90,0,0.1>Gain reduction

@init
  log2db = 8.6858896380650365530225783783321; // 20 / ln(10)
  db2log = 0.11512925464970228420089957273422; // ln(10) / 20 
  i=0;
  attime=0.0002; //200us
  reltime=0.300; //300ms
  rmstime=0.000050; //50us
  maxover=0;
  ratio=0;
  cratio=0;
  rundb=0;
  overdb=0;
  maxover=0;
  atcoef=exp(-1/(attime * srate));
  relcoef=exp(-1/(reltime * srate));
  rmscoef=exp(-1/(rmstime * srate));
  leftright = 0;
  latvert = 1;
  gr_meter=1;
  gr_meter_decay = exp(1/(1*srate));

@slider
  thresh = slider1;
  threshv = exp(thresh * db2log);
  ratio = 20;
  bias = 80 * slider2 / 100;
  cthresh = thresh - bias;
  cthreshv = exp(cthresh * db2log);
  makeup = slider3;
  makeupv = exp(makeup * db2log);
  agc = slider4;
  timeconstant = slider5;
  timeconstant == 1 ? (
    attime = 0.0002;
    reltime = 0.300;
  );
  timeconstant == 2 ? (
    attime = 0.0002;
    reltime = 0.800;
  );
  timeconstant == 3 ? (
    attime = 0.0004;
    reltime = 2.000;
  );
  timeconstant == 4 ? (
    attime = 0.0008;
    reltime = 5.000;
  );
  timeconstant == 5 ? (
    attime = 0.0002;
    reltime = 10.000;
  );
  timeconstant == 6 ? (
    attime = 0.0004;
    reltime = 25.000;
  );
  atcoef = exp(-1 / (attime * srate));
  relcoef = exp(-1 / (reltime * srate));
  
  rmstime = slider6 / 1000000; 
  rmscoef=exp(-1/(rmstime * srate));

@sample
  agc == leftright ? (
    aspl0 = abs(spl0);
    aspl1 = abs(spl1);
  ) : (  
    aspl0 = abs(spl0+spl1)/2;
    aspl1 = abs(spl0-spl1)/2;
  );

  maxspl = max(aspl0, aspl1);
  maxspl = maxspl * maxspl;

  runave = maxspl + rmscoef * (runave - maxspl);
  det = sqrt(max(0,runave));

  overdb = 2.08136898 * log(det/threshv) * log2db;
  overdb = max(0,overdb);

  overdb > rundb ? (
    rundb = overdb + atcoef * (rundb - overdb);
  ) : (
    rundb = overdb + relcoef * (rundb - overdb);
  );
  overdb = max(rundb,0);

  bias == 0 ? (
    cratio = ratio;
  ) : (
    cratio = 1 + (ratio-1) * sqrt((overdb + dcoffset) / (bias + dcoffset));
  );
  //slider7 = cratio;
  
  gr = -overdb * (cratio-1)/cratio;
  //slider8 = -gr;
  grv = exp(gr * db2log);
  
  grv < gr_meter ? gr_meter=grv : ( gr_meter*=gr_meter_decay; gr_meter>1?gr_meter=1; );

  agc == leftright ? (
    spl0 *= grv * makeupv;
    spl1 *= grv * makeupv;
  ) : (
    sav0 = (spl0 + spl1) * grv;
    sav1 = (spl0 - spl1) * grv;
    spl0 = makeupv * (sav0 + sav1) * 0.5;
    spl1 = makeupv * (sav0 - sav1) * 0.5;
  );

@gfx 0 36 // request horizontal/vertical heights (0 means dont care)

  gr_meter *= exp(1/30); gr_meter>1?gr_meter=1; // decay meter here so if the audio processing stops it doesnt "stick"
  gfx_r=1; gfx_g=gfx_b=0; gfx_a=0.8;
  
  meter_bot=20;
  meter_h=min(gfx_h,gfx_h-(gfx_h*0.2));
  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.0; 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;
  );

  //Separation Line
  gfx_x=0; gfx_y=meter_h;
  gfx_lineto(gfx_w,gfx_y,0);

  gfx_a=1;
  gfx_x=0; gfx_y=meter_h/2 - gfx_texth/2;
  gfx_drawnumber(log10(gr_meter)*20,1);
  gfx_drawchar($'d');
  gfx_drawchar($'B');

  //Ratio Info
  gfx_x=(gfx_w/2)-28; gfx_y=meter_h + (gfx_h*0.2)/2  - gfx_texth/2;
  gfx_drawchar($'R');
  gfx_drawchar($'a');
  gfx_drawchar($'t');
  gfx_drawchar($'i');
  gfx_drawchar($'o');
  gfx_drawchar($':');
  gfx_drawchar($' ');
  gfx_drawnumber(cratio,1);

