Skip to main content
Every function here is safe to import during server rendering. Playback does nothing when Web Audio isn’t available or audx is muted.

Sounds

defineSound

Binds a sound definition into a play function. Each call creates a new voice routed through the master bus and returns a VoiceHandle.
Generated sound modules are built with defineSound.

PlayOptions

Every play function accepts these overrides.
number
Volume multiplier, 0 to 1.
number
Stereo pan override, -1 (left) to 1 (right).
number
Detune in cents, added to the source’s detune.
number
Playback rate multiplier.
number
Velocity, 0 to 1. Scales gain and can dim filter cutoffs.
{ detune?: number; volume?: number; playbackRate?: number }
Random variation applied on each play, as a maximum ±value per field. For example { detune: 50, volume: 0.1 } varies pitch by up to ±50 cents and volume by up to ±10%.

Shorthands

One-line sounds. gain defaults to 0.4. noise defaults to "white" and a decay of 0.05.

defineSequence

Schedules several sounds on a timeline. Calling the result starts it and returns a stop function.
Each step takes a sound (a play function or a raw definition), and either at (absolute time in seconds) or wait (seconds after the previous step). A step can set its own volume. With loop: true, the sequence repeats every duration seconds (default 1).

Global controls

ensureReady accepts { latencyHint?, sampleRate? }. Pass them on the first call to configure the context. Browsers only allow audio after a user gesture. audx resumes the context automatically on the first play, so sounds triggered from click or key handlers work without setup.

Patches

A patch is a theme loaded at runtime: a named set of sound definitions you play by name.
loadPatch fetches JSON from a URL and throws if the request fails.
AudioPatch also exposes ready, name, author, version, description and tags.

Offline rendering

Render a sound in memory with OfflineAudioContext. OfflineRenderOptions takes duration in seconds (required), sampleRate (default 44100) and numberOfChannels (default 2). WAV output is 16-bit PCM.

Analysis

createMasterAnalyser is connected to the master bus, so it sees everything audx plays. createAnalyser is unconnected; connect a source to analyser.node yourself. AnalyserOptions: fftSize (default 2048), smoothingTimeConstant (default 0.8), minDecibels, maxDecibels. An AudioAnalyser reuses its arrays between reads, so it’s safe to call in an animation loop:

3D audio

Set or read the listener position (positionX, positionY, positionZ) and orientation (forwardX/Y/Z, defaults 0, 0, -1; upX/Y/Z, defaults 0, 1, 0). Use with the panner field on a layer.

Types

All types used above are exported, including Sound, SoundDefinition, Layer, MultiLayerSound, Source, Envelope, Filter, Effect, LFO, PlayOptions, VoiceHandle, SequenceStep, SoundPatch, AudioPatch and AudioAnalyser.