Skip to content

Tutorials: Simple Automation

jussi-kalliokoski edited this page Sep 8, 2011 · 5 revisions

In this tutorial, we're going to continue from where we left off at Tutorials: Using Effects. Now we're going to dig into some simple automation.

audiolib.js has a high level Automation API to make simple automations very easy to program. Here we're going to create a Step Sequencer and an LFO, and the LFO is going to modulate the Oscillator frequency and the StepSequencer will set the Filter cutoff value.

var dev, osc, flt, lfo, stepSeq;

function audioCallback(buffer, channelCount){
    // Generate automation buffers
    lfo.generateBuffer(buffer.length / channelCount);
    stepSeq.generateBuffer(buffer.length / channelCount);

    // Fill the buffer with the oscillator output.
    osc.append(buffer, channelCount);
    flt.append(buffer);
}

window.addEventListener('load', function(){
    // Create an instance of the AudioDevice class
    dev = audioLib.AudioDevice(audioCallback /* callback for the buffer fills */, 2 /* channelCount */);
    // Create an instance of the Oscillator class
    osc = audioLib.Oscillator(dev.sampleRate /* sampleRate */, 440 /* frequency */);
    // Create an instance of the Filter class
    flt = audioLib.LP12Filter.createBufferBased(2 /* channelCount */, dev.sampleRate, 17000 /* cutoff (in Hz) */, 15 /* resonance */);
    // Set the oscillator wave shape.
    osc.waveShape = 'triangle';

    // Create the LFO
    lfo = audioLib.Oscillator(dev.sampleRate, 0.25 /* frequency */);
    // Create the Step Sequencer
    stepSeq = audioLib.StepSequencer(dev.sampleRate, 250 /* step length, in ms */, [0.3, 0.25, 0.75, 0.01] /* array of step values */, 0.5 /* attack time, 0-1 */);

    // Add the automation
    osc.addAutomation('frequency' /* parameter name */, lfo /* automation source */, 0.2 /* amount */, 'additiveModulation' /* automation operation, additiveModulation is origVal + origVal * automVal * amount */);
    flt.addAutomation('cutoff', stepSeq, 1, 'modulation');
    
}, true);

Back to Tutorials