aiaudiojs: Audio System
aiaudiojs is a thin shell around Howler.js that adds the lifecycle constraints of the ai*js family: idempotent dispose(), AbortSignal support on every path, and a first-class crossfade(). Howler itself is a peerDependency, so it stays upgradable.
Install
Section titled “Install”npm install aiaudiojs howlerQuick Start
Section titled “Quick Start”import { createAudio } from "aiaudiojs";
const audio = createAudio({ autoUnlock: true, // Bind the first user gesture to unlock the AudioContext (on by default) resumeOnVisibility: true, // Best-effort resume when iOS Safari returns from the background});
// 1. Load sounds (returns Promise<Sound>)const zap = await audio.load("zap.mp3");const bgm1 = await audio.load("level1.mp3");const bgm2 = await audio.load("level2.mp3");
// 2. Playzap.play({ volume: 0.8 });bgm1.play({ loop: true });
// 3. Crossfade smoothly on scene change: bgm2 starts and loops, bgm1 stops when it completesawait audio.crossfade(bgm1, bgm2, { duration: 2, loop: true });
// 4. For advanced features, get Howler's native Howl object directlyconst howl = zap.nativeHowl;
// 5. Clean upaudio.disposeAll(); // Dispose every sound created by this audio instanceMain API
Section titled “Main API”| API | Description |
|---|---|
audio.load(url, signal?) |
Loads a sound and returns Promise<Sound> |
sound.play(opts?) |
Plays and returns the Howler sound id. opts accepts loop, volume, rate and signal; a rate that is not a finite number throws AudioError |
sound.pause(id?) |
Pauses |
sound.stop(id?) |
Stops and returns to the start |
sound.resume(id?) |
Resumes a paused voice |
sound.fade(from, to, ms, id?) |
Fades the volume and returns Promise<void>. from / to must be finite numbers within [0, 1] and ms a finite number >= 0; otherwise it rejects with AudioError and does not throw synchronously |
audio.crossfade(from, to, opts) |
Crossfades between two sounds. opts is { duration, curve?, signal?, loop? }, and duration is in seconds |
sound.dispose() |
Disposes this sound (idempotent) |
audio.disposeAll() |
Disposes all sounds; in-flight load() calls reject immediately with AudioDisposedError |
sound.nativeHowl |
Returns the underlying Howl object (escape hatch) |
Crossfade Modes
Section titled “Crossfade Modes”// Default: linear curve, implemented through Howl.fade()await audio.crossfade(bgm1, bgm2, { duration: 2 });
// Equal-power curve: sin/cos curve, more even perceived loudness// Scheduled on the AudioContext timeline, supports cancelling midway with AbortSignalawait audio.crossfade(bgm1, bgm2, { duration: 2, curve: "equal-power", signal: ctrl.signal,});crossfade()starts only one new voice ofto, andfromshould already be playing. On normal completion, both curves stop only the outgoing voice IDs captured whencrossfade()started. Iffrommust keep playing afterwards, callfrom.play()after the promise resolves.loop: truemakes the newly started voice loop;crossfade(s, s)replays the same sound with a crossfade.- Aborting midway does not stop any voice. Afterwards the caller handles both
fromandto(stop them or fade again). - If
optsis missing ordurationis not a finite number> 0, the returned promise rejects withAudioError. An argument that is disposed or is not aSound, or a failed equal-power precondition, throws synchronously. Wrapping the call intry { await audio.crossfade(...) } catchcovers both cases. Error messages carry theaiaudiojs:prefix, so match on the error class plus a regex, not on the full string.
Deliberately Excluded Features
Section titled “Deliberately Excluded Features”aiaudiojs does not include a synth, MIDI, audio worklet, 3D spatial audio (use Howler’s spatial plugin) or audio sprite generation (use the audiosprite CLI). Staying lightweight is a design principle, not an omission.
Common Pairings
Section titled “Common Pairings”- With
aifsmjs: trigger a crossfade on FSM scene changes; FSM entry/exit triggers sound play/stop - With
aieventjs: game events (player death, graze) trigger sound playback - Standalone: background music management for interactive pages and showcase pages