| @@ -1,22 +1,73 @@ | |||||
| let playing = []; | |||||
| looping = {}; | |||||
| let audioDict = {}; | let audioDict = {}; | ||||
| // play some sound | // play some sound | ||||
| function playAudio(name) { | |||||
| function playSfx(name) { | |||||
| if (audioDict[name] == undefined) { | if (audioDict[name] == undefined) { | ||||
| console.log(name + " is not loaded yet, dingus"); | console.log(name + " is not loaded yet, dingus"); | ||||
| return; | return; | ||||
| } | } | ||||
| let src = audioContext.createBufferSource(); | let src = audioContext.createBufferSource(); | ||||
| src.buffer = audioDict[name]; | src.buffer = audioDict[name]; | ||||
| src.connect(audioContext.destination); | |||||
| playing.push(src); | |||||
| src.name = name; | |||||
| src.onended = (event) => src.done = true; | |||||
| src.start(0); | |||||
| } | |||||
| function playLoop(name) { | |||||
| if (audioDict[name] == undefined) { | |||||
| console.log(name + " is not loaded yet, dingus"); | |||||
| return; | |||||
| } | |||||
| // if already playing, just keep going | |||||
| if (looping[name] && !looping[name].done) { | |||||
| console.log(name + " is already looping"); | |||||
| return; | |||||
| } | |||||
| let src = audioContext.createBufferSource(); | |||||
| src.buffer = audioDict[name]; | |||||
| src.connect(audioContext.destination); | src.connect(audioContext.destination); | ||||
| looping[name] = src; | |||||
| src.name = name; | |||||
| src.onended = (event) => src.done = true; | |||||
| src.loop = true; | |||||
| src.start(0); | src.start(0); | ||||
| } | } | ||||
| function stopSfx(name) { | |||||
| playing.map(item => { | |||||
| if (item.name == name) | |||||
| item.stop(); | |||||
| } ); | |||||
| cleanPlaying(); | |||||
| } | |||||
| function stopLoop(name) { | |||||
| if (looping[name]) { | |||||
| looping[name].stop(); | |||||
| delete looping[name]; | |||||
| } | |||||
| } | |||||
| function cleanPlaying() { | |||||
| playing = playing.filter(item => !item.done); | |||||
| } | |||||
| // asynchronously load an audio file | // asynchronously load an audio file | ||||
| function loadAudio(name, flush=false) { | function loadAudio(name, flush=false) { | ||||