| @@ -1,6 +1,8 @@ | |||||
| let playing = []; | let playing = []; | ||||
| let looping = {}; | let looping = {}; | ||||
| let waiting = {}; | |||||
| let audioContext; | let audioContext; | ||||
| let gainControl; | let gainControl; | ||||
| @@ -15,6 +17,16 @@ function setVolume(vol) { | |||||
| // play some sound | // play some sound | ||||
| function playSfx(name) { | 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) { | if (audioDict[name] == undefined) { | ||||
| console.error(name + " is not loaded yet, dingus"); | console.error(name + " is not loaded yet, dingus"); | ||||
| return; | return; | ||||
| @@ -33,6 +45,16 @@ function playSfx(name) { | |||||
| } | } | ||||
| function playLoop(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) { | if (audioDict[name] == undefined) { | ||||
| console.error(name + " is not loaded yet, dingus"); | console.error(name + " is not loaded yet, dingus"); | ||||
| return; | return; | ||||
| @@ -98,12 +120,20 @@ function cleanPlaying() { | |||||
| function loadAudio(name, flush=false) { | function loadAudio(name, flush=false) { | ||||
| // are we already trying to get the audio? | |||||
| if (waiting[name]) { | |||||
| return; | |||||
| } | |||||
| // do we already have the audio? | // do we already have the audio? | ||||
| if (audioDict[name] && !flush) { | if (audioDict[name] && !flush) { | ||||
| return; | return; | ||||
| } | } | ||||
| waiting[name] = []; | |||||
| // is the audio already stored locally? | // is the audio already stored locally? | ||||
| if (!flush) { | if (!flush) { | ||||
| @@ -124,11 +154,25 @@ function cacheAndParse(name, data) { | |||||
| } | } | ||||
| function parseAudioData(name, data) { | function parseAudioData(name, data) { | ||||
| console.log(data); | |||||
| audioContext.decodeAudioData(data, function(buffer) { | audioContext.decodeAudioData(data, function(buffer) { | ||||
| audioDict[name] = 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) => { | xhr.onload = (res) => { | ||||
| if (xhr.status == 200) | if (xhr.status == 200) | ||||
| cacheAndParse(name, xhr.response); | cacheAndParse(name, xhr.response); | ||||
| else | |||||
| else { | |||||
| console.log("Couldn't load " + name); | 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(); | xhr.send(); | ||||
| } | } | ||||