// This effect Copyright (C) 2004 and later Cockos Incorporated
// License: GPL - http://www.gnu.org/licenses/gpl.html
desc: ping-pong beatsync delay
slider1:0<0,13000,1>delay (ms) - 0 for beatsync
slider2:-5<-120,6,1>feedback (dB)
slider3:0<-120,6,1>mix in (dB)
slider4:-6<-120,6,1>output wet (dB)
slider5:0<-120,6,1>output dry (dB)
slider6:0<0,100,1>ping-pong width%
slider7:0.25<0.0625,4,0.0625>beatsync - fraction of whole note

@init
  delaypos=0;
  pongloc=0;

@slider
  odelay=delaylen;
  beat = 240 * slider7;
  wetmix = 2 ^(slider2/6);
  drymix = 2 ^(slider3/6);
  wetmix2 = 2 ^(slider4/6);
  drymix2 = 2 ^(slider5/6);
  pongwidth = slider6/100;
  pongpan=(1-pongwidth)/2;

@block
  slider1==0 ? (
    delaylen=min((beat / tempo)*srate,500000);
  ):(
    delaylen=min(slider1 * srate / 1000,500000);
  );

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

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

  switching=0;

  abs(delaypos)<400 ? (
    switch = pongloc ? abs(delaypos)/400 : ((400 - abs(delaypos))/400);
  );

  (delaypos+=1) >= delaylen ? (
    delaypos=0;
    pongloc = (pongloc * -1) + 1;
  );

  os = (os1 + os2) / 2;
  panloc = pongpan + pongwidth * switch;

  spl0=spl0*drymix2 + os*wetmix2*(panloc);
  spl1=spl1*drymix2 + os*wetmix2*(1-panloc);
