Skip to main content
The React bindings live in @litlab/audx/react. They are marked "use client", so they work in the Next.js App Router without extra setup.

Play a sound from a component

Pass a sound to useSound and call the function it returns:
The returned callback never changes between renders, so it’s safe to pass to memoized children or use in effect dependencies. useSound skips playback when:
  • the nearest <SoundProvider> has enabled={false}
  • the user has prefers-reduced-motion: reduce set
  • audx is muted globally with setMuted(true)
  • Web Audio isn’t available, for example during server rendering
You can pass default play options as the second argument:
Use jitter for sounds that repeat quickly, like type or tick. A small random change on each play keeps repeats from sounding mechanical.

Add a sound setting

Wrap your app in <SoundProvider> and drive enabled and volume from your own state. Every useSound, useSequence and usePatch below it follows those values.
app/providers.tsx
components/sound-toggle.tsx
volume multiplies the volume of every sound under the provider, from 0 to 1.

Calling sounds directly

Sounds are plain functions, so you can call them outside React too, for example in an event handler, a store action or a toast helper:
Calling a sound directly bypasses <SoundProvider> and the reduced-motion check. Use useSound inside components, or setMuted to silence direct calls.
Use setMuted and setMasterVolume from @litlab/audx to control every sound, including direct calls:

Sequences

useSequence schedules several sounds on a timeline and returns stable play and stop callbacks:
Each step uses at for an absolute time in seconds, or wait for an offset from the previous step. Calling play again stops the previous run first.

Visualize output

useAnalyser connects an analyser to the master bus and disposes it on unmount. Read from it in an animation loop:
See the React API reference for every hook and prop.