desc:Fast cosine via square wave interpolation
slider1:440<20,20000,1>Frequency (Hz)
slider2:2<0,4,1{Round to Nearest,Linear Interpolation,Cubic Interpolation,Quintic Interpolation,Cosine}>Function
slider3:100<1,1000,1>Loop Count

@slider

dt = slider1 / srate;
f = slider2|0;
n = slider3|0;

@sample

// Round to Nearest
f == 0 ? (
  stack_push(t);
  loop(n,
    t = stack_peek();

    (x = t + 0.25) >= 1 ? x -= 1;
    (t += dt) >= 1 ? t -= 1;
    y = x < 0.5 ? 1 : -1;
  );
  stack_pop();
) :

// Linear Interpolation
f == 1 ? (

  stack_push(t);
  loop(n,
    t = stack_peek();

    (x = 2*t) < 1 ? y = 1 : x += y = -1;
    (t += dt) >= 1 ? t -= 1;
    y *= 1 - 2*x;
  );
  stack_pop();
) :

// Cubic Interpolation
f == 2 ? (
  stack_push(t);
  loop(n,
    t = stack_peek();

    (x = 2*t) < 1 ? y = 1 : x += y = -1;
    (t += dt) >= 1 ? t -= 1;
    y *= (4*x - 6) * sqr(x) + 1;
  );
  stack_pop();
) :

// Quintic Interpolation
f == 3 ? (
  stack_push(t);
  loop(n,
    t = stack_peek();

    (x = 2*t) < 1 ? y = 1 : x += y = -1;
    (t += dt) >= 1 ? t -= 1;
    y *= ((5 - 2*x) * sqr(x) - 5) * sqr(x) + 1;
  );
  stack_pop();
) :

// Cosine
(
  stack_push(t);
  loop(n,
    t = stack_peek();

    y = cos(2*$pi * t);
    (t += dt) >= 1 ? t -= 1;
  );
  stack_pop();
);

spl0 = spl1 = 0.25 * y;
