| @@ -1,6 +1,8 @@ | |||
| let playing = []; | |||
| let looping = {}; | |||
| let waiting = {}; | |||
| let audioContext; | |||
| let gainControl; | |||
| @@ -15,6 +17,16 @@ function setVolume(vol) { | |||
| // play some sound | |||
| function playSfx(name) { | |||
| if (waiting[name]) { | |||
| waiting[name].push({ | |||
| type: "sfx", | |||
| name: name | |||
| }); | |||
| console.error(name + " isn't ready yet"); | |||
| return; | |||
| } | |||
| if (audioDict[name] == undefined) { | |||
| console.error(name + " is not loaded yet, dingus"); | |||
| return; | |||
| @@ -33,6 +45,16 @@ function playSfx(name) { | |||
| } | |||
| function playLoop(name) { | |||
| if (waiting[name]) { | |||
| waiting[name].push({ | |||
| type: "loop", | |||
| name: name | |||
| }); | |||
| console.error(name + " isn't ready yet"); | |||
| return; | |||
| } | |||
| if (audioDict[name] == undefined) { | |||
| console.error(name + " is not loaded yet, dingus"); | |||
| return; | |||
| @@ -98,12 +120,20 @@ function cleanPlaying() { | |||
| function loadAudio(name, flush=false) { | |||
| // are we already trying to get the audio? | |||
| if (waiting[name]) { | |||
| return; | |||
| } | |||
| // do we already have the audio? | |||
| if (audioDict[name] && !flush) { | |||
| return; | |||
| } | |||
| waiting[name] = []; | |||
| // is the audio already stored locally? | |||
| if (!flush) { | |||
| @@ -124,11 +154,25 @@ function cacheAndParse(name, data) { | |||
| } | |||
| function parseAudioData(name, data) { | |||
| console.log(data); | |||
| audioContext.decodeAudioData(data, function(buffer) { | |||
| audioDict[name] = buffer; | |||
| }, function(e){ console.log("Error with decoding audio data" + e.err);}); | |||
| waiting[name].forEach(queued => { | |||
| if (queued.type == "sfx") { | |||
| playSfx(name); | |||
| } | |||
| if (queued.type == "loop") { | |||
| playLoop(name); | |||
| } | |||
| delete waiting[name]; | |||
| }) | |||
| }, function(e){ | |||
| console.log("Error with decoding audio data" + e.err); | |||
| delete waiting[name]; | |||
| }); | |||
| } | |||
| @@ -140,10 +184,15 @@ function loadRemoteAudio(name) { | |||
| xhr.onload = (res) => { | |||
| if (xhr.status == 200) | |||
| cacheAndParse(name, xhr.response); | |||
| else | |||
| else { | |||
| console.log("Couldn't load " + name); | |||
| delete waiting[name]; | |||
| } | |||
| } | |||
| xhr.onerror = (xhr) => { | |||
| console.log("Couldn't load " + name); | |||
| } | |||
| xhr.onerror = (xhr) => console.log("Couldn't load " + name); | |||
| xhr.send(); | |||
| } | |||