Guide.

How to use Onda: its API, how to mount it on a site, when it should and shouldn’t play, and its accessibility.

Introduction

Onda is Flouvia’s interface sound library: eleven sounds with the timbre of a drop falling into water, in its Seda version, synthesized live with Web Audio. It ships no audio files: it weighs a few kilobytes.

It’s at version 0.1.0 and stays private until v1, when it reaches npm as @flouvia/onda, under the MIT license. Meanwhile, this guide documents its real API, the same one that plays on flouvia.com.

How it ships

Onda isn’t on npm until v1. Then it installs like any package and imports from @flouvia/onda, with its types. Until then, flouvia.com, Flouvia OS and Ops use their own copy, with the same sounds and the same numbers.

The API

Four functions and one type to play, turn on and turn off. Each person’s preference and the files have their own, each in its section.

NameSignatureWhat it does
play (name: OndaSound) => voidPlays a sound if Onda is on. The first call creates the AudioContext.
emit (name: OndaSound) => voidFires the global flouvia:sound event, with the name in detail.
soundEnabled () => booleanWhether Onda is on. It is by default.
setSoundEnabled (on: boolean) => voidTurns it on or off, remembers the choice and sets data-sound on <html>.
OndaSound typeThe eleven names: logo, tap, hover, open, close, on, off, send, success, error and notify.

The sounds

Each sound is named for what happens, not for how it sounds: send means sending, even if its timbre changes tomorrow. The OndaSound type gathers them and your editor completes them.

TypeScript
import type { OndaSound } from '@flouvia/onda';

// 'logo' | 'tap' | 'hover' | 'open' | 'close' | 'on' | 'off'
// | 'send' | 'success' | 'error' | 'notify'
const sound: OndaSound = 'send';

Playing a sound

play(name) plays a sound if Onda is on. The first call creates the AudioContext, so always call it from a gesture: a click, a key or a tap. The browser won’t play sound before one.

TypeScript
import { play } from '@flouvia/onda';

button.addEventListener('click', () => {
  play('open');
  sheet.show();
});
  • When off, it plays nothing and creates nothing.
  • Two identical sounds less than 60 ms apart play once.
  • Each repeat varies ±2.5% in pitch and ±12% in volume.
  • It returns nothing and never throws: without Web Audio, it simply stays silent.

Emitting a sound

emit(name) fires the global flouvia:sound event, for components that don’t import Onda. Whoever mounts it on the page listens and plays.

TypeScript
import { emit, type OndaSound, play } from '@flouvia/onda';

// In a component: announce it, whoever plays it
emit('success');

// Once per page, whoever mounts Onda listens and plays
window.addEventListener('flouvia:sound', (e) => play((e as CustomEvent<OndaSound>).detail));

Turning it on and off

soundEnabled() tells you whether Onda is on, and setSoundEnabled(on) turns it on or off: it saves the choice in localStorage, under the flouvia:sound key, one per domain, and sets data-sound="on" or "off" on <html> so your CSS knows. This is the sound button:

TypeScript
import { play, setSoundEnabled, soundEnabled } from '@flouvia/onda';

toggle.setAttribute('aria-pressed', String(soundEnabled()));

toggle.addEventListener('click', () => {
  if (soundEnabled()) {
    play('off');
    setSoundEnabled(false);
  } else {
    setSoundEnabled(true);
    play('logo');
  }
  toggle.setAttribute('aria-pressed', String(soundEnabled()));
});

Order matters: turning it off plays off and then goes silent; turning it on switches it on first and then plays the logo.

As files

A native app, a push notification or a video has no Web Audio. For them, render(name) plays the sound offline, through the same chain as play(), and returns it as an AudioBuffer ready to save.

NameSignatureWhat it does
render (name: OndaSound, options?: RenderOptions) => Promise<AudioBuffer>Plays a sound offline, at full volume, and returns it in stereo, 3 s long. It needs no gesture and makes no sound, ignores the person’s preference and never touches the AudioContext of play(). It requires OfflineAudioContext.
RenderOptions typetake: 0, the reference take, or from 1, a variation. sampleRate: 48,000 by default.
TypeScript
import { render } from '@flouvia/onda';

// The reference take: every drop on its exact note, level and place
const notify = await render('notify');

// A variation, as it plays live, identical every time
const again = await render('notify', { take: 2 });

