# API reference

URL: https://www.audiobits.dev/docs/api

Public package exports and resource limits.

AudioBits API · 0.7.0 · Available on npm.

Imports use public package exports. `audiobits/schema.json` supplies structural
recipe JSON Schema; `audiobits/capabilities.json` supplies this package version's
shipped primitives and browser gate. Semantic limits still require
`validateRecipe`; sample-rate constraints are checked at playback.

| Export                  | Responsibility                                                         |
| ----------------------- | ---------------------------------------------------------------------- |
| `createAudio(options?)` | Own an engine; defaults: 32 voices, eight per sound, -12 dB master     |
| `defineSound(input)`    | Author typed recipe data; validate and freeze; throws `AudioBitsError` |
| `validateRecipe(input)` | Return a recipe or bounded path-specific issues                        |
| `recipeSchema`          | Published recipe schema version 1                                      |
| `AudioBitsError`        | Error with `code` and validation `issues`                              |

Creative recipes live in the [copy-owned catalog](https://www.audiobits.dev/docs/recipes#catalog-source-you-own).
Import them from local application files after copying or installing their source.

Public types include `Recipe`, its layer/source/envelope/filter/value and
parameter types, `ValidationResult`, `RecipeIssue`, `AudioEngine`, `AudioOptions`,
`AudioState`, `Sound`, `Voice`, `PlayOptions`, `StopOptions`, `RecipeControls`, `LiveControls`,
`Effect`, `Delay`, and `Bus`.

## Playback options

`sound.play({ at, gainDb, pan, parameters, seed, bus })` uses absolute context
time for `at`; omit it for immediate play. Past times are rejected. Gain is
-60–0 dB, pan is -1–1, and seed is an unsigned 32-bit integer. Future voices
reserve capacity. Invalid options fail before stealing or graph allocation.

Engine and per-sound limits are integers from 1–128. Oldest-voice stealing uses a
5 ms output fade with at most one extra retiring voice. `audio.counts` separates
active/stopping reservations from retirees. Stopping voices retain capacity until
cleanup; further steals finalize the previous retiree before creating another.
`masterGainDb` accepts -60–0 dB; playback gain and pan default to 0.
At most 32 buses, including master, may be live.

## Voice release

`voice.stop()` begins the existing release immediately. `voice.stop({ at })`
schedules its beginning at an absolute AudioContext time in seconds. Explicit
`at` values must be finite numbers from 0 through `Number.MAX_SAFE_INTEGER`;
invalid values throw `invalid-option` before changing the voice. Valid past
values release immediately. This does not change `play({ at })` validation.

The earliest release wins: a later or equal request does nothing, an earlier
request replaces a pending release, and plain `stop()` releases now. Once release
has begun, repeated stops are idempotent. A scheduled stop cannot extend a
one-shot's natural gate or lifetime. A stop at or before a reserved onset cancels
playback and cleans up immediately. Release envelopes and effect tails retain
their normal behavior.

## Recipe effects

`Effect` is `Filter | Delay`. Both are recipe data supported at layer and recipe
scope, with at most eight effects total. [Delay](https://www.audiobits.dev/docs/recipes#delay) uses
`{ type: "delay", time, feedback?, mix? }`: time is 0.01–2 seconds, feedback is
0–0.8 (default 0), and mix is 0–1 (default 0.5). Each accepts numeric values,
seeded variation and play-only mappings. No effect-instance API is needed.

Delay tails count toward the existing voice limit until `voice.ended`. A wet
delay allocates four nodes, or six with feedback, plus one shared native clock
node per voice with delay. Each effect requests mono delay storage totaling its resolved
time, never more than two seconds. Feedback uses a loop delay and a one-quantum
feedforward delay to compensate native circulation latency. `mix: 0` allocates no delay nodes. Serial
path tails are capped at 30 seconds after release plus a 50 ms final fade.

## Errors

`AudioBitsError` exposes `code` and validation `issues`. Invalid recipes use
`invalid-recipe`; invalid controls use `invalid-control` and invalid seeds use
`invalid-seed`. Playback before activation uses `not-ready`; failed activation
uses retryable `start-failed`. See [Lifecycle](https://www.audiobits.dev/docs/lifecycle) for gesture retry
and cleanup. Control updates on a stopped or completed voice use `ended-voice`.

## Guides

[Recipes](https://www.audiobits.dev/docs/recipes), [Lifecycle](https://www.audiobits.dev/docs/lifecycle),
[Parameters](https://www.audiobits.dev/docs/parameters), [Buses](https://www.audiobits.dev/docs/buses), and
[Native interop](https://www.audiobits.dev/docs/native) explain the contracts and cleanup paths.

Defaults provide headroom for the curated sounds; arbitrary recipes, resonance,
or concurrency require application-specific signal checks.

Buses provide gain, mute, and routing. Shared effects are deferred until
multiple real sound requirements justify an API.
