|
|
|
@@ -1,22 +1,73 @@ |
|
|
|
let playing = []; |
|
|
|
looping = {}; |
|
|
|
|
|
|
|
let audioDict = {}; |
|
|
|
|
|
|
|
// play some sound |
|
|
|
|
|
|
|
function playAudio(name) { |
|
|
|
function playSfx(name) { |
|
|
|
if (audioDict[name] == undefined) { |
|
|
|
console.log(name + " is not loaded yet, dingus"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
let src = audioContext.createBufferSource(); |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
looping[name] = src; |
|
|
|
|
|
|
|
src.name = name; |
|
|
|
src.onended = (event) => src.done = true; |
|
|
|
|
|
|
|
src.loop = true; |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
function loadAudio(name, flush=false) { |
|
|
|
|