Playback and lifecycle
Own definitions, voices, activation, interruption, and cleanup.
AudioBits API · 0.7.0 · Available on npm.
A Sound is reusable; every play() creates a
new Voice with fresh sources. Keep the voice for live updates and Stop.
Install thruster using the catalog source guide
under src/sounds/recipes/, then use this host code in src/:
import { createAudio } from "audiobits";
import { thruster } from "./sounds/recipes/thruster/recipe";
const audio = createAudio();
const sound = audio.sound(thruster);
let request = 0;
let current: ReturnType<typeof sound.play> | undefined;
export async function startFromGesture() {
const token = ++request;
await audio.start();
if (token !== request) return;
current?.stop();
const voice = sound.play({ parameters: { throttle: 0.2 }, seed: 42 });
current = voice;
return voice;
}
export function stop() {
request++;
current?.stop();
current = undefined;
}
export async function teardown() {
stop();
await audio.dispose();
}Bind stop() to Stop and cancellation, and call it before suspension on hide.
The request counter invalidates pending startFromGesture() work on Stop or
teardown. start() must be called directly
in a user gesture. Construction is silent; playback before successful activation
fails without queuing. Blocked resume times out after two wall-clock seconds,
with a retryable start-failed error. Concurrent starts share a promise.
Held touch and earlier activation
A trusted event does not always grant audio activation. For example, WebKit's
activation-triggering events
include completed touch taps, while a touch press alone may leave audio suspended.
Calling start() synchronously is necessary, but does not override that policy.
If a held interaction cannot activate audio itself, an existing earlier accepted
gesture, such as the link tap that opens an experience, can prepare its engine. Preparation calls
audio.start() without playing a sound. Retain and pass that same engine to the
stage; creating another engine or caching an "unlocked" boolean loses the native
context's activation. Transfer ownership explicitly and dispose abandoned
preparations. This application pattern can be shared across input methods;
verify browser eligibility in the intended flow.
The host still owns playback intent. Await activation before playing, and
invalidate a pending hold on release, cancellation, hide or teardown. A later
activation must never replay released input. Keep normal touch defaults intact;
use scoped touch-action and selection/callout CSS on the stage.
A cold direct visit has no earlier gesture. Navigation preparation cannot make that first held touch eligible on a browser that blocks it. Surface activation failure and allow a fresh gesture to retry; test this case separately. Silent buffer sources and delayed calls do not establish permission to play.
Stop and disposal
voice.stop() cancels before onset or releases from the current envelope.
voice.stop({ at }) schedules release on the audio clock. The voice stays
active and accepts live updates until that deadline, then reports stopping
without a JavaScript transition timer. An earlier stop overrides the schedule.
voice.ended resolves after owned sources and tails are cleaned up, never merely
because release was scheduled. States are active, stopping, retiring,
and ended; repeated stops do not extend lifetime. sound.dispose() finalizes
that sound's voices immediately. audio.stopAll() releases managed voices and
follows their recipe release; { tails: "cut" } uses a 5 ms voice fade.
Sound/bus disposal and forced cuts override pending releases.
suspend() invalidates pending starts and finalizes owned voices before
suspending. Native interruption or closure also clears managed resources.
dispose() is terminal and idempotent: it invalidates activation, disconnects
owned nodes, and closes the context even while suspended. Caller-created sources
need their own cleanup.
Controllers installed from the catalog own their sounds and voices; they use your engine without taking over its lifecycle. Await activation before starting them, supply bounded application inputs, then stop and dispose the controller on removal. A shared engine remains the application owner's responsibility.
Host policy
Stop with cut tails and suspend on page hide. Dispose when the owning host is removed, unless ownership is explicitly transferred to another host. Never automatically restart playback on return. Unsubscribe listeners and remove host events on teardown. Choose the engine's ownership boundary to match the application's lifecycle.
Audio state is idle, starting, running, suspended, interrupted, closed,
or disposed. subscribe(listener) returns an unsubscribe function.