campfire/src/lib/sound.js

27 lines
686 B
JavaScript
Raw Normal View History

import sound_log from '../sound/log.ogg';
import sound_hello from '../sound/hello.ogg';
import sound_success from '../sound/success.ogg';
2024-06-29 10:46:27 +01:00
let sounds;
if (typeof Audio !== typeof undefined) {
sounds = {
"default": new Audio(sound_log),
"post": new Audio(sound_hello),
"boost": new Audio(sound_success),
2024-06-29 10:46:27 +01:00
};
}
export function play_sound(name) {
2024-06-28 08:43:12 +01:00
if (name === false) return;
if (!name) name = "default";
const sound = sounds[name];
if (!sound) {
console.warn(`Attempted to play sound "${name}", which does not exist!`);
return;
}
sound.pause();
sound.volume = 0.25;
sound.currentTime = 0;
sound.play();
}