// 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:1175 Compressor
desc:1175 FAST Attack Compressor with Program-Dependent Release [Stillwell]
//tags: dynamics compressor
//author: Stillwell

slider1:0<-60,0,0.1>Threshold (dB)
slider2:5<0,9,1{Blown Capacitor 4 (Deprecated),Blown Capacitor 8 (Deprecated),Blown Capacitor 12 (Deprecated),Blown Capacitor 20 (Deprecated),Blown Capacitor All (Deprecated),4,8,12,20,All}>Ratio
slider3:0<-20,20,0.1>Gain (dB)
slider4:20<20,2000,10>Attack (uS)
slider5:250<20,1000,1>Release (mS)
slider6:100<0,100,0.1>Mix (%)

in_pin:left input
in_pin:right input
out_pin:left output
out_pin:right output

@init
  log2db = 8.6858896380650365530225783783321; // 20 / ln(10)
  db2log = 0.11512925464970228420089957273422; // ln(10) / 20 
  attime=0.010;
  reltime=0.100;
  ratio=0;
  cratio=0;
  rundb=0;
  overdb=0;
  ratatcoef = exp(-1/(0.00001 * srate));
  ratrelcoef = exp(-1/(0.5 * srate));
  atcoef=exp(-1/(attime * srate));
  relcoef=exp(-1/(reltime * srate));
  mix=1;
  gr_meter=1;
  gr_meter_decay = exp(1/(1*srate));

@slider
  thresh = slider1;
  threshv = exp(thresh * db2log);

  capsc = log2db;
  (rpos = slider2) > 4 ? ( rpos -= 5; ) : ( capsc *= 2.08136898; /* broken capacitor */ );
  ratio = (rpos==0 ? 4 : (rpos==1 ? 8 : (rpos == 2 ? 12 : (rpos == 3 ? 20 : 20 ))));
  rpos == 4 ? (allin=1; cratio=20;) : (allin=0; cratio = ratio;);
  cthresh = (softknee ? (thresh -3) : thresh);
  cthreshv = exp(cthresh * db2log);
  makeup = slider3;
  makeupv = exp((makeup+autogain) * db2log);
  attime = slider4 / 1000000;
  reltime = slider5 / 1000;
  atcoef=exp(-1/(attime * srate));
  relcoef=exp(-1/(reltime * srate));
  mix=slider6/100;

@sample
  ospl0 = spl0;
  ospl1 = spl1;
  aspl0 = abs(spl0);
  aspl1 = abs(spl1);
  maxspl = max(aspl0, aspl1);
  maxspl = maxspl * maxspl;
  runave = maxspl + rmscoef * (runave - maxspl);
  det = sqrt(max(0,runave));

  overdb = max(0, capsc * log(det/cthreshv));

  overdb - rundb > 5 ? (averatio = 4;);

  overdb > rundb ? (
    rundb = overdb + atcoef * (rundb - overdb);
    runratio = averatio + ratatcoef * (runratio - averatio);
  ) : (
    rundb = overdb + relcoef * (rundb - overdb);
    runratio = averatio + ratrelcoef * (runratio - averatio);
  );
  overdb = rundb;
  averatio = runratio;

  allin ? (
    cratio = 12 + averatio;
  ) : (
    cratio = ratio;
  );
  
  gr = -overdb * (cratio-1)/cratio;
  grv = exp(gr * db2log);
  
  runmax = maxover + relcoef * (runmax - maxover);  // highest peak for setting att/rel decays in reltime
  maxover = runmax;

  grv < gr_meter ? gr_meter=grv : ( gr_meter*=gr_meter_decay; gr_meter>1?gr_meter=1; );

  spl0 *= grv * makeupv * mix;
  spl1 *= grv * makeupv * mix;  
  
  spl0 += ospl0 * (1-mix);
  spl1 += ospl1 * (1-mix);


@gfx 0 32 // 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,32);
  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;
  );
  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');
