Skip to content
Thrum
Docs · 06 of 09All pages

Sound

How sound works, the switch, the volume, and how to keep things quiet.

Some components can make a small sound when something happens: a tick as a chart cursor moves, a soft tone when a number lands. Sound is off until the visitor turns it on. Your part is to give them a switch, which is one hook.

How it works

  • Synthesised, not recorded. Every sound is built in the browser with the Web Audio API from filtered noise and sine tones. There are no audio files and no dependencies.
  • Nothing runs until sound is on. While it is off, a component's request to play returns at once. No audio context is created, nothing loads, and nothing plays.
  • One switch for the page. The setting is shared by every component and saved on the device, so it holds across pages and visits. It is kept in the browser's local storage under thrum:sound and thrum:sound-volume.
  • One shared audio context, created on the first sound after the switch goes on. Turning sound off fades anything still ringing and releases the audio device.
  • Silent in the background. Nothing plays while the page is hidden, and hiding the page releases the audio device. The next sound wakes it.
  • Short. Clicks last at most 40 ms, answers to an action 200 ms, outcomes 400 ms. The longest sound lasts 392 ms. All tones come from one five-note scale, so sounds that overlap stay in tune.
  • Levelled. Every sound is trimmed to a loudness target, and outcomes are ranked: success is quieter than warn, and warn quieter than error.
  • Not tied to reduced motion. Sound has its own switch. A visitor who asks for less motion may still want sound, so prefers-reduced-motion does not change it.

Add a sound switch

You will add a switch and a volume slider to your app. useSound() comes from lib/thrum-sound.ts, which is installed with the first component that uses sound.

components/sound-switch.tsx
"use client";

import { useSound } from "@/lib/thrum-sound";

/** A sound switch and a volume slider for a settings panel. */
export function SoundSwitch() {
  const { enabled, volume, setEnabled, setVolume } = useSound();
  return (
    <div className="flex items-center gap-4">
      <button
        type="button"
        role="switch"
        aria-checked={enabled}
        onClick={() => setEnabled(!enabled)}
      >
        Sound
      </button>
      <label className="flex items-center gap-2">
        Volume
        <input
          type="range"
          min={0}
          max={1}
          step={0.05}
          value={volume}
          disabled={!enabled}
          onChange={(event) =>
            setVolume(Number(event.target.value))
          }
        />
      </label>
    </div>
  );
}

The hook returns five things:

Name and typeWhat it is
enabledbooleanWhether sound is on. It is false on the server and until the saved setting is read.
volumenumberFrom 0 to 1.
setEnabled(on: boolean) => voidTurns sound on or off. Call it from a click or key handler.
setVolume(volume: number) => voidSets the volume.
play(name: SoundName) => voidPlays a palette sound, if sound is on.

You should see

Click the switch: you hear a short rising pair of notes, the sound of the switch itself. Reload the page: the switch is still on. Every component on the page follows it, and so does a second switch elsewhere in your app.

Outside React, the same functions are plain exports: setSoundEnabled, isSoundEnabled, setVolume and play. To mark an event in your own interface with a palette sound, call play:

Play a palette sound
import { play } from "@/lib/thrum-sound";

async function save() {
  const ok = await submit();
  play(ok ? "success" : "error");
}

A sound is never the only feedback. Show the same event on screen as well.

Volume

  • The volume runs from 0 to 1 and starts at 0.8. Values outside that range are clamped.
  • The slider follows a squared curve, so the lower half of its travel gives fine control over quiet levels.
  • It is saved on the device together with the switch.
  • Sounds mix with whatever else the device is playing. Nothing is paused or turned down.

Keep one component quiet

Every component that can play a sound takes a sound prop. It defaults to true, which means “play if the visitor has sound on”. Pass false to keep that one component silent whatever the switch says:

A silent chart
<AreaChart data={data} label="Revenue" sound={false} />

