desc:Soft Clipper/Limiter
//tags: processing gain amplitude saturation limiter
//author: Schwa

// for a nice final level boost.

// Modeled after the soft-clipping stage in a popular 
// "tube mastering" plug-in.  This is really just
// a low threshold brick wall limiter that is all knee.

slider1:0<0,9,0.1>Boost (dB)
slider2:0<-3,1,0.1>Output Brickwall (dB)

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

@init

ext_tail_size = -2;

amp_dB = 8.6562;
baseline_threshold_dB = -9.0;
a = 1.017;
b = -0.025;

@slider

boost_dB = slider1;
limit_dB = slider2;
threshold_dB = baseline_threshold_dB + limit_dB;

@sample

dB0 = amp_dB * log(abs(spl0)) + boost_dB;
dB1 = amp_dB * log(abs(spl1)) + boost_dB;

(dB0 > threshold_dB) ? (
  over_dB = dB0 - threshold_dB;
  over_dB = a * over_dB + b * over_dB * over_dB;
  dB0 = min(threshold_dB + over_dB, limit_dB);
);

(dB1 > threshold_dB) ? (
  over_dB = dB1 - threshold_dB;
  over_dB = a * over_dB + b * over_dB * over_dB;
  dB1 = min(threshold_dB + over_dB, limit_dB);
);

spl0 = exp(dB0 / amp_dB) * sign(spl0);
spl1 = exp(dB1 / amp_dB) * sign(spl1);

