Use URLs as unique keys instead of arbitrary names.

This commit is contained in:
Pachira 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 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;
};

View file

@ -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')

View file

@ -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');
});

View file

@ -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;

View file

@ -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(),
};
};

View file

@ -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;
};

View file

@ -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();
};

View file

@ -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);