Code Monkey home page Code Monkey logo

beat-emitter's Introduction

beat-emitter

WebAudio scheduler with an event emitter

Features

  • runs as a web worker (so that setInterval fires more reliably)
  • schedules audio events with a slight look-ahead, for precise timing

Usage

const beatEmitter = require('beat-emitter')

const ac = new AudioContext()
const beats = beatEmitter(ac)

beats.setBpm(110)

// optional: shuffle factor (best results between .02 and .09)
beats.setShuffle(.07)

beats.start()
beats.schedule((beat, time) => {
  // play a note every 2nd beat
  if (beat % 2 === 0) {
    const osc = ac.createOscillator()
    osc.frequency.value = 440
    osc.start(time)
    osc.stop(time + 0.1)
    osc.connect(ac.destination)
  }
})

Acknowledgements

I learned a lot from reading https://github.com/cwilso/metronome and based the code on this.

beat-emitter's People

Contributors

joshwnj avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

beat-emitter's Issues

worker function has a problem

If user call start function twice in a row, the user won't be able to stop it anymore.

Below codes can fix it. Which one is your preference?

function worker () {
  let timerId = null

  self.onmessage = function (e) {
    if (timerId === null && e.data === 'start') {
      timerId = setInterval(postMessage.bind(null, 'tick'), 25)
    } else if (e.data === 'stop') {
      clearInterval(timerId)
      timerId = null
    }
  }
}

or

function worker () {
  let timerId

  self.onmessage = function (e) {
    clearInterval(timerId)
    timerId = null
    if (e.data === 'start') {
      timerId = setInterval(postMessage.bind(null, 'tick'), 25)
    }
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.