Command Palette

Search for a command to run...

SFX/Sound Engine

Sound Engine

Web Audio API engine for playing sound assets

Overview

A lightweight Web Audio API wrapper that handles audio decoding, caching, and playback. Installed automatically as a dependency when you add any sound.

Installation

npx shadcn@latest add @elements/sound-engine

Typically you don't need to install this directly - it's pulled in automatically by any sfx-* component.

API

playSound

Play a sound from a base64 data URI with optional volume and playback rate.

import { playSound } from "@/lib/sound-engine";
import { whooshSound } from "@/sfx/whoosh";

const { stop } = await playSound(whooshSound.dataUri, {
  volume: 0.5,
  playbackRate: 1.0,
  onEnd: () => console.log("done"),
});

Options

OptionTypeDefaultDescription
volumenumber1Volume level (0-1)
playbackRatenumber1Playback speed (0.5 = half, 2 = double)
onEnd() => void-Callback when playback finishes

Returns

PropertyTypeDescription
stop() => voidStop playback immediately

Details

  • Uses a singleton AudioContext (created on first play)
  • Caches decoded audio buffers by data URI
  • Handles base64 decoding internally
  • Works in all modern browsers

License

CC0 (public domain). No attribution required.