// A stereo AudioBuffer, 3 s long at 48 kHz, ready to save
notify.getChannelData(0);
  • Take 0 is the reference: every drop on its exact note, level and place. From take 1, each one varies as it does live (±2.5% in pitch and ±12% in volume) and comes out identical every time. A sound that repeats in an app never sounds mechanical.
  • It is the sound of play(), sample for sample: the same chain, starting at the same point in its audio block. In Chrome, the only difference from play() is the browser’s rounding, below −90 dBFS.
  • render() returns it at the browser’s level, with the volume at full. Onda’s files add a single gain for all of them, until the loudest reaches −1 dBTP: they sit alongside system sounds and keep their steps.
  • Per platform: 24-bit WAV for video and prototypes, 16-bit CAF for iOS and its notifications (UNNotificationSound) and Ogg Vorbis for Android (res/raw), all at 48 kHz. Name them with lowercase letters, numbers and underscores, like onda_notify_2, which Android accepts as is.

Mounting it on a site

On a site, every link and button clicks by delegation: one listener on the document, in the capture phase. It plays tap, or the sound the button asks for with data-sound (the contract of Cuenca’s button, which also asks for success with the cuenca:sound event). The near-subliminal hover is requested with data-sound-hover and only plays with a pointer.

TypeScript
import { type OndaSound, play } from '@flouvia/onda';

const SOUNDS =['logo', 'tap', 'hover', 'open', 'close', 'on', 'off', 'send', 'success', 'error', 'notify'];

document.addEventListener('click', (e) => {
  const el = (e.target as Element | null)?.closest('a, button, [role="button"]');
  if (!el || el.closest('[data-sound="none"], [data-sound-own], .sound-toggle')) return;
  const asked = el.getAttribute('data-sound') ?? '';
  play((SOUNDS.includes(asked) ? asked : 'tap') as OndaSound);
}, { capture: true });

document.addEventListener('cuenca:sound', (e) => play((e as CustomEvent<{ sound: OndaSound }>).detail.sound));

if (matchMedia('(hover: hover) and (pointer: fine)').matches) {
  let last: Element | null = null;
  document.addEventListener('pointerover', (e) => {
    const el = (e.target as Element | null)?.closest('[data-sound-hover]') ?? null;
    if (el && el !== last) play('hover');
    last = el;
  }, { passive: true });
}

An element that plays its own sound (the send button, which plays send) carries data-sound-own; one that shouldn’t make a sound, data-sound="none".

HTML
<a href="/work" data-sound-hover>Work</a>
<button type="submit" data-sound-own>Send</button>
<button type="button" data-sound="none">Later</button>

When to play, and when not

The more frequent, the subtler. On a site, clicks and hover play; in a portal or a console, where people work all day, navigating stays silent: only actions play.

  • On a site: tap on every link and button, hover where it’s requested, open and close on the menu and the form, and send, success and error when it’s sent.
  • In a portal: open and close on sheets, menus and dialogs; on and off on switches; send, success and error on what gets sent, and notify when something new arrives.
  • Everywhere: logo when sound is turned on and off when it’s turned off.
  • Never: on page load, while scrolling, for a change nobody asked for, or on an inbox’s first load.

When it stays silent

In sound, the bar isn’t sounding beautiful: it’s knowing when to stay quiet. Onda checks on every play(), before it creates anything, with nothing for you to do.

  • The iPhone’s silent mode: when its AudioContext is born, Onda sets navigator.audioSession.type = 'ambient' (the Audio Session API, Safari 16.4 or later), only if it exists and nobody chose another session. That way it respects the silent switch and mixes with the person’s music instead of cutting it off. The session applies to the whole page.
  • A hidden tab: with document.visibilityState === 'hidden' nothing plays. When the tab hides, whatever was playing fades out in 30 ms, with no click, and the AudioContext is suspended until the next play().
  • One voice: across tabs and windows of the same site, only the one that’s visible and has focus plays (document.hasFocus()). When focus moves to another, whatever was playing fades out; an iframe on the same page doesn’t count.
  • Video and audio: if a <video> or <audio> on the page is playing with sound, Onda doesn’t play over it, and when one starts, Onda silences what it was playing. A muted one (muted) or one at zero volume doesn’t count.

The preference

Each person chooses on their device “On,” “Actions only” or “Off,” and the volume. It’s saved in localStorage, like system settings, and it overrides the defaults: on, at full volume. In Flouvia OS and Ops it lives in the account menu.

NameSignatureWhat it does
soundPreference () => SoundPreference'on', 'actions' or 'off'. Turned off is 'off'; turned on, its mode.
setSoundPreference (preference: SoundPreference) => voidChooses it and remembers it. Turning off keeps the mode: the sound button goes back to it when turned on.
soundVolume () => numberThis device’s volume, from 0 to 1. It’s 1 by default.
setSoundVolume (volume: number) => voidChanges it, remembers it and applies it to what’s already playing. It follows the ear: halfway is −12 dB. At 0 nothing plays.
SoundPreference type'on' | 'actions' | 'off'.
  • “Actions only” silences the interface sounds, the ones that go with a gesture (tap, hover, open, close, on and off), and keeps the ones that say what happened: send, success, error, notify and logo.
  • It sounds like the sound button: turning on plays the logo; turning off plays off and then goes silent. Releasing the volume plays success at that volume.
  • Every change fires the flouvia:sound-change event on window; another tab reports it through storage. That keeps the button and the control in sync.
  • The keys, one per domain: flouvia:sound (on or off, the button’s), flouvia:sound-mode (all or actions) and flouvia:sound-volume.
TypeScript
import {
  play, setSoundPreference, setSoundVolume, soundPreference, soundVolume, type SoundPreference,
} from '@flouvia/onda';

// On, Actions only or Off: three radios with value "on", "actions" and "off"
for (const radio of radios) {
  radio.checked = radio.value === soundPreference();
  radio.addEventListener('change', () => {
    const before = soundPreference();
    if (radio.value === 'off') play('off');
    setSoundPreference(radio.value as SoundPreference);
    if (before === 'off') play('logo');
  });
}

// Volume, from 0 to 1; on release you hear how it sounds
volume.value = String(soundVolume() * 100);
volume.addEventListener('input', () => setSoundVolume(Number(volume.value) / 100));
volume.addEventListener('change', () => play('success'));

// The sound button or another tab can change it too
window.addEventListener('flouvia:sound-change', sync);
window.addEventListener('storage', sync);

Never the only signal

What you hear, you also see, and it’s announced (WCAG 1.3.3). Someone who can’t hear, has sound off or has their iPhone on silent finds out all the same: sound accompanies, it never informs alone.

  • An error, with its message in view in a role="alert"; if a dialog is open, inside the dialog.
  • A send or a success, with its state in view and spoken in a live region (role="status") that was already on the page: one that appears along with its text isn’t always read.
  • What arrives (notify), in view where it arrives and announced: “New reply in ‘…’.”
  • Opening and closing move focus or change aria-expanded; a switch changes aria-pressed or its checkbox.
  • When you add a play(), check the same thing: what does someone who can’t hear it see and hear?
TypeScript
import { play } from '@flouvia/onda';

// status: role="status" · problem: role="alert", both already on the page
play('send');
status.textContent = 'Sending…';
try {
  await send();
  play('success');
  status.textContent = 'Message sent.';
} catch {
  play('error');
  problem.textContent = 'We couldn’t send it. Please try again.';
}

notify sounds only for what arrived while the person was looking: the refresh compares with what was last painted, never on first load or for what they wrote themselves. One refresh, one notice: if several arrive, one sounds and is announced.

TypeScript
import { play } from '@flouvia/onda';
import { announce } from '@flouvia/cuenca/behavior';

// Every 20 s, with the tab in view and no write in progress
const next = await read();
const arrived = arrivedFromOthers(painted, next); // what wasn’t there and someone else wrote
if (arrived) {
  play('notify');
  announce(`New reply in “${arrived.title}”.`);
}
paint(next); // it rises in the list with its last line

Accessibility

  • It can always be turned off: every surface has its sound button, with aria-pressed, and the choice is remembered.
  • It’s never the only signal: anything you hear, you can also see, and it’s announced. An error has its message, a send its state and a notification its notice.
  • It knows when to stay quiet: it respects the iPhone’s silent mode and doesn’t play in a hidden tab, without focus or over a video.
  • Nothing plays before the first gesture, not the logo and not a notification.
  • Levels are measured: no sound plays louder than another, and hover is barely audible.
  • Everything sits between 600 Hz and 5 kHz: it sounds the same on a laptop or phone speaker, with no highs that sting.