jussi-kalliokoski / audiolib.js

audiolib.js is a powerful audio tools library for javascript.
http://audiolibjs.org/
672 stars 58 forks source link

Implement Note Sequencer #15

Closed davidgovea closed 13 years ago

davidgovea commented 13 years ago

The ability to easily sequence notes would be excellent. I'm working on a Tracker class in my fork, but there might be a better way. Different from the existing StepSequencer (basically just a gate).

Could tie in with the new generator.append(noteBuffer) style.

Should support:

jussi-kalliokoski commented 13 years ago

Actually, although not designed for that, StepSequencer can do it and can even add interesting dimensions to it, try this out:


var dev, osc, seq;

function audioProcess(buffer, ch){
    var i, n;
    for (i=0; i<buffer.length; i+=ch){
        seq.generate();
        osc.frequency = seq.value;
        osc.generate();

        for (n=0; n<ch; n++){
            buffer[i + n] = osc.sawtooth();
        }
    }
}

window.onload = function onload(){
    dev = audioLib.AudioDevice(audioProcess, 2);
    osc = new audioLib.Oscillator(dev.sampleRate);
    seq = new audioLib.StepSequencer(dev.sampleRate, 500, [440, 660, 880, 660], 0.4);
}

There we have a glide.

That said... I'm not exactly sure yet as to how to structure the automation things for this project, and there should definitely be a dedicated note sequencer, with features you mentioned. There's also a MIDI library in the works (on the thought level, implementing such a thing isn't much hassle), so it would be nice if these two were compatible. (Synthesize MIDI, or frequencies, read midi data to virtual instruments, etc). But meanwhile, if you have the time, any code would help as in to see what's good and what's bad, and might last the critical thinking after all. :)

davidgovea commented 13 years ago

Awesome, thanks for the tip.

Got a test case up for my tracker: 49ce6521c1b3391b949e

Tracker.addNote takes a generation function that can be any mix of generators/effects. The test case is a bit unnecessarily verbose, just to illustrate. I'll post back when I get some things ironed out, namely the pops between notes caused by jumps in the pcm buffers.

jussi-kalliokoski commented 13 years ago

Nice! Take a look at the https://github.com/jussi-kalliokoski/audiolib.js/blob/master/js/controls/midieventsequencer.js , it's a bit old now and not that much used, but... It only deals with a JSON representation of MIDI data. Maybe these two can be combined in some nice way, that way we'd be ready for the MIDI library.

davidgovea commented 13 years ago

So when it comes to note callbacks.. I'm a little stuck.

Something like the web audio API currentTime could be helpful, but what about on the Mozilla side? samplesWritten?

Trying to get the callbacks to fire within +-30 ms or so of the actual playback without wrecking performance.. Got any input?

jussi-kalliokoski commented 13 years ago

I added something useful for this in sink.js e6697ef031d98f214ad591f1215539ae5116904f, sink.getPlayBackTime() returns the amount of samples fed to the speakers, if not malfunctioning.