Skip to content

Instantly share code, notes, and snippets.

@simonlc
Created December 10, 2016 07:01
Show Gist options
  • Save simonlc/c03a85ce3fd980341d44c43482b721d4 to your computer and use it in GitHub Desktop.
Save simonlc/c03a85ce3fd980341d44c43482b721d4 to your computer and use it in GitHub Desktop.
A really quick example on how to use sound buffers.
'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