Forums > Javascript

[sharingisfun] js based LFO


May 1, 2007 | 7:43 am

Here is a javascript based "lfo" and help patch with lots of features.
Probably best for jitter or things that don’t require SuperDuperAccuracy. Great for testing streams of data, too.

/* —————– BEGIN lfo.js SCRIPT —————– */
/*
LFO generates a continuous ramp of numbers for a specified range, offset, duration and mode.
arguments:
, , ,, <0=float output, 1=integer output>,
if only one is specified, that is the max value, and lfo will sweep from 0 to max
*/

outlets = 2;
inlets = 5;

autowatch = 1;
var taskinterval = 20;
var lfo = new Task(calclfo,this);
var clock = 0;
var dur = 1000;
var rmin = 0.;
var rmax = 1.;
var intmode = 0;
var sinemode = 0.;
var cosmode = 0.;
var mode = 0;
var invertit = 0;
const PI = 3.14159;

// process arguments
if (jsarguments.length==2) //if only 1 arg is given, it is max value
rmax = jsarguments[1];
if (jsarguments.length>2)
rmin = jsarguments[1];
rmax = jsarguments[2];
if (jsarguments.length>3)
dur = jsarguments[3];
if (jsarguments.length>4)
mode = jsarguments[3];
if (jsarguments.length>5)
intmode = jsarguments[5];
if (jsarguments.length>6)
invertit = jsarguments[6];

function msg_float(v){
if (inlet == 1) rmin = v;
if (inlet == 2) rmax = v;
if (inlet == 3) dur = v;
if (inlet == 4) mode = v;
}

function rate(v){
dur = v;
}
function calclfo(){
var lfoout;
var time = ((taskinterval*clock)%dur);
var src = time/dur;
var range = rmax-rmin;

lfoout = (src*range)+rmin;
switch(mode){
case 0:
lfoout = (src*range)+rmin;
break;
case 1:
lfoout = (range*Math.sin(src*PI))+rmin;
break;
case 2:
var halfway = dur/2;

if (time
lfoout = (2*src*range)+rmin;
}else{
var srctmp = (halfway-(time-halfway))/dur;
lfoout = (2*srctmp*range)+rmin;
}
break;
}

if (intmode) lfoout = Math.floor(lfoout);

if(!invertit)
outlet (0,lfoout);
else
outlet (0,rmax-lfoout+rmin);
if (time == dur-taskinterval) outlet(1,"bang");
clock++;
}
//calclfo.immediate = 1;

function onoff(v){
lfo.interval = taskinterval;
if (v){
lfo.repeat();
}else{
lfo.cancel();
}
}

function range(a,b){
if(a
rmin = a;
rmax = b;
}else{
rmin = b;
rmax = a;
}
}

function toint(v){
intmode = v;
}

function outmode(v){ //saw ,sine, tri
mode = v;
}
function invert(v){
invertit = v;
}
function grain(v){
taskinterval = v;
if(lfo.running) {
onoff(1)
}
}
/* —————– END lfo.js SCRIPT —————– */

