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

slider1:15<1,500>chorus length (ms)
slider2:1<1,8,1>number of voices
slider3:0.5<0,16,0.01>rate (hz) 0 for beatsync
slider4:0.7<0,1>pitch fudge factor
slider5:-6<-100,12,1>wet mix (dB)
slider6:-6<-100,12,1>dry mix (dB)
slider7:0.0<-1,1,0.01>channel rate offset (hz)
slider8:0.25<0.0625,4,0.0625>beat sync (fraction of whole note)

@init

bpos=0;

@slider

numvoices=min(16,max(slider2,1));
choruslen=slider1*srate*0.001;

i0=0;
i1=0;
i=0;
loop(numvoices, 
  i0[0]=(i+1)/numvoices*$pi; 
  i1[0]=(i+1)/numvoices*$pi;
  i+=1; 
);
bufofs0=32768;
bufofs1=32768;

csize=choruslen/numvoices * slider4;

wetmix = 2 ^ (slider5/6);
drymix = 2 ^ (slider6/6);


@block


slider3==0 ? (
  rateadj=tempo/(240*slider8)*2*$pi/srate;
  rateadj0=tempo/(240*slider8)*2*$pi/srate;
  rateadj1=(tempo/(240*slider8)+slider7)*2*$pi/srate;
):(
  rateadj=slider3*2*$pi/srate;
  rateadj0=slider3*2*$pi/srate;
  rateadj1=(slider3+slider7)*2*$pi/srate;
);

@sample

bpos0 >= choruslen ? (
  bpos0=0;
);
os0=spl0;
bpos1 >= choruslen ? (
  bpos1=0;
);
os1=spl1;

// calculate new sample based on numvoices

spl0=spl0*drymix;
spl1=spl1*drymix;
vol=wetmix/numvoices;
i0=0;
i1=0;

loop(numvoices,
   tpos0 = bpos0 - (0.5+0.49*(sin( $pi*(i0[0] += rateadj0))/($pi*i0[0]))) * (i0+1) * csize;
   tpos1 = bpos1 - (0.5+0.49*(sin( $pi*(i1[0] += rateadj1))/($pi*i1[0]))) * (i1+1) * csize;

   tpos0 < 0 ? tpos0 += choruslen;
   tpos1 < 0 ? tpos1 += choruslen;
   tpos0 > choruslen ? tpos0 -= choruslen;
   tpos1 > choruslen ? tpos1 -= choruslen;

   frac0=tpos0-(0|tpos0);
   tpos02=tpos0>=choruslen-1?0:tpos0+1;
   frac1=tpos1-(0|tpos1);
   tpos12=tpos1>=choruslen-1?0:tpos1+1;

   spl0 += (bufofs0[tpos0]*(1-frac0)+bufofs0[tpos02]*frac0) * vol;
   spl1 += (bufofs1[tpos1]*(1-frac1)+bufofs1[tpos12]*frac1) * vol;

   i0+=1;
   i1+=1;
);


bufofs0[bpos0]=os0;
bufofs1[bpos1]=os1;

bpos0+=1;
bpos1+=1;
