bopper

    2.11.0 • Public • Published

    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/

    Install

    npm i bopper

    DownloadsWeekly Downloads

    7

    Version

    2.11.0

    License

    MIT

    Last publish

    Collaborators

    • mmckegg