// This effect Copyright (C) 2004 and later Cockos Incorporated
// License: LGPL - http://www.gnu.org/licenses/lgpl.html

desc:Distortion
//tags: guitar distortion waveshaper
//author: Cockos

slider1:20<0,50,1>Gain (dB)
slider2:6<1,10,1>Hardness
slider3:-12<-40,0,1>Max Volume (dB)
slider4:0<0,2,1{Left,Right,Stereo}>Channel Mode

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

@init
@slider
preamp=2^(slider1/6);
soft=2^slider2;
maxv=2 ^ (slider3/6);

//maxv=0.5 * (soft+maxv - sqrt((soft+maxv)*(soft+maxv)-4*(maxv*(soft-1)-1)));

@sample

slider4 != 1 ? (
spl0*=preamp;
t=abs(spl0);
t > maxv ? 
(
  s=sign(spl0);
  diff=t-maxv;
  spl0=s*(maxv + diff/(soft+diff));
);

);

slider4 != 0 ? (
spl1*=preamp;
t=abs(spl1);
t > maxv ? 
(
  s=sign(spl1);
  diff=t-maxv;
  spl1=s*(maxv + diff/(soft+diff));
);

);
