oramics/dsp-kit

View on GitHub
packages/elastica/example/lib/player.js

Summary

Maintainability
A
0 mins
Test Coverage
var ac = require('audio-context')

module.exports = function player (buffer, context) {
  var player = null
  return function (e, el) {
    if (player) {
      console.log('stop')
      player.stop()
      player = null
      if (el) el.innerText = 'Play'
    } else {
      console.log('playing...')
      if (el) el.innerText = 'Stop'
      player = play(buffer, true, context)
    }
  }
}

function play (buffer, loop, context = ac) {
  var source = context.createBufferSource()
  source.buffer = buffer
  source.connect(context.destination)
  if (loop === true) source.loop = true
  source.start()
  return source
}