Home

Awesome

Dilla

npm npm GitHub stars GitHub forks

Travis branch Code Climate Code Climate David dependencies David devDependencies

Schedule looped playback of Web Audio notes at 96 ticks per beat

Based on ditty and bopper. Named after one of the greatest to ever touch a drum machine.

Install

$ npm install --save dilla

Usage

var Dilla = require('dilla');
var audioContext = new AudioContext();
var dilla = new Dilla(audioContext, options);

Options and defaults

{
  "tempo": 120,
  "beatsPerBar": 4,
  "loopLength": 2
}

Note that loopLength is measured in bars, i.e. the default loop length above is 8 beats.

Example

The "hello world" of audio libraries, the simple metronome: check out the demo or code.

var high = {
  'position': '*.1.01',
  'freq': 440,
  'duration': 15
};
var low = { 'freq': 330, 'duration': 15 };

dilla.set('metronome', [
  high,
  ['*.>1.01', low]
]);

var oscillator, gainNode;
dilla.on('step', function (step) {
  if (step.event === 'start') {
    oscillator = step.context.createOscillator();
    gainNode = step.context.createGain();
    oscillator.connect(gainNode);
    gainNode.connect(step.context.destination);
    oscillator.frequency.value = step.args.freq;
    gainNode.gain.setValueAtTime(1, step.time);
    oscillator.start(step.time);
  }
  else if (step.event === 'stop' && oscillator) {
    gainNode.gain.setValueAtTime(1, step.time);
    gainNode.gain.linearRampToValueAtTime(0, step.time + 0.1);
    oscillator.stop(step.time + 0.1);
    oscillator = null;
    gainNode = null;
  }
});

dilla.start();

Tutorials

API

Playback controls

Scheduling

Position and options

Objects

Position

Note

Events

tick

Fires when the bar, beat or tick value of dilla.position() is updated.

dilla.on('tick', function (tick) {
  console.log(tick.position) // "1.1.01"
});

step

Fires when a scheduled note should start or stop. For notes with undefined or falsy duration value (i.e. oneshots), no stop step event is triggered.

dilla.on('step', function (step) {
  console.log(step.event); // "start" or "stop"
  console.log(step.time); // offset in seconds
  console.log(step.args); // note data originally passed to set()
});

Develop

Changelog

License

MIT © Adam Renklint