[sharingisfun] js based LFO

Peter Nyboer's icon

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:
, , ,, ,
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 ----------------- */

Max Patch
Copy patch and select New From Clipboard in Max.

/* -------lfo.help---------*/

Peter Nyboer's icon

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

Peter Nyboer's icon

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.