mardi 23 août 2016

JSFX: Teatime's quick sine vs other (fast) sine functions

I was curious about teatime's quick sine function, so I thought I'd write a script that compares his quick sine with a bunch of sine (fast) functions:

Code:

desc:Sine vs sine
slider1:440<20,20000,1>Frequency (Hz)
slider2:0<0,4,1{Quick Sine,Maclaurin Series,State Variable Filter,Sine,Table}>Function
slider3:100<1,1000,1>Loop Count

@init

size = 1024;
i = 0;
loop(size,
  tbl[i] = sin(2*$pi * i / size);
  i += 1;
);

@slider

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

f == 2 ? (
  g1 = tan($pi * min(dt, 0.49));
  g0 = 2/(1 + sqr(g1)) - 1;
  g1 *= g0 + 1;

  s0 = cos(2*$pi * t);
  s1 = sin(2*$pi * t);
);

@sample

x = 2*$pi * t;
x >= $pi ? x -= 2*$pi;

f == 0 ? loop(n,
  y = (4/$pi - 4/($pi*$pi) * abs(x)) * x;
  y = (0.225 * (abs(y) - 1) + 1)*y;
) :

f == 1 ? loop(n,
  y = x*(1 - 1/6*sqr(x) *(1 - 1/20*sqr(x) * (1 - 1/42*sqr(x) * (1 - 1/72*sqr(x)))));
) :

f == 2 ? (
  stack_push(s0); stack_push(s1);
  loop(n - 1,
    tmp = g0*s0 - g1*s1;
    y = s1 = g1*s0 + g0*s1;
    s0 = tmp;
  );
  stack_pop(s1); stack_pop(s0);

  tmp = g0*s0 - g1*s1;
  y = s1 = g1*s0 + g0*s1;
  s0 = tmp;
) :

f == 3 ? loop(n,
  y = sin(x);
) :

f == 4 ? loop(n,
  i = (t * size)|0;
  y = tbl[i];
);

spl0 = spl1 = 0.25 * y;

t += dt;
t -= t|0;

You can add JS: Analysis/gfxanalyzer after this script to compare quality.

IMHO this shows that teatime's quick sine is pretty fast. Its quality is probably not good enough for audio, but it is perfectly suited for LFOs. Cool, thanks teatime!


JSFX: Teatime's quick sine vs other (fast) sine functions

Aucun commentaire:

Enregistrer un commentaire