UI Patterns
Feedback

Sounds

Short interface sounds — clicks, ticks, toggles, success/error, swooshes — synthesized with the Web Audio API, opt-in and persisted.

Native apps lean on sound as feedback: the keyboard click, the payment "ding", the swoosh of a sent message. On the web it's rare, mostly because it's easy to get wrong. Done carefully — quiet, short, opt-in — it makes actions feel confirmed in a way visuals alone don't, especially on touch screens without haptics.

Examples

Sound pad

Every sound on this page is generated at runtime with the Web Audio API — no audio files, nothing to download. Sound is off by default; turn it on first.

Interface sounds

Sounds are off by default — turn them on above.

In context

The same sounds attached to real interactions: ticking a checkbox, flipping a switch, saving, dismissing a row. Note how little sound it takes — each cue is under 300 ms.

Interface sounds

Save alternates between success and failure.

Synthesis

Two building blocks cover most UI sounds:

  • Filtered noise for percussive sounds. A few milliseconds of white noise through a band-pass filter is a click; through a high-pass it's a tick; with the filter frequency sweeping upwards over ~300 ms it's a swoosh.
  • Oscillators for pitched sounds. A short sine with a pitch glide up is "on", the same glide down is "off". Two notes a fifth apart, ascending, read as success; a low triangle wave descending reads as error.

Every sound goes through an envelope: ramp the gain up from silence in 1–5 ms, then decay exponentially. Starting or stopping a sound at full volume produces its own audible pop.

Install with the shadcn CLI, or copy the source below.

npx shadcn@latest add https://ui-patterns.stereobooster.com/r/feedback-sound.json
lib/feedback-sound.ts
export type FeedbackSound =
  | "click"
  | "tick"
  | "toggle-on"
  | "toggle-off"
  | "success"
  | "error"
  | "swoosh";

let context: AudioContext | null = null;
let noise: AudioBuffer | null = null;

function getContext() {
  if (typeof AudioContext === "undefined") return null;
  context ??= new AudioContext();
  // Autoplay policy: a context created or resumed outside a user gesture
  // stays suspended, so the first play() must come from a click/keypress.
  if (context.state === "suspended") void context.resume();
  return context;
}

function getNoise(ctx: AudioContext) {
  if (!noise) {
    noise = ctx.createBuffer(1, ctx.sampleRate, ctx.sampleRate);
    const data = noise.getChannelData(0);
    for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1;
  }
  return noise;
}

function envelope(
  ctx: AudioContext,
  start: number,
  peak: number,
  attack: number,
  decay: number,
) {
  const gain = ctx.createGain();
  // Ramping from silence avoids the audible pop of a hard start/stop.
  gain.gain.setValueAtTime(0.0001, start);
  gain.gain.linearRampToValueAtTime(peak, start + attack);
  gain.gain.exponentialRampToValueAtTime(0.0001, start + attack + decay);
  return gain;
}

function noiseBurst(
  ctx: AudioContext,
  out: AudioNode,
  {
    start,
    duration,
    filter,
    frequency,
    to,
    q = 1,
    peak,
    attack = 0.001,
  }: {
    start: number;
    duration: number;
    filter: BiquadFilterType;
    frequency: number;
    to?: number;
    q?: number;
    peak: number;
    attack?: number;
  },
) {
  const source = ctx.createBufferSource();
  source.buffer = getNoise(ctx);
  const biquad = ctx.createBiquadFilter();
  biquad.type = filter;
  biquad.Q.value = q;
  biquad.frequency.setValueAtTime(frequency, start);
  if (to) biquad.frequency.exponentialRampToValueAtTime(to, start + duration);
  const gain = envelope(ctx, start, peak, attack, duration - attack);
  source.connect(biquad).connect(gain).connect(out);
  source.start(start, Math.random() * 0.5, duration);
}

function tone(
  ctx: AudioContext,
  out: AudioNode,
  {
    start,
    duration,
    type = "sine",
    frequency,
    to,
    peak,
    attack = 0.004,
  }: {
    start: number;
    duration: number;
    type?: OscillatorType;
    frequency: number;
    to?: number;
    peak: number;
    attack?: number;
  },
) {
  const osc = ctx.createOscillator();
  osc.type = type;
  osc.frequency.setValueAtTime(frequency, start);
  if (to) osc.frequency.exponentialRampToValueAtTime(to, start + duration);
  const gain = envelope(ctx, start, peak, attack, duration - attack);
  osc.connect(gain).connect(out);
  osc.start(start);
  osc.stop(start + duration + 0.01);
}