Components that make a sound today: Calendar, Checkbox, Checkbox group, Choice card, Color picker, Combobox, Countdown, Date input, Date picker, Date range picker, File upload, Form, Inline edit, Input group, Input OTP, Mask input, Mini calendar, Multi select, Native select, Number input, Password input, Prompt input, Radio group, Rating, Search field, Select, Slider, Switch, Tags input, Theme switcher, Time input, Copy button, Floating action button, Hold to confirm, Segmented control, Sound toggle, Toggle, Toggle group, Alert dialog, Command palette, Confirm popover, Context menu, Dialog, Drawer, Dropdown menu, Menubar, Notification center, Popover, Sheet, Toast, Toolbar, Navbar, Pagination, Sidebar, Stepper, Tabs, Data table, Diff viewer, Filter bar, JSON viewer, Sortable list, Tag, Transfer list, Tree view, Virtual list, Alert, Animated list, Banner, Code block, Image zoom, Reasoning panel, Result state, Tool call card, Accordion, Carousel, Collapsible, Data list, Resizable panels, Animated number, Stat card, Area chart, Bar chart, Bar list, Bullet chart, Calendar heatmap, Category bar, Chart legend, Cohort grid, Combo chart, Donut chart, Funnel chart, Heatmap, Histogram, Line chart, Live chart, Metric comparison, Radar chart, Radial bar chart, Scatter chart, Sparkline, Stacked bar chart, Status tracker, Treemap, Waterfall chart, Gauge, Progress bar, Ring meter, Usage meter, Activity feed, Image compare, Text scramble, Ticker tape, App shell, Chat thread, Finance dashboard, Kanban board, KPI grid, Metric table, Onboarding checklist, Ops monitoring dashboard, Overview dashboard, SaaS metrics dashboard, Sales dashboard, Sign in form, Support dashboard, Web analytics dashboard. Each component page says which sound it plays and when. Components are always silent on initial page load; sounds fire only in response to user-triggered interactions (or live data changes you pass after sound is turned on), never on hover. Outcome sounds such as warn are wired to interactive warning states and can also be called directly via play("warn").

The palette

16 sounds, named by what happened. Lengths are measured from start to silence. Hear each one on the Sounds page.

