diff --git a/src/mami.js/audio/buffer.js b/src/mami.js/audio/buffer.js deleted file mode 100644 index 8f5868a..0000000 --- a/src/mami.js/audio/buffer.js +++ /dev/null @@ -1,5 +0,0 @@ -const MamiAudioBuffer = function(ctx, buffer) { - return { - createSource: () => ctx.createSource(buffer), - }; -}; diff --git a/src/mami.js/audio/context.js b/src/mami.js/audio/context.js index 6d5de3a..e44211c 100644 --- a/src/mami.js/audio/context.js +++ b/src/mami.js/audio/context.js @@ -1,5 +1,4 @@ #include utility.js -#include audio/buffer.js #include audio/source.js const MamiAudioContext = function() { @@ -63,15 +62,14 @@ const MamiAudioContext = function() { pub.createBuffer = async url => { if(ctx === undefined) - return new MamiAudioBuffer(pub); + return undefined; const result = await $x.get(url, { type: 'arraybuffer' }); - const buffer = await ctx.decodeAudioData(result.body()); - return new MamiAudioBuffer(pub, buffer); + return await ctx.decodeAudioData(result.body()); }; pub.createSource = buffer => { - if(ctx === undefined) + if(ctx === undefined || buffer === undefined) return new MamiAudioSourceDummy; const gain = ctx.createGain(); diff --git a/src/mami.js/context.js b/src/mami.js/context.js index 010d109..1c2af6f 100644 --- a/src/mami.js/context.js +++ b/src/mami.js/context.js @@ -31,8 +31,7 @@ const MamiContext = function(targetBody) { pub.getSoundPackPlayer = () => sndPckPlay; pub.playLibrarySound = async (name, volume, rate) => { - const buffer = await soundMgr.load(soundLib.getSoundSources(name)); - const source = buffer.createSource(); + const source = await soundMgr.loadSource(soundLib.getSoundSources(name)); if(typeof volume === 'number') source.setVolume(volume); diff --git a/src/mami.js/sockchat_old.js b/src/mami.js/sockchat_old.js index d798478..70a7370 100644 --- a/src/mami.js/sockchat_old.js +++ b/src/mami.js/sockchat_old.js @@ -106,13 +106,14 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { }; const playBannedBgm = async preload => { const soundMgr = mami.getSound(); - const soundLib = mami.getSoundLibrary(); + const sources = mami.getSoundLibrary().getSoundSources('touhou:th10score'); - const buffer = await soundMgr.load(soundLib.getSoundSources('touhou:th10score')); - if(preload) + if(preload) { + await soundMgr.loadBuffer(sources); return; + } - const source = buffer.createSource(); + const source = await soundMgr.loadSource(sources); source.setLoop(true, 10.512, 38.074); await source.play(); }; diff --git a/src/mami.js/sound/sndmgr.js b/src/mami.js/sound/sndmgr.js index 15973cb..810e903 100644 --- a/src/mami.js/sound/sndmgr.js +++ b/src/mami.js/sound/sndmgr.js @@ -45,18 +45,21 @@ const MamiSoundManager = function(context) { const loaded = new Map; context.onReset(() => loaded.clear()); + const loadBuffer = async urls => { + const url = extractUrl(urls); + + if(loaded.has(url)) + return loaded.get(url); + + const buffer = await context.createBuffer(url); + loaded.set(url, buffer); + + return buffer; + }; + return { - load: async urls => { - const url = extractUrl(urls); - - if(loaded.has(url)) - return loaded.get(url); - - const buffer = await context.createBuffer(url); - loaded.set(url, buffer); - - return buffer; - }, + loadBuffer: loadBuffer, + loadSource: async urls => context.createSource(await loadBuffer(urls)), unload: urls => { loaded.delete(extractUrl(urls)); }, diff --git a/src/mami.js/sound/sndpacks.js b/src/mami.js/sound/sndpacks.js index ed70d3d..d12c36f 100644 --- a/src/mami.js/sound/sndpacks.js +++ b/src/mami.js/sound/sndpacks.js @@ -80,9 +80,9 @@ const MamiSoundPackPlayer = function(soundMgr, sndLibrary) { return; const info = sndLibrary.getSound(pack.getEventSound(eventName)); - const buffer = await soundMgr.load(info.getSources()); + const source = await soundMgr.loadSource(info.getSources()); - await buffer.createSource().play(); + await source.play(); }; return pub; diff --git a/src/mami.js/youare.jsx b/src/mami.js/youare.jsx index fac565e..eb77948 100644 --- a/src/mami.js/youare.jsx +++ b/src/mami.js/youare.jsx @@ -26,9 +26,8 @@ const MamiYouAreAnIdiot = function() { try { const sources = mami.getSoundLibrary().getSoundSources('misc:youare'); - const buffer = await soundMgr.load(sources); - soundSrc = buffer.createSource(); + soundSrc = soundMgr.loadSource(sources); soundSrc.setMuted(true); soundSrc.setLoop(true, 0.21, 5); soundSrc.play();