// In 'Ring Mod' mode feed it with a sine wave (with very low frequency)
// e.g. ReaSynth sine wave only, tuned very low...
// Sounds also nice if you run different kick drum samples (TR-606,707,808,909, etc.) through it...

desc: Mono

slider1:3<0,3,1{In,Tone,Tone + In,Ring Mod}>mode
slider2:0.5<0,1,0.01>rate
slider3:0.5<0,1,0.01>output

in_pin:L in
in_pin:R in
out_pin:L out
out_pin:R out

@init
twopi = 6.2831853;

max = 512;
buf1 = 0; 
buf2 = max;
memset(buf1, 0, max);
memset(buf2, 0, max);

max = 0;
while(max<511) (
max += 1;
pos = (twopi * max / 511); //generate wavetables
x = 0;
a = 1;
buf[buf2 + max] = sin(pos);

i = 0;
while(i<8)(
i += 1;
// modulo: pos=mod(pos,twopi) ==>
// (pos%twopi)
// for pos...
test1 = (pos / twopi) |0;
test2 = test1 * twopi;
pos = pos - test2;
x += a * sin(pos);
a *= 0.5;
pos *= 2;
);
buf[buf1 + max] = x;
);

buf[buf1 + 511] = 0;
buf[buf2 + 511] = 0; //wrap end for interpolation

pos = 0;
rate = 1;

@slider
fParam0 = slider1; //mode
fParam1 = slider2; //(slider2*1.25)-0.125; //rate
fParam2 = slider3; //level

mode = fParam0;
drate = 1 + 10 * pow(fParam1-0.5,3) / srate;
out = 0.4842 * pow(10, 2 * fParam2 - 1);
//out = 0.19276349 * pow(10, 2 * fParam2 - 1);

@sample
p = pos;
x = max;

a = (spl0+spl1)*0.5;

rate *= drate; 
(rate>2) ? (
rate *= 0.5; 
p *= 0.5;
):
(rate<1) ? (
rate *= 2;
p *= 2; (p>x) ? p -= x;
);

p += rate;
(p>x) ? p -= x;

i1 = p|0; //interpolate position
i2 = i1 + 1;
di = i2 - p;

b = di * ( buf[buf1 + i1] + (rate - 2) * buf[buf2 + i1] );
b += (1 - di) * ( buf[buf1 + i2] + (rate - 2) * buf[buf2 + i2] );
b *= out / rate;

mode==0 ? b = a;
mode==2 ? b += a; //add
mode==3 ? b *= a; //ring mod

spl0 = b;
spl1 = b;

pos = p;

@gfx 0 60
gfx_r=0; gfx_g=0.9; gfx_b=0; gfx_a=1;
gfx_setfont(1,"Arial", 16);

gfx_x =20; gfx_y =10;  gfx_printf("%.0f",(200 * fParam1 - 100) );
gfx_x =70; gfx_y =10;  gfx_drawchar($'%');
gfx_x =110; gfx_y =10;  gfx_printf("Rate");

gfx_x =20; gfx_y =30;  gfx_printf("%.1f",(40 * fParam2 - 20) );
gfx_x =70; gfx_y =30;  gfx_printf("dB");
gfx_x =110; gfx_y =30;  gfx_printf("Output");