export function playFeedbackSound(sound: FeedbackSound, volume = 0.5) {
  const ctx = getContext();
  if (!ctx) return;
  const out = ctx.createGain();
  out.gain.value = volume;
  out.connect(ctx.destination);
  const t = ctx.currentTime;

  switch (sound) {
    case "click":
      noiseBurst(ctx, out, {
        start: t,
        duration: 0.025,
        filter: "bandpass",
        frequency: 3200,
        q: 1.5,
        peak: 0.9,
      });
      break;
    case "tick":
      noiseBurst(ctx, out, {
        start: t,
        duration: 0.012,
        filter: "highpass",
        frequency: 5000,
        peak: 0.5,
      });
      break;
    case "toggle-on":
      tone(ctx, out, {
        start: t,
        duration: 0.08,
        frequency: 600,
        to: 950,
        peak: 0.35,
      });
      break;
    case "toggle-off":
      tone(ctx, out, {
        start: t,
        duration: 0.08,
        frequency: 950,
        to: 600,
        peak: 0.3,
      });
      break;
    case "success":
      tone(ctx, out, {
        start: t,
        duration: 0.12,
        frequency: 1046.5,
        peak: 0.25,
      });
      tone(ctx, out, {
        start: t + 0.09,
        duration: 0.22,
        frequency: 1568,
        peak: 0.25,
      });
      break;
    case "error":
      tone(ctx, out, {
        start: t,
        duration: 0.09,
        type: "triangle",
        frequency: 220,
        to: 190,
        peak: 0.45,
      });
      tone(ctx, out, {
        start: t + 0.12,
        duration: 0.14,
        type: "triangle",
        frequency: 200,
        to: 160,
        peak: 0.45,
      });
      break;
    case "swoosh":
      noiseBurst(ctx, out, {
        start: t,
        duration: 0.28,
        filter: "bandpass",
        frequency: 400,
        to: 3500,
        q: 2,
        peak: 1.5,
        attack: 0.09,
      });
      break;
  }
}

Preference

Install with the shadcn CLI (it also installs lib/feedback-sound.ts), or copy the source below.

npx shadcn@latest add https://ui-patterns.stereobooster.com/r/use-feedback-sound.json
hooks/use-feedback-sound.ts
"use client";

import * as React from "react";

import { playFeedbackSound, type FeedbackSound } from "@/lib/feedback-sound";

const STORAGE_KEY = "feedback-sound-enabled";
const listeners = new Set<() => void>();

function readEnabled() {
  try {
    return localStorage.getItem(STORAGE_KEY) === "1";
  } catch {
    return false;
  }
}

let enabled: boolean | null = null;

function getEnabled() {
  enabled ??= readEnabled();
  return enabled;
}

function setEnabled(value: boolean) {
  enabled = value;
  try {
    localStorage.setItem(STORAGE_KEY, value ? "1" : "0");
  } catch {}
  listeners.forEach((listener) => listener());
}

function subscribe(listener: () => void) {
  listeners.add(listener);
  const onStorage = (event: StorageEvent) => {
    if (event.key !== STORAGE_KEY) return;
    enabled = readEnabled();
    listener();
  };
  window.addEventListener("storage", onStorage);
  return () => {
    listeners.delete(listener);
    window.removeEventListener("storage", onStorage);
  };
}

/**
 * Sound is off until the user turns it on; the choice is persisted in
 * `localStorage` and synced across tabs. `play` is a no-op while disabled
 * or while the page is hidden.
 */
export function useFeedbackSound(volume = 0.5) {
  const isEnabled = React.useSyncExternalStore(
    subscribe,
    getEnabled,
    () => false,
  );

  const play = React.useCallback(
    (sound: FeedbackSound) => {
      if (!getEnabled() || document.visibilityState !== "visible") return;
      playFeedbackSound(sound, volume);
    },
    [volume],
  );

  return { enabled: isEnabled, setEnabled, play };
}

Usage:

const { enabled, setEnabled, play } = useFeedbackSound();

<button onClick={() => { play("click"); save(); }}>Save</button>

Rules

  • Off by default, one switch to turn it on. Unexpected sound is worse than unexpected motion — people are in meetings, on trains, listening to music. There's no prefers-reduced-sound media query, so the site has to own the setting. Persist it (localStorage here, user settings in a real app) and sync it across tabs.
  • Autoplay policy. Browsers keep an AudioContext suspended until the user interacts with the page. Create or resume it inside a click/keypress handler — the first play() call here always comes from one. Sounds triggered by timers or network events before any interaction are silently dropped.
  • Short and quiet. Under ~300 ms, well below media volume. The UI should still be fully usable with sound off; sound confirms, it never carries information on its own.
  • No sounds on hover, scroll or typing in dense interfaces — anything that fires dozens of times a minute becomes noise. Reserve sound for deliberate actions and outcomes.
  • Match the sound to the meaning, consistently: the same "success" for every save, the same "error" for every failure. A semantic set (click, toggle, success, error, dismiss) beats a different sound per component.
  • Don't play in background tabs. The hook skips sounds while document.visibilityState is hidden.
  • Synthesized vs recorded. Synthesis costs zero bytes and is easy to tweak (pitch, length, volume); recorded samples sound richer and more "physical". If you go with files, preload them and decode once with decodeAudioData — new Audio(src).play() per click has noticeable latency on mobile.

Libraries

Recorded sound sets:

  • soundcn — 700+ CC0 sounds installed into your codebase with the shadcn CLI, plus a React hook.
  • UI SFX — a semantic system: 78 named cues in 13 categories, MIT code with CC0 audio.
  • audx — browse, preview and install sound assets with one command.
  • snd-lib — includes swipe / page-transition sounds.
  • Peal — CLI plus a thin Howler.js wrapper.

Synthesized (no audio files):

Playback and components:

  • use-sound — Josh Comeau's React hook over Howler.js for your own files.
  • sensory-ui — sound layer for shadcn/ui components.

Further reading

On this page