// 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:Event Horizon Limiter/Clipper
desc:Event Horizon Limiter/Clipper [Stillwell]
//tags: dynamics limiter
//author: Stillwell

slider1:0.0<-30.0,0.0,0.1>Threshold
slider2:-0.1<-20.0,0.0,0.1>Ceiling
slider3:0<0,1200,1>Release (ms) (0=auto)

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

@init
  ext_tail_size = -1;
  pi = 3.1415926535;
  log2db = 8.6858896380650365530225783783321; // 20 / ln(10)
  db2log = 0.11512925464970228420089957273422; // ln(10) / 20 
  attime=0.004;
  reltime=0.200;
  atcoef=exp(-1/(attime * srate));
  relcoef=exp(-1/(reltime * srate));

@slider
  thresh=exp(slider1 * db2log);
  threshdb=slider1;
  ceiling=exp(slider2 * db2log);
  ceildb=slider2;
  makeup=exp((ceildb-threshdb) * db2log);
  makeupdb=ceildb-threshdb;
  peakdb = ceildb+25;
  peaklvl = exp(peakdb * db2log);
  release=slider3/1000;
  release==0 ? (reltime = overdb / 125) : (reltime = release);
  relcoef=exp(-1/(reltime * srate));


@sample
  spl0=spl0*makeup;
  spl1=spl1*makeup;

  abs0=abs(spl0);
  abs1=abs(spl1);
  overdbv = max(abs0, abs1);
  overdb = 2.08136898 * log(overdbv) * log2db - ceilingdb;

  overdb > rundb ? (                                  // calculate overshoot (and thus GR)
    rundb = overdb + atcoef * (rundb - overdb);       //    use attack and release to 
  ) : (                                               //    smoothly scale gain reduction
    rundb = overdb + relcoef * (rundb - overdb);
  );
  overdb = max(0,rundb);

  release == 0 ? (                                    // Auto-release?  Compute release time
    overdb > maxover ? (
      maxover = overdb;
      reltime = overdb / 125;                         // release at constant 125 dB/sec.
      relcoef = exp(-1/(reltime * srate));
    );
    runmax = maxover + relcoef * (runmax - maxover);  // highest peak for setting att/rel decays in reltime
    maxover = runmax;
  );
  
  gr = exp(overdb * db2log);

  spl0*=gr;
  spl1*=gr;
  
  spl0=min(ceiling,abs(spl0))*sign(spl0);             // If it gets past the limiter, whack it.
  spl1=min(ceiling,abs(spl1))*sign(spl1);             // since we don't lookahead, some stuff will
