Load all sounds from the sound library instead of random URLs.

This commit is contained in:
flash 2024-02-09 23:28:02 +00:00
parent 149cdac8ed
commit fd41509c74
7 changed files with 42 additions and 100 deletions

View file

@ -16,8 +16,7 @@
<div class="noscript-icon"><i class="fas fa-exclamation-triangle fa-3x"></i></div>
<div class="noscript-header">Enable Javascript!</div>
<div class="noscript-body">
<p>{title} is a web based chat and requires Javascript to work.</p>
<p>If you use any other privacy tools that prevent loading content from other domains, you may also need to white list <code>static.flash.moe</code> as many resources are loaded from there.</p>
<p>{title} is a web based chat application and requires Javascript to work.</p>
</div>
</div>
</div>

View file

@ -30,8 +30,8 @@ const MamiContext = function(targetBody) {
let soundMgr = null;
let sndPckPlay = null;
const soundLib = new MamiSoundLibrary(),
soundPck = new MamiSoundPacks();
const soundLib = new MamiSoundLibrary();
const soundPck = new MamiSoundPacks();
pub.initSound = function() {
if(soundMgr !== null)
@ -48,35 +48,21 @@ const MamiContext = function(targetBody) {
pub.getSoundPacks = function() { return soundPck; };
pub.getSoundPackPlayer = function() { return sndPckPlay; };
const playSoundBuffer = async (buffer, volume, rate) => {
const source = buffer.createSource();
if(typeof volume === 'number')
source.setVolume(volume);
if(typeof rate === 'number')
source.setRate(rate);
await source.play();
};
pub.playUrlSound = async (sources, volume, rate) => {
if(soundMgr === null)
return;
const url = soundMgr.findSupportedUrl(sources);
if(url === null)
return;
const buffer = await soundMgr.load(`MamiCtx:${url}`, url);
await playSoundBuffer(buffer, volume, rate);
};
pub.playLibrarySound = async (name, volume, rate) => {
if(soundMgr === null)
return;
const soundInfo = soundLib.getSound(name);
const buffer = await soundMgr.load(soundInfo.getName(), soundInfo.getSources());
await playSoundBuffer(buffer, volume, rate);
const source = buffer.createSource();
if(typeof volume === 'number')
source.setVolume(volume);
if(typeof rate === 'number')
source.setRate(rate);
await source.play();
};
const txtTriggers = new MamiTextTriggers;

View file

@ -101,34 +101,24 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
return loading;
};
const playBannedSfx = function() {
if(!mami.hasSound())
return;
const urls = {
mp3: '//static.flash.moe/sounds/touhou-death.mp3',
ogg: '//static.flash.moe/sounds/touhou-death.ogg',
};
mami.getSound().load('banSFX', urls).then(buffer => buffer.createSource().play());
const playBannedSfx = async () => {
await mami.playLibrarySound('touhou:pichuun');
};
const playBannedBgm = function(preload) {
const playBannedBgm = async preload => {
if(!mami.hasSound())
return;
const urls = {
opus: '//static.flash.moe/sounds/players-score.opus',
caf: '//static.flash.moe/sounds/players-score.caf'
};
const soundMgr = mami.getSound();
const soundLib = mami.getSoundLibrary();
const soundInfo = soundLib.getSound('touhou:th10score');
mami.getSound().load('banBGM', urls).then(buffer => {
if(preload)
return;
const buffer = await soundMgr.load(soundInfo.getName(), soundInfo.getSources());
if(preload)
return;
const source = buffer.createSource();
source.setLoop(true, 10.512, 38.074);
source.play();
});
const source = buffer.createSource();
source.setLoop(true, 10.512, 38.074);
await source.play();
};
const onOpen = function(ev) {
@ -863,15 +853,14 @@ Umi.Protocol.SockChat.Protocol = function(views, settings) {
MamiAnimate({
duration: 550,
easing: 'outExpo',
start: function() {
start: () => {
playBannedBgm(true);
playBannedSfx();
},
update: function(t) {
const scale = 'scale(' + (1 - .5 * t).toString() + ', ' + (1 - 1 * t).toString() + ')';
currentView.style.transform = scale;
update: t => {
currentView.style.transform = `scale(${(1 - .5 * t)}, ${(1 - 1 * t)})`;
},
end: function() {
end: () => {
getLoadingOverlay(icon, header, message).then(() => {
playBannedBgm();

View file

@ -1,47 +1,30 @@
#include rng.js
const OsuKeys = (() => {
const urlBase = '//static.flash.moe/sounds/';
const rng = new MamiRNG;
let sndRng = false;
const playSound = async name => {
if(!mami.hasSound())
return;
const soundMgr = mami.getSound();
const urls = {
ogg: urlBase + name + '.ogg',
mp3: urlBase + name + '.mp3',
};
const buffer = await soundMgr.load('OsuKeys:' + name, urls);
const source = buffer.createSource();
if(sndRng)
source.setRate(1.8 - (rng.sample() * 1.5));
await source.play();
};
const keyHandler = ev => {
if(ev.key === 'Control' || ev.key === 'Alt' || ev.key === 'Shift')
return;
let soundName;
if(ev.key === 'Enter' || ev.key === 'NumpadEnter')
soundName = 'key-confirm';
soundName = 'confirm';
else if(ev.key === 'Backspace' || ev.key === 'Delete')
soundName = 'key-delete';
soundName = 'delete';
else if(ev.key === 'NumLock' || ev.key === 'CapsLock' || ev.key === 'ScrollLock')
soundName = 'key-caps';
soundName = 'caps';
else if(ev.key === 'ArrowUp' || ev.key === 'ArrowDown' || ev.key === 'ArrowLeft' || ev.key === 'ArrowRight')
soundName = 'key-movement';
soundName = 'movement';
else
soundName = `key-press-${rng.next(1, 5)}`;
soundName = `press${rng.next(1, 5)}`;
playSound(soundName);
mami.playLibrarySound(
`osu:key:${soundName}`,
undefined,
sndRng ? (1.8 - (rng.sample() * 1.5)) : undefined
);
};
return {

View file

@ -1,17 +1,10 @@
#include rng.js
const Seinfeld = (function() {
const urlBase = '//static.flash.moe/sounds/seinfeld/', sounds = 22, rng = new MamiRNG;
const sounds = 22;
const rng = new MamiRNG;
return {
getRandom: function() {
const name = (rng.next(sounds) + 1).toString(),
url = urlBase + name;
return {
opus: url + '.opus',
caf: url + '.caf',
};
},
getRandom: () => `seinfeld:riff${rng.next(1, sounds + 1)}`,
};
})();

View file

@ -47,14 +47,6 @@ const MamiSoundManager = function(context) {
pub.findSupportedUrl = findSupportedUrl;
pub.loadAnonymous = async urls => {
const url = findSupportedUrl(urls);
if(url === null)
throw 'No supported audio format could be determined.';
return await context.createBuffer(url);
};
pub.load = async (name, urls) => {
if(loaded.has(name))
return loaded.get(name);

View file

@ -17,7 +17,7 @@ Umi.Sound = (() => {
return;
if(settings.get('seinfeld')) {
mami.playUrlSound(Seinfeld.getRandom());
mami.playLibrarySound(Seinfeld.getRandom());
break;
}