/* ——-lfo.help———*/
#P button 211 192 15 0;
#P window setfont "Sans Serif" 9.;
#P number 459 45 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont Arial 9.;
#P window linecount 1;
#P message 459 65 43 131137545 grain $1;
#P window setfont Arial 12.;
#P comment 424 189 198 131137548 pnyboer at slambassador dot com;
#P window setfont Arial 18.;
#P comment 425 152 181 131137554 javascript-based LFO;
#P window setfont Arial 12.;
#P window linecount 3;
#P comment 11 318 633 131137548 arguments: ; , , , , <0=float output , 1=integer output> , ; if only one is specified , that is the max value , and lfo will sweep from 0 to max;
#P window setfont Arial 9.;
#P window linecount 1;
#P comment 271 214 84 131137545 not too accurate!;
#P comment 261 190 84 131137545 end of cycle bang;
#P newex 138 264 47 131137545 pak 0. 1.;
#P number 253 150 35 9 0 2 3 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 204 150 37 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P comment 243 45 20 131137545 ms;
#P flonum 72 192 37 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 106 150 35 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 155 150 37 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont "Sans Serif" 9.;
#N vpatcher 20 74 182 275;
#P window setfont Arial 9.;
#P newex 68 51 62 131137545 unpack 0. 1.;
#P newex 51 138 33 131137545 zl rev;
#P newex 31 110 44 131137545 gate 2 1;
#P newex 84 94 27 131137545 + 1;
#P newex 84 74 27 131137545 > 1.;
#P inlet 52 30 15 0;
#P outlet 31 159 15 0;
#P connect 3 0 4 0;
#P connect 4 0 0 0;
#P connect 5 0 0 0;
#P connect 4 1 5 0;
#P connect 1 0 4 1;
#P connect 1 0 6 0;
#P connect 6 0 2 0;
#P connect 2 0 3 0;
#P connect 6 1 2 1;
#P pop;
#P newobj 128 285 56 196617 p rangefix;
#P window setfont Arial 9.;
#P newex 58 269 69 131137545 prepend range;
#P newex 115 89 69 131137545 prepend range;
#P message 401 65 46 131137545 invert $1;
#P toggle 401 45 15 0;
#P flonum 204 45 37 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 204 65 39 131137545 rate $1;
#P window setfont "Sans Serif" 9.;
#P user ubumenu 310 45 38 196617 0 1 1 0;
#X add saw;
#X add sine;
#X add tri;
#X prefix_set 0 0 0;
#X pattrmode 1;
#P number 266 45 35 9 0 2 3 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont Arial 9.;
#P message 266 65 58 131137545 outmode $1;
#P user multiSlider 57 209 70 44 0. 1. 1 3193 15 0 0 2 0 0 0;
#M frgb 0 0 0;
#M brgb 255 255 255;
#M rgb2 127 127 127;
#M rgb3 0 0 0;
#M rgb4 37 52 91;
#M rgb5 74 105 182;
#M rgb6 112 158 18;
#M rgb7 149 211 110;
#M rgb8 187 9 201;
#M rgb9 224 62 37;
#M rgb10 7 114 128;
#P flonum 227 212 43 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 228 191 30 131137545 timer;
#P message 353 65 41 131137545 toint $1;
#P toggle 353 45 15 0;
#P flonum 115 45 35 9 0 0 0 2001 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont "Sans Serif" 9.;
#P flonum 159 45 37 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P window setfont Arial 9.;
#P newex 115 65 47 131137545 pak 0. 1.;
#P window setfont "Sans Serif" 9.;
#P message 57 65 48 196617 onoff $1;
#P toggle 57 45 15 0;
#P window setfont Arial 9.;
#P newex 57 171 209 131137545 js lfo.js 0. 1. 1000. 0 0 0;
#P window setfont "Sans Serif" 9.;
#P comment 117 135 237 196617 can also use inlets to set range , rate , and outmode;
#P connect 2 0 3 0;
#P fasten 34 0 1 0 464 125 62 125;
#P fasten 8 0 1 0 358 125 62 125;
#P fasten 3 0 1 0 62 125 62 125;
#P fasten 12 0 1 0 271 125 62 125;
#P fasten 15 0 1 0 209 125 62 125;
#P fasten 18 0 1 0 406 125 62 125;
#P fasten 19 0 1 0 120 125 62 125;
#P connect 1 0 11 0;
#P connect 20 0 11 0;
#P connect 21 0 20 0;
#P connect 1 0 24 0;
#P connect 23 0 1 1;
#P connect 6 0 4 0;
#P connect 4 0 19 0;
#P fasten 4 0 21 0 120 86 30 86 30 257 133 257;
#P connect 28 0 21 0;
#P fasten 23 0 28 0 111 167 32 167 32 257 143 257;
#P connect 5 0 4 1;
#P connect 22 0 1 2;
#P fasten 22 0 28 1 160 167 31 167 31 257 180 257;
#P connect 16 0 15 0;
#P connect 26 0 1 3;
#P fasten 1 1 36 0 261 189 216 189;
#P connect 9 0 10 0;
#P fasten 1 1 9 0 261 190 233 190;
#P fasten 1 1 9 1 261 190 253 190;
#P connect 27 0 1 4;
#P connect 14 0 12 0;
#P connect 13 0 12 0;
#P connect 7 0 8 0;
#P connect 17 0 18 0;
#P connect 35 0 34 0;
#P window clipboard copycount 37;

May 1, 2007 | 3:26 pm

and, for the sake of the archives, as I update it (as I did this morning with a pulse wave and in/out assists), it can be found here:
http://www.yowstar.com/share/lfojs.zip

August 14, 2007 | 9:58 pm

http://www.yowstar.com/share/lfojs.zip
has been updated, and includes event-based triggering and two new lfos – random and "continuous random" aka random triangle ("randangle"?)

Peter.

Viewing 3 posts - 1 through 3 (of 3 total)

Forums > Javascript