ditty

Schedule Web Audio events for a midi loop sequence using bopper clock source.

npm install ditty
8 downloads in the last month

ditty

Schedule Web Audio events for a midi loop sequence using bopper clock source.

Install

$ npm install ditty

Example

var Ditty = require('ditty')
var Bopper = require('bopper')

var audioContext = new webkitAudioContext()
var bopper = Bopper(audioContext)

var ditty = Ditty(bopper)

ditty.on('data', function(event){
  // event: key, data, action, time, position

  if (event.data[2]){
    noteOn(event.time, event.data[1], event.data[2])
  } else {
    noteOff(event.time, event.data[1])
  }

})

var C = 60, F = 65, G = 67, A = 69

ditty.setPlayback([
  [144, C, 100, 0.0, 0.9],
  [144, C, 100, 1.0, 0.9],
  [144, F, 100, 2.0, 0.9],
  [144, F, 100, 3.0, 0.9],
  [144, G, 100, 4.0, 0.9],
  [144, G, 100, 5.0, 0.4],
  [144, F, 100, 5.5, 0.9],
  [144, F, 100, 6.5, 0.4],
  [144, F, 100, 7.0, 0.4],
  [144, F, 100, 7.5, 0.4]
], 8)

// mixer
var output = audioContext.createGain()
output.gain.value = 0.5
output.connect(audioContext.destination)

// simple oscillating synth
var onNotes = {}
function noteOn(time, id, velocity){
  console.log('on', time, id)
  noteOff(time, id) // choke existing note if any
  var oscillator = audioContext.createOscillator()
  oscillator.connect(output)
  oscillator.frequency.value = getFrequency(id)
  oscillator.type = 2
  oscillator.start(time)
  onNotes[id] = oscillator
}
function noteOff(time, id){
  if (onNotes[id]){
    console.log('off', time, id)
    onNotes[id].stop(time)
    onNotes[id] = null
  }
}
function getFrequency(id){
  return 440 * Math.pow(2, (id - 69.0) / 12.0)
}

bopper.setTempo(120)
bopper.start()

To run the example npm install && npm install -g beefy then beefy example.js and navigate to http://localhost:9966/

npm loves you