18 UI sounds. Zero audio files.
A complete UI sound palette synthesized at runtime with the Web Audio API. No assets, no decode latency, and every parameter is a number you can tune.
- Published
- 21 Sept 2026
- Length
- 4 min read
- Roles
- Design, Frontend
- From the work on
- Interactive Lab
This site has a small sound layer. Every hover and every click plays the next note of one short tune, so moving around the site plays it at your own pace. Behind them is a palette of eighteen interface sounds, and none of them exist as a file. Each one is a function that builds a tiny audio graph, plays it, and lets it be garbage collected.
This started as a constraint: I did not want to ship a folder of MP3s on a portfolio. It became the part of the site I enjoy most, because once sound is code, it gets designed like the rest of the interface.
Ticks
Clicks
Toggles
Confirmations
Errors
Notifications
Navigation
Why synthesize at all
An audio file has costs that are invisible until you count them. It has to be fetched, which means a network round trip or a preload hint. It has to be decoded, and decodeAudioData is asynchronous, so the first play is never guaranteed to land on the interaction that triggered it. Changing the tick means an editor, an export, a re-upload and a cache bust.
Synthesis removes all of that. The sound exists the moment the function runs. Latency is bounded by the audio hardware, not by I/O. Every parameter is a number in a config object, so tuning is a hot reload, not an export. The whole palette, hook included, is a few kilobytes of TypeScript, smaller than any one of the files it would have replaced.
The trade-off is realism. You will not get a convincing wooden knock out of an oscillator, and for interface feedback that is fine. Abstract, short, pitched sounds read as UI. Recorded ones read as foley.
The building blocks
Everything here is built from four Web Audio primitives.
OscillatorNode generates the pitched material. Sine for ticks and confirmations because it is soft. Triangle for errors because it has a little more edge. Square only at very low gain, through a lowpass, for the mechanical toggles.
GainNode shapes the envelope. Gain is an AudioParam, so the whole shape is scheduled up front with setValueAtTime, exponentialRampToValueAtTime and setTargetAtTime. Exponential ramps cannot reach zero, so every envelope starts and ends at 0.0001 instead. The release uses setTargetAtTime, whose exponential decay sounds far more natural than a linear fade.
BiquadFilterNode colours the result. A lowpass on a square wave turns a buzzer into a switch. A bandpass with a swept centre frequency is what turns noise into a whoosh.
A noise AudioBuffer covers everything that is not pitched. One second of white noise, filled once with Math.random(), cached for the life of the context, and played through short envelopes. A click is a 40 ms burst of that noise through a highpass. A whoosh is a longer burst through a bandpass whose frequency ramps up or down.
Every pitched sound is a call to this helper with different numbers.
type ToneOptions = {
freq: number;
to?: number; // optional glide target in Hz
type?: OscillatorType;
attack?: number; // seconds
decay?: number; // seconds
gain?: number; // 0..1 before the master bus
};
export function tone(ctx: AudioContext, out: AudioNode, o: ToneOptions) {
const t0 = ctx.currentTime;
const attack = o.attack ?? 0.005;
const decay = o.decay ?? 0.12;
const peak = o.gain ?? 0.2;
const osc = ctx.createOscillator();
osc.type = o.type ?? "sine";
osc.frequency.setValueAtTime(o.freq, t0);
if (o.to) osc.frequency.exponentialRampToValueAtTime(o.to, t0 + attack + decay);
const env = ctx.createGain();
env.gain.setValueAtTime(0.0001, t0);
env.gain.exponentialRampToValueAtTime(peak, t0 + attack);
env.gain.setTargetAtTime(0.0001, t0 + attack, decay / 4);
osc.connect(env).connect(out);
osc.start(t0);
osc.stop(t0 + attack + decay + 0.05);
}The noise side handles clicks and, with a different filter and envelope, whooshes.
let noise: AudioBuffer | null = null;
function noiseBuffer(ctx: AudioContext) {
if (noise) return noise;
const length = ctx.sampleRate; // one second is plenty
noise = ctx.createBuffer(1, length, ctx.sampleRate);
const data = noise.getChannelData(0);
for (let i = 0; i < length; i++) data[i] = Math.random() * 2 - 1;
return noise;
}
export function click(ctx: AudioContext, out: AudioNode, cutoff = 2500) {
const t0 = ctx.currentTime;
const src = ctx.createBufferSource();
src.buffer = noiseBuffer(ctx);
const filter = ctx.createBiquadFilter();
filter.type = "highpass";
filter.frequency.value = cutoff;
const env = ctx.createGain();
env.gain.setValueAtTime(0.5, t0);
env.gain.exponentialRampToValueAtTime(0.0001, t0 + 0.04);
src.connect(filter).connect(env).connect(out);
src.start(t0);
src.stop(t0 + 0.05);
}Every voice connects to a shared master GainNode and a DynamicsCompressorNode before the destination, so a tick landing on top of a confirmation does not clip. I never hear the compressor working, which is the point.
Designing the palette as a system
Eighteen sounds designed one at a time would sound like eighteen different apps. So I designed families first and individual sounds second.
- Ticks are the quietest and shortest, around 40 ms, high pitched, for hover and scrubbing.
- Clicks are noise bursts with no pitch, for buttons and taps. Primary actions get a brighter cutoff than secondary ones.
- Toggles are a pair. On glides up, off glides down, same length, same gain, so the two feel like one object moving.
- Confirmations are two sine notes a fifth apart, rising.
- Errors are two triangle notes falling, slightly longer, slightly louder.
- Notifications are a bright pair pitched above the confirmations so they cut through whatever else is happening.
- Navigation is a whoosh with a direction. Forward sweeps up, back sweeps down, open and close mirror each other.
The pitch relationships do most of the work. Success rises and error falls, which people read instantly without being told. Everything sits between roughly 260 Hz and 1.8 kHz, because lower gets muddy on laptop speakers and higher gets piercing on headphones. Durations run from 40 ms for a tick to about 400 ms for a navigation whoosh, and nothing is longer. A UI sound that is still playing when the next interaction starts stops being feedback and becomes music.
The palette is one object, so a sound is a row, not a file.
export const palette = {
tick: { kind: "tone", freq: 1800, decay: 0.04, gain: 0.08 },
click: { kind: "noise", cutoff: 2500, decay: 0.04 },
toggleOn: { kind: "tone", freq: 520, to: 780, decay: 0.09 },
toggleOff: { kind: "tone", freq: 780, to: 520, decay: 0.09 },
success: { kind: "seq", notes: [523, 784], step: 0.09, decay: 0.3 },
error: { kind: "seq", notes: [330, 262], step: 0.11, decay: 0.25, type: "triangle" },
notify: { kind: "seq", notes: [880, 1175], step: 0.07, decay: 0.2 },
navigate: { kind: "whoosh", from: 300, to: 1600, decay: 0.4 },
// ...ten more rows, same four shapes
} satisfies Record<string, SoundSpec>;Loudness was the hardest part. I tuned every sound relative to the primary click by ear, then dropped the master gain until the loudest had comfortable headroom. The palette numbers are relative; the master bus is the only absolute.
The useSound hook
The hook is small, but each line answers a browser rule. The render function it calls is a switch over the four palette kinds, and usePersistedState is a thin wrapper around useState and localStorage.
let ctx: AudioContext | null = null;
let master: GainNode | null = null;
const lastPlayed = new Map<SoundId, number>();
function getBus() {
if (!ctx) {
ctx = new AudioContext();
master = ctx.createGain();
master.gain.value = 0.6;
master.connect(ctx.createDynamicsCompressor()).connect(ctx.destination);
}
if (ctx.state === "suspended") void ctx.resume();
return { ctx, out: master! };
}
export function useSound() {
const [muted, setMuted] = usePersistedState("ui-sound-muted", true);
const play = useCallback(
(id: SoundId, minGap = 30) => {
if (muted) return;
const now = performance.now();
if (now - (lastPlayed.get(id) ?? 0) < minGap) return;
lastPlayed.set(id, now);
const { ctx, out } = getBus(); // first call always sits inside a user gesture
render(ctx, out, palette[id]);
},
[muted],
);
return { play, muted, setMuted };
}Autoplay policy. Browsers will not start an AudioContext outside a user gesture. So the context is created lazily inside the first play, which only ever runs from a click, key or pointer handler, and it is resumed if the browser suspended it while the tab was in the background. Never create it at module load; it will start suspended and nothing will play.
The iOS silent switch. On iOS, Web Audio respects the ring/silent switch. Silent switch on, nothing plays, no error, no event. There is no reliable workaround and I do not try; a silent phone is just a user who has muted.
Debouncing. A range input firing a tick per pixel is unpleasant and, on a cheap device, measurable. Each call carries a minimum gap, 30 ms by default and higher for ticks, tracked per sound id so a click and a tick can still overlap.
Global mute. Sound is off until the user turns it on, and the choice is persisted. With prefers-reduced-motion set, the navigation whooshes are dropped too, because the transitions they belong to no longer animate.
Sound is never the only feedback
Every sound in the palette is attached to a state change that is already visible. A toggle moves. A button depresses. A toast appears. If the context is suspended, the switch is on silent, or the user has muted, nothing is lost. A sound-only error would be an accessibility bug, and a sound-only success would be worse.
The mute control is focusable, labelled, and exposes its state through aria-pressed. The sound layer is decoration on top of a working interface, not part of its contract.
What I'd carry forward
- Design families and pitch relationships before designing any individual sound. Rising means yes, falling means no.
- Keep the palette in one config object. Sounds are rows, not files.
- Create the
AudioContextlazily inside a gesture, resume it defensively, and treat silence as a normal state. - Rate-limit per sound id, not globally.
- Never let sound carry meaning on its own. Pair every sound with a visible state.