SoundWhen it playsKindLength
tickThe cursor lands on a different chart point, or a slider or stepper crosses a step.Used by: Countdown, Date input, Inline edit, Input group, Input OTP, Number input, Search field, Slider, Time input, Hold to confirm, Stepper, Filter bar, Sortable list, Carousel, Resizable panels, Area chart, Bar chart, Bullet chart, Calendar heatmap, Category bar, Cohort grid, Combo chart, Donut chart, Funnel chart, Heatmap, Histogram, Line chart, Live chart, Radar chart, Radial bar chart, Scatter chart, Sparkline, Stacked bar chart, Status tracker, Treemap, Waterfall chart, Image compare, Text scramble, Ticker tape, Finance dashboard, Kanban board, Onboarding checklist, Ops monitoring dashboard, Overview dashboard, SaaS metrics dashboard, Sales dashboard, Support dashboard, Web analytics dashboardmicro22 ms
hoverA soft hover. No component plays it by default.micro32 ms
selectA series, row, legend item or tab is picked.Used by: Calendar, Choice card, Color picker, Combobox, Date picker, Date range picker, Mini calendar, Native select, Prompt input, Radio group, Rating, Search field, Select, Theme switcher, Floating action button, Segmented control, Toggle group, Command palette, Context menu, Dropdown menu, Menubar, Notification center, Pagination, Tabs, Data table, Diff viewer, Filter bar, Tree view, Virtual list, Tool call card, Bar list, Combo chart, Line chart, Radar chart, Stacked bar chart, App shell, Chat thread, Finance dashboard, Metric table, Ops monitoring dashboard, Overview dashboard, SaaS metrics dashboard, Sales dashboard, Support dashboard, Web analytics dashboardcontrol114 ms
toggleOnA switch turns on. Also the confirmation when sound itself is turned on.Used by: Checkbox, Checkbox group, Choice card, Password input, Switch, Sound toggle, Toggle, Context menu, Dropdown menu, Menubar, Toolbar, Sidebar, Data table, Tag, Chart legend, App shell, Sign in formcontrol168 ms
toggleOffA switch turns off.Used by: Checkbox, Checkbox group, Choice card, Password input, Switch, Toggle, Context menu, Dropdown menu, Menubar, Toolbar, Sidebar, Data table, Tag, Chart legend, App shell, Sign in formcontrol168 ms
openA panel, menu or popover opens.Used by: Color picker, Combobox, Date picker, Date range picker, Multi select, Select, Theme switcher, Floating action button, Alert dialog, Command palette, Confirm popover, Context menu, Dialog, Drawer, Dropdown menu, Menubar, Notification center, Popover, Sheet, Navbar, Data table, Filter bar, JSON viewer, Tree view, Image zoom, Reasoning panel, Tool call card, Accordion, Collapsible, App shell, Onboarding checklistcontrol153 ms
closeA panel, menu or popover closes.Used by: Color picker, Combobox, Date picker, Date range picker, Multi select, Prompt input, Select, Theme switcher, Floating action button, Alert dialog, Command palette, Confirm popover, Context menu, Dialog, Drawer, Dropdown menu, Menubar, Notification center, Popover, Sheet, Navbar, Data table, Filter bar, JSON viewer, Tree view, Image zoom, Reasoning panel, Tool call card, Accordion, Collapsible, App shell, Onboarding checklistcontrol128 ms
addAn item joins a list.Used by: File upload, Multi select, Prompt input, Tags input, Notification center, Filter bar, Transfer list, Animated list, Activity feed, Chat thread, Overview dashboardcontrol156 ms
removeAn item leaves a list.Used by: Date picker, File upload, Multi select, Prompt input, Tags input, Notification center, Filter bar, Tag, Transfer list, Animated listcontrol142 ms
liftAn item is picked up to be dragged.Used by: Sortable list, Kanban boardcontrol86 ms
dropA dragged item is set down, or the drag is cancelled.Used by: Sortable list, Kanban boardcontrol86 ms
successAn action finished.Used by: Countdown, File upload, Inline edit, Input OTP, Mask input, Copy button, Hold to confirm, Toast, JSON viewer, Alert, Code block, Reasoning panel, Result state, Tool call card, Data list, Progress bar, Onboarding checklistnotice352 ms
warnAn action finished, but something needs a look.Used by: Toast, Alert, Banner, Result statenotice392 ms
errorAn action failed and can be tried again.Used by: File upload, Form, Inline edit, Copy button, Toast, JSON viewer, Alert, Banner, Code block, Result state, Tool call card, Data list, Sign in formnotice392 ms
countDoneAn animated number lands on its value.Used by: Animated number, Stat card, Metric comparison, Ring meter, Finance dashboard, KPI grid, Overview dashboard, SaaS metrics dashboard, Sales dashboard, Support dashboard, Web analytics dashboardnotice212 ms
thresholdA value climbs over a marked limit.Used by: Live chart, Gauge, Usage meter, Ops monitoring dashboard, Support dashboardnotice314 ms

Rate limiting

A cursor swept across a chart can ask for dozens of ticks a second. The engine thins them out, so components and your own code can call play freely with no timers.

  • A sound asked for again within 60 ms of itself (a click) or 80 ms (anything else) is dropped, not queued. One repeated within 200 ms plays softer, down to half its level, so a fast run patters instead of hammering.
  • At most 6 sounds ring at once. Clicks give way earlier, when 3 are ringing. Other sounds still play up to the cap.
  • Each play varies pitch and level slightly, so a repeated sound does not sound mechanical. Pitched sounds move by less than a seventh of a semitone and stay in tune.
  • A soft limiter sits after the volume. One sound alone never reaches it; sounds that stack are bent smoothly under −1 dBFS instead of clipping.
  • If the audio device takes more than 300 ms to wake, the waiting sound is dropped. A late sound is worse than none.

The browser's autoplay rule

Browsers do not let a page make sound until the visitor has clicked or pressed a key on it. Thrum follows the rule and does not create an audio context before that. Two things follow:

  • Call setEnabled(true) from a click or key handler, as the example does. Turning sound on from an effect or a timer saves the setting but may stay silent.
  • A returning visitor with sound saved as on hears nothing until their first click or key press on the new page. Moving the pointer does not count, so a chart swept straight after a reload is silent at first.

If it goes wrong

No sound at all, with the switch on: check the device volume and the tab's mute state, then click once anywhere on the page. If the browser has no Web Audio support, every call does nothing and no error is thrown.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor