Created
December 10, 2016 07:01
-
-
Save simonlc/c03a85ce3fd980341d44c43482b721d4 to your computer and use it in GitHub Desktop.
A really quick example on how to use sound buffers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// TODO Group audio files so we can control volume seperately | |
const context = new AudioContext(); | |
const soundDatas = {}; | |
const soundFiles = { | |
clear: '/audio/clear.wav', | |
fall: '/audio/fall.wav', | |
land: '/audio/land.wav', | |
lock: '/audio/lock.wav', | |
irs: '/audio/irs.wav', | |
}; | |
for (let key in soundFiles) { | |
const request = new Request(soundFiles[key]); | |
fetch(request).then(body => { | |
return body.arrayBuffer(); | |
}).then(buffer => { | |
context.decodeAudioData(buffer, data => { | |
soundDatas[key] = data; | |
}); | |
}); | |
} | |
function play(key) { | |
// TODO Check if loaded. | |
const source = context.createBufferSource(); | |
source.buffer = soundDatas[key]; | |
source.connect(context.destination); | |
source.start(0); | |
} | |
// e.g., | |
// play('fall'); | |
// TODO Promise.all for onload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment