Streaming clock source for scheduling Web Audio events rhythmically
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
README.md
example.js
index.js
package.json

README.md

bopper

A streaming clock source for scheduling Web Audio events rhythmically.

Use with ditty if you want to create loop sequences.

Install

$ npm install bopper

Require into your browser bundle with browserify.

Example

var audioContext = new AudioContext()
var bopper = require('bopper')(audioContext)

// save a reference on the window to avoid garbage collection
window.scheduler = bopper

var playback = [
  {position: 0, length: 0.1},
  {position: 1, length: 0.1},
  {position: 2, length: 0.1},
  {position: 3, length: 0.1},
  {position: 3.5, length: 0.1},
  {position: 4, length: 0.1},
  {position: 5, length: 0.1},
  {position: 6, length: 0.1},
  {position: 7, length: 0.1},
  {position: 7+1/3, length: 0.1},
  {position: 7+2/3, length: 0.1}
]

// emits data roughly every 20ms

bopper.on('data', function(schedule){
  // schedule: from, to, time, beatDuration

  playback.forEach(function(note){
    if (note.position >= schedule.from && note.position < schedule.to){
      var delta = note.position - schedule.from
      var time = schedule.time + delta
      var duration = note.length * schedule.beatDuration
      play(time, duration)
    }
  })

})

function play(at, duration){
  var oscillator = audioContext.createOscillator()
  oscillator.connect(audioContext.destination)
  oscillator.start(at)
  oscillator.stop(at+duration)
}

bopper.setTempo(120)
bopper.start()

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

License

MIT