// This effect Copyright (C) 2004 and later Cockos Incorporated
// License: GPL - http://www.gnu.org/licenses/gpl.html
desc: autosustaining delay
slider1:120<0,4000,20>length (ms)
slider2:-44<-120,6,1>threshold
slider3:10<0,1000,1>attack (ms)
slider4:10<0,1000,1>release (ms)
slider5:0<-120,0,1>maximum mixin
slider6:0<-120,6,1>output wet (dB)
slider7:0<-120,6,1>output dry (dB)

@init
delaypos=0;
env=0;

@slider
odelay=delaylen;
delaylen=min(slider1 * srate / 1000,500000);
odelay != delaylen ? (
  delaypos >= delaylen ? delaypos = 0;
  freembuf(delaylen*2);
);
maxmix = 10^(slider5/20);
wetmix2 = 10^(slider6/20);
drymix2 = 10^(slider7/20);

thresh = 10^(slider2/20);
tmp = slider3*0.001;
tmp==0 ? tmp=0.0001;
denv = exp(-1/(tmp*srate));
tmp = slider4*0.001;
tmp==0 ? tmp=0.0001;
denvr = exp(-1/(tmp*srate));

@sample

a = max(abs(spl0),abs(spl1));

env = a > env ? env*denv + a*(1-denv) : env*denvr + a*(1-denvr);

dpint = delaypos*2;
os1=dpint[0];
os2=dpint[1];

sc=0.0;
env > thresh ? (
  a = env/thresh; // ~6dB = 2, 12dB over=4
  sc=a*0.5*maxmix;
  sc>maxmix?sc=maxmix;
);

dpint[0]=min(max(spl0*sc + os1*(1-sc),-4),4);
dpint[1]=min(max(spl1*sc + os2*(1-sc),-4),4);

(delaypos+=1) >= delaylen ? delaypos=0;

spl0=spl0*drymix2 + os1*wetmix2;
spl1=spl1*drymix2 + os2*wetmix2;
