From 3bc595c3ee8313604443d1630747dfb980c22508 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 10 Feb 2024 00:13:47 +0000 Subject: [PATCH] Use URLs as unique keys instead of arbitrary names. --- src/mami.js/audio/context.js | 8 +++---- src/mami.js/context.js | 3 +-- src/mami.js/main.js | 9 +++----- src/mami.js/sockchat_old.js | 3 +-- src/mami.js/sound/sndlibrary.js | 13 ++++++----- src/mami.js/sound/sndmgr.js | 39 +++++++++++---------------------- src/mami.js/sound/sndpacks.js | 2 +- src/mami.js/youare.jsx | 4 ++-- 8 files changed, 33 insertions(+), 48 deletions(-) diff --git a/src/mami.js/audio/context.js b/src/mami.js/audio/context.js index f5552d7..10a6439 100644 --- a/src/mami.js/audio/context.js +++ b/src/mami.js/audio/context.js @@ -5,8 +5,8 @@ const MamiAudioContext = function() { const pub = {}; - let volume = null, - isMuted = false; + let volume = null; + let isMuted = false; const ctxArgs = { latencyHint: 'playback' }; let ctx = null; @@ -30,6 +30,7 @@ const MamiAudioContext = function() { }; init(); + pub.getContext = () => ctx; pub.resetContext = init; @@ -50,7 +51,7 @@ const MamiAudioContext = function() { return new MamiAudioBuffer(pub, buffer); }; - const createSource = buffer => { + pub.createSource = buffer => { const gain = ctx.createGain(); gain.connect(mainGain); @@ -60,7 +61,6 @@ const MamiAudioContext = function() { return new MamiAudioSource(source, gain); }; - pub.createSource = createSource; return pub; }; diff --git a/src/mami.js/context.js b/src/mami.js/context.js index 53ace67..4078c33 100644 --- a/src/mami.js/context.js +++ b/src/mami.js/context.js @@ -52,8 +52,7 @@ const MamiContext = function(targetBody) { if(soundMgr === null) return; - const soundInfo = soundLib.getSound(name); - const buffer = await soundMgr.load(soundInfo.getName(), soundInfo.getSources()); + const buffer = await soundMgr.load(soundLib.getSoundSources(name)); const source = buffer.createSource(); if(typeof volume === 'number') diff --git a/src/mami.js/main.js b/src/mami.js/main.js index 87ae717..6377f72 100644 --- a/src/mami.js/main.js +++ b/src/mami.js/main.js @@ -247,13 +247,10 @@ const Umi = { UI: {} }; settings.watch('preventOverflow', v => document.body.classList.toggle('prevent-overflow', v)); settings.watch('tmpDisableOldThemeSys', (v, n, i) => { if(!i) Umi.UI.View.AccentReload(); }); - const mcPortalSnd = 'minecraft:nether:enter'; - if(settings.get('minecraft') !== 'no' && ctx.hasSound() && sndLib.hasSound(mcPortalSnd)) - ctx.getSound().load(mcPortalSnd, sndLib.getSound(mcPortalSnd).getSources()) - .then(buffer => buffer.createSource().play()); - settings.watch('minecraft', (v, n, i) => { - if(!i) + if(i) + mami.playLibrarySound('minecraft:nether:enter'); + else Umi.Sound.Play('join'); }); diff --git a/src/mami.js/sockchat_old.js b/src/mami.js/sockchat_old.js index 9b16f70..9325570 100644 --- a/src/mami.js/sockchat_old.js +++ b/src/mami.js/sockchat_old.js @@ -110,9 +110,8 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) { const soundMgr = mami.getSound(); const soundLib = mami.getSoundLibrary(); - const soundInfo = soundLib.getSound('touhou:th10score'); - const buffer = await soundMgr.load(soundInfo.getName(), soundInfo.getSources()); + const buffer = await soundMgr.load(soundLib.getSoundSources('touhou:th10score')); if(preload) return; diff --git a/src/mami.js/sound/sndlibrary.js b/src/mami.js/sound/sndlibrary.js index 3f80391..be77382 100644 --- a/src/mami.js/sound/sndlibrary.js +++ b/src/mami.js/sound/sndlibrary.js @@ -45,6 +45,12 @@ const MamiSoundLibrary = function() { addSound(new MamiSoundInfo(soundInfo.name, readOnly, soundInfo.title, sources)); }; + const getSound = name => { + if(!sounds.has(name)) + throw 'No sound with this name has been registered.'; + return sounds.get(name); + }; + return { addSound: addSound, loadSound: loadSound, @@ -64,10 +70,7 @@ const MamiSoundLibrary = function() { sounds.forEach(body); }, hasSound: name => sounds.has(name), - getSound: name => { - if(!sounds.has(name)) - throw 'No sound with this name has been registered.'; - return sounds.get(name); - } + getSound: getSound, + getSoundSources: name => getSound(name).getSources(), }; }; diff --git a/src/mami.js/sound/sndmgr.js b/src/mami.js/sound/sndmgr.js index 66d2cdc..3871109 100644 --- a/src/mami.js/sound/sndmgr.js +++ b/src/mami.js/sound/sndmgr.js @@ -30,7 +30,10 @@ const MamiSoundManager = function(context) { const pub = {}; - const findSupportedUrl = urls => { + const extractSupportedUrl = urls => { + if(typeof urls === 'object' && typeof urls.getSources === 'function') + urls = urls.getSources(); + if(typeof urls === 'string') return urls; @@ -42,44 +45,28 @@ const MamiSoundManager = function(context) { if(type in urls && typeof urls[type] === 'string') return urls[type]; - return null; + throw 'No supported audio format could be determined.'; }; - pub.findSupportedUrl = findSupportedUrl; + pub.load = async urls => { + const url = extractSupportedUrl(urls); - pub.load = async (name, urls) => { - if(loaded.has(name)) - return loaded.get(name); - - const url = findSupportedUrl(urls); - if(url === null) - throw 'No supported audio format could be determined.'; + if(loaded.has(url)) + return loaded.get(url); const buffer = await context.createBuffer(url); - loaded.set(name, buffer); + loaded.set(url, buffer); return buffer; }; - pub.unload = name => { - loaded.delete(name); + pub.unload = urls => { + loaded.delete(extractSupportedUrl(urls)); }; + pub.reset = () => { loaded.clear(); }; - pub.play = name => { - if(loaded.has(name)) - loaded.get(name).createSource().play(); - }; - - pub.isLoaded = name => loaded.has(name); - - pub.get = name => { - if(!loaded.has(name)) - return null; - return loaded.get(name).createSource(); - }; - return pub; }; diff --git a/src/mami.js/sound/sndpacks.js b/src/mami.js/sound/sndpacks.js index 893502d..ed70d3d 100644 --- a/src/mami.js/sound/sndpacks.js +++ b/src/mami.js/sound/sndpacks.js @@ -80,7 +80,7 @@ const MamiSoundPackPlayer = function(soundMgr, sndLibrary) { return; const info = sndLibrary.getSound(pack.getEventSound(eventName)); - const buffer = await soundMgr.load(info.getName(), info.getSources()); + const buffer = await soundMgr.load(info.getSources()); await buffer.createSource().play(); }; diff --git a/src/mami.js/youare.jsx b/src/mami.js/youare.jsx index 57bc0ca..fac565e 100644 --- a/src/mami.js/youare.jsx +++ b/src/mami.js/youare.jsx @@ -25,8 +25,8 @@ const MamiYouAreAnIdiot = function() { return; try { - const sources = mami.getSoundLibrary().getSound('misc:youare').getSources(); - const buffer = await soundMgr.load('youarebgm', sources); + const sources = mami.getSoundLibrary().getSoundSources('misc:youare'); + const buffer = await soundMgr.load(sources); soundSrc = buffer.createSource(); soundSrc.setMuted(true);