Skip to content

Instantly share code, notes, and snippets.

@sgentle
Created April 26, 2015 10:08
Show Gist options
  • Save sgentle/58e3e37c28a7662d014f to your computer and use it in GitHub Desktop.
Save sgentle/58e3e37c28a7662d014f to your computer and use it in GitHub Desktop.
Audio loops in CoffeeScript with fetch and Promises
ctx = new (AudioContext || webkitAudioContext)
addAudio = (src) ->
fetch src
.then (response) -> response.arrayBuffer()
.then (audioData) -> new Promise (accept) -> ctx.decodeAudioData audioData, accept
.then (buffer) ->
node = ctx.createBufferSource()
node.buffer = buffer
node.loop = true
node.connect ctx.destination
node.start(0)
node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment