Use URLs as unique keys instead of arbitrary names.

This commit is contained in:
flash 2024-02-10 00:13:47 +00:00
parent fd41509c74
commit 3bc595c3ee
8 changed files with 33 additions and 48 deletions

View file

@ -5,8 +5,8 @@
const MamiAudioContext = function() { const MamiAudioContext = function() {
const pub = {}; const pub = {};
let volume = null, let volume = null;
isMuted = false; let isMuted = false;
const ctxArgs = { latencyHint: 'playback' }; const ctxArgs = { latencyHint: 'playback' };
let ctx = null; let ctx = null;
@ -30,6 +30,7 @@ const MamiAudioContext = function() {
}; };
init(); init();
pub.getContext = () => ctx; pub.getContext = () => ctx;
pub.resetContext = init; pub.resetContext = init;
@ -50,7 +51,7 @@ const MamiAudioContext = function() {
return new MamiAudioBuffer(pub, buffer); return new MamiAudioBuffer(pub, buffer);
}; };
const createSource = buffer => { pub.createSource = buffer => {
const gain = ctx.createGain(); const gain = ctx.createGain();
gain.connect(mainGain); gain.connect(mainGain);
@ -60,7 +61,6 @@ const MamiAudioContext = function() {
return new MamiAudioSource(source, gain); return new MamiAudioSource(source, gain);
}; };
pub.createSource = createSource;
return pub; return pub;
}; };

View file

@ -52,8 +52,7 @@ const MamiContext = function(targetBody) {
if(soundMgr === null) if(soundMgr === null)
return; return;
const soundInfo = soundLib.getSound(name); const buffer = await soundMgr.load(soundLib.getSoundSources(name));
const buffer = await soundMgr.load(soundInfo.getName(), soundInfo.getSources());
const source = buffer.createSource(); const source = buffer.createSource();
if(typeof volume === 'number') if(typeof volume === 'number')

View file

@ -247,13 +247,10 @@ const Umi = { UI: {} };
settings.watch('preventOverflow', v => document.body.classList.toggle('prevent-overflow', v)); settings.watch('preventOverflow', v => document.body.classList.toggle('prevent-overflow', v));
settings.watch('tmpDisableOldThemeSys', (v, n, i) => { if(!i) Umi.UI.View.AccentReload(); }); 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) => { settings.watch('minecraft', (v, n, i) => {
if(!i) if(i)
mami.playLibrarySound('minecraft:nether:enter');
else
Umi.Sound.Play('join'); Umi.Sound.Play('join');
}); });

View file

@ -110,9 +110,8 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
const soundMgr = mami.getSound(); const soundMgr = mami.getSound();
const soundLib = mami.getSoundLibrary(); 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) if(preload)
return; return;

View file

@ -45,6 +45,12 @@ const MamiSoundLibrary = function() {
addSound(new MamiSoundInfo(soundInfo.name, readOnly, soundInfo.title, sources)); 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 { return {
addSound: addSound, addSound: addSound,
loadSound: loadSound, loadSound: loadSound,
@ -64,10 +70,7 @@ const MamiSoundLibrary = function() {
sounds.forEach(body); sounds.forEach(body);
}, },
hasSound: name => sounds.has(name), hasSound: name => sounds.has(name),
getSound: name => { getSound: getSound,
if(!sounds.has(name)) getSoundSources: name => getSound(name).getSources(),
throw 'No sound with this name has been registered.';
return sounds.get(name);
}
}; };
}; };

View file

@ -30,7 +30,10 @@ const MamiSoundManager = function(context) {
const pub = {}; const pub = {};
const findSupportedUrl = urls => { const extractSupportedUrl = urls => {
if(typeof urls === 'object' && typeof urls.getSources === 'function')
urls = urls.getSources();
if(typeof urls === 'string') if(typeof urls === 'string')
return urls; return urls;
@ -42,44 +45,28 @@ const MamiSoundManager = function(context) {
if(type in urls && typeof urls[type] === 'string') if(type in urls && typeof urls[type] === 'string')
return urls[type]; 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(url))
if(loaded.has(name)) return loaded.get(url);
return loaded.get(name);
const url = findSupportedUrl(urls);
if(url === null)
throw 'No supported audio format could be determined.';
const buffer = await context.createBuffer(url); const buffer = await context.createBuffer(url);
loaded.set(name, buffer); loaded.set(url, buffer);
return buffer; return buffer;
}; };
pub.unload = name => { pub.unload = urls => {
loaded.delete(name); loaded.delete(extractSupportedUrl(urls));
}; };
pub.reset = () => { pub.reset = () => {
loaded.clear(); 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; return pub;
}; };

View file

@ -80,7 +80,7 @@ const MamiSoundPackPlayer = function(soundMgr, sndLibrary) {
return; return;
const info = sndLibrary.getSound(pack.getEventSound(eventName)); 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(); await buffer.createSource().play();
}; };

View file

@ -25,8 +25,8 @@ const MamiYouAreAnIdiot = function() {
return; return;
try { try {
const sources = mami.getSoundLibrary().getSound('misc:youare').getSources(); const sources = mami.getSoundLibrary().getSoundSources('misc:youare');
const buffer = await soundMgr.load('youarebgm', sources); const buffer = await soundMgr.load(sources);
soundSrc = buffer.createSource(); soundSrc = buffer.createSource();
soundSrc.setMuted(true); soundSrc.setMuted(true);