Skip to content

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.

終端機視窗
npm install aiaudiojs howler
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. Play
zap.play({ volume: 0.8 });
bgm1.play({ loop: true });
// 3. Crossfade smoothly on scene change: bgm2 starts and loops, bgm1 stops when it completes
await audio.crossfade(bgm1, bgm2, { duration: 2, loop: true });
// 4. For advanced features, get Howler's native Howl object directly
const howl = zap.nativeHowl;
// 5. Clean up
audio.disposeAll(); // Dispose every sound created by this audio instance
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)
// 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 AbortSignal
await audio.crossfade(bgm1, bgm2, {
duration: 2,
curve: "equal-power",
signal: ctrl.signal,
});
  • crossfade() starts only one new voice of to, and from should already be playing. On normal completion, both curves stop only the outgoing voice IDs captured when crossfade() started. If from must keep playing afterwards, call from.play() after the promise resolves.
  • loop: true makes 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 from and to (stop them or fade again).
  • If opts is missing or duration is not a finite number > 0, the returned promise rejects with AudioError. An argument that is disposed or is not a Sound, or a failed equal-power precondition, throws synchronously. Wrapping the call in try { await audio.crossfade(...) } catch covers both cases. Error messages carry the aiaudiojs: prefix, so match on the error class plus a regex, not on the full string.

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.

  • 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