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


desc:chorus

slider1:15<1,250>chorus length (ms)
slider2:1<1,8,1>number of voices
slider3:0.5<0.1,16>rate (hz)
slider4:0.7<0,1>pitch fudge factor
slider5:-6<-100,12,1>wet mix (dB)
slider6:-6<-100,12,1>dry mix (dB)

@init

bpos=0;

@slider

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

i=0;
loop(numvoices, 
  i[0]=(i+1)/numvoices*$pi; 
  i+=1; 
);

bufofs=4096;

csize=choruslen/numvoices * slider4;

rateadj=slider3*2*$pi/srate;
wetmix = 2 ^ (slider5/6);
drymix = 2 ^ (slider6/6);


@sample

bpos >= choruslen ? (
  bpos=0;
);
os0=spl0;

// calculate new sample based on numvoices

spl0=spl0*drymix;
vol=wetmix/numvoices;
i=0;

loop(numvoices,
   tpos = bpos - (0.5+0.49*sin( i[0] += rateadj)) * (i+1) * csize;

   tpos < 0 ? tpos += choruslen;
   frac=tpos-(0|tpos); 
   ntpos=tpos>=(choruslen-1)?0:tpos+1;
   
   spl0 += (bufofs[tpos]*(1-frac) + bufofs[ntpos]*frac ) * vol;
   i+=1;
);

bufofs[bpos]=os0;
bpos+=1;

spl1=spl0;
