mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
127 lines
4.7 KiB
JavaScript
127 lines
4.7 KiB
JavaScript
// Plan2Code Web Console - the sound cues: which plays, one at a time, held until the first click; defines only.
|
|
|
|
import { looks } from "./looks.js";
|
|
import { soundKey, SOUND_EVENTS, PICKABLE_SOUNDS, soundFileKey } from "./palette.js";
|
|
|
|
/* ------------------------------------------------------------- sounds
|
|
|
|
The cues are the events in SOUND_EVENTS: a skill started from the
|
|
dashboard, Planny snoring and waking, the session opening and ending, a
|
|
new batch of questions, a launched skill ready, a reply on Ask, and a new
|
|
tab joining the strip (a posted document, a spec's overview). Each plays
|
|
only while both the master switch and its own switch are on. A browser will
|
|
not let a page make noise before the person has touched it, so a cue that
|
|
fires too early is held and let out by the first click or keypress rather
|
|
than dropped.
|
|
|
|
Only ever one at a time, never two overlapping or back to back. Cues asked
|
|
for in the same task collapse to the highest-ranked one; a cue asked for
|
|
while another is still playing is dropped unless it outranks it, in which
|
|
case it cuts the quieter one off. The held cue is a single slot on the same
|
|
rule, so the first click lets out one sound, not a queue of them.
|
|
|
|
Made on first use, not at load: audio downloads racing the page's own
|
|
scripts at start-up are bytes spent on sounds a browser will not play
|
|
before the first click anyway. One Audio per file, shared by the events
|
|
that use it. */
|
|
const SOUNDS = {};
|
|
|
|
let soundQueued = null;
|
|
|
|
let soundHeld = null;
|
|
|
|
let soundPlaying = null;
|
|
|
|
export const soundOn = (event) => looks.sound && looks[soundKey(event)];
|
|
|
|
// The file (path under the page's root, no extension) a cue plays: the pick
|
|
// for a pickable cue, the event's own file otherwise.
|
|
const soundPath = (event) =>
|
|
PICKABLE_SOUNDS.includes(event) && looks[soundFileKey(event)] ? `sounds/${looks[soundFileKey(event)]}` : SOUND_EVENTS[event].file;
|
|
|
|
const audioFor = (path) => (SOUNDS[path] ||= new Audio(`/${path}.mp3`));
|
|
|
|
// The Audio a cue last played, so a pick changed mid-sound still stops the right one.
|
|
const soundAudio = (event) => SOUNDS[playingPath[event] || soundPath(event)];
|
|
|
|
const playingPath = {};
|
|
|
|
const louderSound = (a, b) => (!b || SOUND_EVENTS[a].rank > SOUND_EVENTS[b].rank ? a : b);
|
|
|
|
export function sound(event) {
|
|
if (!soundOn(event)) return;
|
|
if (!soundQueued) queueMicrotask(flushSound);
|
|
soundQueued = louderSound(event, soundQueued);
|
|
}
|
|
|
|
function flushSound() {
|
|
const event = soundQueued;
|
|
soundQueued = null;
|
|
if (!soundOn(event)) return;
|
|
const busy = soundPlaying && !soundAudio(soundPlaying).paused;
|
|
if (busy && SOUND_EVENTS[event].rank <= SOUND_EVENTS[soundPlaying].rank) return;
|
|
if (busy) soundAudio(soundPlaying).pause();
|
|
const path = soundPath(event);
|
|
playingPath[event] = path;
|
|
const a = audioFor(path);
|
|
// Stops a preview, or any cue the rank check let through, before this one starts.
|
|
for (const other of Object.values(SOUNDS)) if (other !== a) other.pause();
|
|
a.currentTime = 0;
|
|
soundPlaying = event;
|
|
a.play().catch(() => {
|
|
if (soundPlaying === event) soundPlaying = null;
|
|
soundHeld = louderSound(event, soundHeld);
|
|
});
|
|
}
|
|
|
|
// Plays one picked file (sound-07) from the start for the picker's preview,
|
|
// cutting off whatever is playing. Dropped while a higher-ranked cue is
|
|
// playing, on the same busy check as flushSound. A preview is always a click
|
|
// or change, so the browser lets it play.
|
|
export function previewSound(file) {
|
|
const busy = soundPlaying && !soundAudio(soundPlaying).paused;
|
|
if (busy && SOUND_EVENTS[soundPlaying].rank > 0) return;
|
|
const a = audioFor(`sounds/${file}`);
|
|
for (const other of Object.values(SOUNDS)) if (other !== a) other.pause();
|
|
a.currentTime = 0;
|
|
soundPlaying = null;
|
|
a.play().catch(() => {});
|
|
}
|
|
|
|
// Fades a cue out over `ms` and stops it, so a long one never outlasts the
|
|
// moment it belongs to. Timers, not frames: a background tab still finishes.
|
|
export function fadeSound(event, ms) {
|
|
const a = soundAudio(event);
|
|
if (!a || a.paused) return;
|
|
const from = performance.now();
|
|
const timer = setInterval(() => {
|
|
const left = 1 - (performance.now() - from) / ms;
|
|
if (left > 0 && !a.paused) {
|
|
a.volume = left;
|
|
return;
|
|
}
|
|
clearInterval(timer);
|
|
a.pause();
|
|
a.volume = 1;
|
|
}, 25);
|
|
}
|
|
|
|
// Drops anything queued or held, for a Skip that wants silence.
|
|
export function dropSounds() {
|
|
soundQueued = null;
|
|
soundHeld = null;
|
|
}
|
|
|
|
// The first click or keypress lets out the one cue that was held.
|
|
export function releaseHeldSound() {
|
|
if (!soundHeld) return;
|
|
const name = soundHeld;
|
|
soundHeld = null;
|
|
sound(name);
|
|
}
|
|
|
|
// Drops the held cue: any of them, or only `event`'s.
|
|
export function dropHeldSound(event) {
|
|
if (event === undefined || soundHeld === event) soundHeld = null;
|
|
}
|