@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 touseSound and call the function it returns:
useSound skips playback when:
- the nearest
<SoundProvider>hasenabled={false} - the user has
prefers-reduced-motion: reduceset - audx is muted globally with
setMuted(true) - Web Audio isn’t available, for example during server rendering
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: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:
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: