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

desc: pitch down
slider1:1<0,6,1>octaves down (0..6)
slider2:0<0,11,1>semitones down (0..11)
slider3:0<0,99,1>cents down (0..99)
slider4:100<4,500,10>chunk size (ms)
slider5:1<0.001,1>overlap size (0..1)
slider6:0<-120,6,1>wet mix (dB)
slider7:-120<-120,6,1>dry mix (dB)

@slider
  lbufsize=bufsize; bufsize=(srate*0.001*slider4)&65534; 
  lbufsize!=bufsize ? bufpos=0;
  scl=2 ^ (-(slider1 + slider2/12 + slider3/1200)); 
  rilen=(max(scl,0.5)*bufsize)|0; 
  rrilen=((scl*bufsize)|0)*2; 
  slider5=min(1,max(slider5,0)); 
  rspos=slider5*(bufsize-rilen); 
  invbs=1/rspos;
  drymix=2 ^ (slider7/6); 
  wetmix=2 ^ (slider6/6);

@sample
ss0=spl0; ss1=spl1;

hbp=((bufpos * scl)|0)*2;

// pre read these before writing
s0r=rrilen[hbp];
s1r=rrilen[hbp+1];

(bufpos*2)[0]=spl0;
(bufpos*2)[1]=spl1;

bufpos < rspos ? 
(
     // mix
     sc=bufpos*invbs;
     spl0=hbp[0]*sc + s0r*(1-sc);
     spl1=hbp[1]*sc + s1r*(1-sc);
) : (
    // straight resample
     spl0=hbp[0]; 
     spl1=hbp[1];
);

(bufpos+=1) >= bufsize ? bufpos=0;

spl0 = spl0*wetmix + ss0*drymix;
spl1 = spl1*wetmix + ss1*drymix;
