Bash.tv → GitHub → itch.io

Audio Manager

Music and sound effects on separate volume channels, with mute, fades, music crossfades and ducking. It waits for the first tap or key press, as browsers require, before making any sound.

Files: audio-manager.js (the component, no dependencies) and audio-manager.test.mjs (6 tests). Copy audio-manager.js into your game, for example to src/components/, and import it as an ES module.

Usage

import { createAudio } from "./audio-manager.js";
const audio = createAudio();
audio.autoUnlock();                                   // starts sound on the first tap or key press
await audio.load({ jump: "assets/sfx/jump.ogg", theme: "assets/music/theme.ogg" });
audio.play("jump", { volume: 0.8, rate: 0.95 + Math.random() * 0.1 });
audio.playMusic("theme", { fade: 1 });                // waits for unlock if needed
audio.setVolume("music", 0.5);

API

audio.context, audio.missing (names asked for but not loaded)

Engines

Engine Notes
Vanilla yes.
Phaser yes, instead of Phaser’s sound system (turn it off with audio: { noAudio: true }) or alongside it with its own volume.
PixiJS yes (Pixi has no built-in audio).
Three.js yes. Pass audio.context to THREE.AudioContext.setContext() to share one context for positional audio.

Tests

Node tests with a fake AudioContext: locked until unlock, music waits for unlock, bus volumes and mute, crossfade, ducking, missing sounds, failed loads.

Run them with node --test toolkit/components/audio-manager/ (Node 20 or newer, nothing to install).