defineSound, and it plays through the Web Audio API in any browser.
Set up
Runinit from your project root. It works with any package manager and any bundler that understands ES modules:
init asks where sound files should go and suggests src/sounds (or lib/sounds, or sounds). Sounds are written as .ts files, or .js if your project doesn’t use TypeScript. If your tsconfig.json or jsconfig.json has a path alias that covers that directory, the CLI tells you the import path to use, for example @/sounds.
Play a sound
Call the sound inside an event handler. Wrap the call in an arrow function, so the event object isn’t passed in as play options.Add a mute setting
Without the React provider, use the global controls from@litlab/audx. They apply to every sound you call:
sound-settings.ts
useSound in React skips playback when the user prefers reduced motion. Outside React, add that check yourself:
Server rendering
Nuxt, SvelteKit, Astro and other SSR frameworks import your components on the server, where Web Audio doesn’t exist. That’s fine: calling a sound on the server does nothing, so you don’t need guards or dynamic imports.Browser autoplay rules
Browsers keep audio suspended until the user interacts with the page. audx resumes it automatically on the first sound you play, so sounds triggered by a click, key press or tap always work. A sound played on page load, before any interaction, stays silent. To make sure audio is running before something time-sensitive, awaitensureReady() inside a user gesture:
Next steps
Core API
Everything exported from
@litlab/audx.Themes
Change every sound in your app with one command.