> A guide to [@fabiocv/ui](https://ui.fabio.cv/). Every guide is listed at https://ui.fabio.cv/llms.txt.

# Sound

Interaction sounds for a click, or for as long as a transition: clean tones,
synthesised with Web Audio and exported from `@fabiocv/ui/sound`. Components
make no sound by themselves. The page gives them one.

```ts
import { PENTATONIC, sound, TECH } from '@fabiocv/ui/sound';

const click = sound({ ...TECH, notes: PENTATONIC });

click.play(); // on a click

click.start(); // when a motion starts
click.stop(); // when the motion ends
```

- `TECH` is the sound of the system: two soft, ringing ticks, the second higher and
  louder. `TECH_CLOSE` falls instead, its second tick lower, for closing
  what a click opened: a menu from its trigger, or a drawer from its close tab. `TECH_HOVER` is one tick of it, shorter and much
  quieter, for the pointer entering a menu item and for a submenu opening.
- `play()` plays the sound once, until its spring has settled.
- `start()` plays the sound from its first values. `stop()` fades it out. Until
  then it keeps playing.
- `play()` and `start()` return the note they play, as times the pitch.
- With `notes`, each start moves to the next note, and round again. One
  sound shared by every control walks the notes click by click. `PENTATONIC`
  goes up a pentatonic scale to the fifth above the pitch, down to the fourth
  below, and back.
- A value given as a pair travels from its first to its second on `spring`,
  then holds. Give it the motion's spring and the two move together. A single
  number holds throughout.
- Starting a sound that is still playing fades the old one out under the new.
- The sounds are clean. They are pure tones with no noise in them, and what
  varies, across a transition and from one click to the next, is the pitch and
  not the volume. Start a new one from `TECH`.
- Let hover sounds wait until `audioRunning()`, so the pointer alone never wakes
  the page's audio.
- Give visitors a way to turn sound off: a `Toggle` with the `Sound` icon in a
  menu or beside other icons, or a `Switch` named for it among settings. On
  fabio.cv it starts on.
- A browser plays nothing until the page has had a click or a key press.
  `audioRunning()` says whether it plays yet, and `subscribeAudio()` calls a
  listener as that changes.

| Option       | What it sets                                     | Default |
| ------------ | ------------------------------------------------ | ------- |
| `pitch`      | The tone's pitch, in Hz                          |         |
| `wave`       | `sine`, `triangle`, `square` or `sawtooth`       | `sine`  |
| `harmony`    | A second tone at this many times the pitch       | `0`     |
| `ticks`      | How many ticks a second the sound is broken into | `0`     |
| `ring`       | How long each tick rings                         | `12`    |
| `volume`     | How loud, from `0` to `1`                        | `0.1`   |
| `brightness` | Where the highs are cut, in Hz. Lower is duller  | Open    |
| `flutter`    | How many times a second the loudness wavers      | `0`     |
| `spring`     | The spring the travelling values move on         |         |
| `attack`     | Seconds to fade in                               | `0.02`  |
| `release`    | Seconds to fade out after `stop()`               | `0.12`  |
| `notes`      | The notes it steps through, as times the pitch   | `[1]`   |

- Pitch and brightness travel in even musical steps, the rest in even amounts.
  Where a spring overshoots, the pitch overshoots with it.
- A harmony is half as loud as the tone, and the two together peak at the
  volume. `1.5` is a fifth above and `2` an octave. With ticks, each tick
  rings at both.
- A tick is a short ring at the pitch, in place of the tone: `wave` does not
  shape it. It dies away in about a third of `ring` cycles, so higher ticks
  are shorter. A long ring carries into the next tick, as a struck bar does.
- A flutter swings the loudness `30%` either side of its level.
- The sound is silent `2 × release` after `stop()`.

## Types of `@fabiocv/ui/sound`

```ts
/** A value that holds, or one that travels from its first to its second. */
export type Travel = number | readonly [from: number, to: number];

export type SoundOptions = {
    /** The tone's pitch, in Hz. */
    pitch: Travel;
    /** The tone's waveform. Defaults to `sine`. */
    wave?: Exclude<OscillatorType, 'custom'>;
    /**
     * A second tone at this many times the pitch, half as loud: `1.5` is a
     * fifth above, `2` an octave. Defaults to `0`, none.
     */
    harmony?: number;
    /**
     * Breaks the sound into this many ticks a second, each a short ring at the
     * pitch in place of the tone. Defaults to `0`, unbroken.
     */
    ticks?: Travel;
    /**
     * How long each tick rings: it dies away in about a third of this many
     * cycles of its pitch. Defaults to `12`.
     */
    ring?: number;
    /** How loud, from `0` to `1`. Defaults to `0.1`. */
    volume?: Travel;
    /** Where the highs are cut, in Hz. Lower is duller. Defaults to open. */
    brightness?: Travel;
    /** How many times a second the loudness wavers. Defaults to `0`, steady. */
    flutter?: Travel;
    /** The spring every travelling value moves on, as the motion does. */
    spring: Spring;
    /** Seconds to fade in. Defaults to `0.02`. */
    attack?: number;
    /** Seconds to fade out after `stop()`. Defaults to `0.12`. */
    release?: number;
    /**
     * Each time the sound starts it moves to the next of these, and round
     * again. A note is as many times the pitch. Defaults to `[1]`, the pitch.
     */
    notes?: readonly number[];
};

export type Sound = {
    /**
     * Starts the sound from its first values, ending one still playing.
     * Returns the note it plays, as times the pitch.
     */
    start: () => number;
    /** Fades the sound out. */
    stop: () => void;
    /** Starts the sound, and stops it once its spring has settled. Returns its note. */
    play: () => number;
};

/** Two soft, ringing ticks, the second higher and louder. */
export declare const TECH: {
    pitch: [number, number];
    harmony: number;
    ticks: [number, number];
    ring: number;
    volume: number;
    spring: {
        response: number;
        dampingRatio: number;
    };
    attack: number;
    release: number;
};

/** Tech falling: the same ticks, the second lower, for closing what a click opened. */
export declare const TECH_CLOSE: {
    pitch: [number, number];
    harmony: number;
    ticks: [number, number];
    ring: number;
    volume: number;
    spring: {
        response: number;
        dampingRatio: number;
    };
    attack: number;
    release: number;
};

/** Tech for the pointer: one tick, shorter and much quieter. */
export declare const TECH_HOVER: {
    ticks: [number, number];
    ring: number;
    volume: number;
    spring: {
        response: number;
        dampingRatio: number;
    };
    attack: number;
    release: number;
    pitch: [number, number];
    harmony: number;
};

/**
 * Notes of a pentatonic scale around the pitch: up to the fifth above, down
 * to the fourth below, and back.
 */
export declare const PENTATONIC: number[];

/** Whether the page's audio plays. It waits for a click or key press. */
export declare function audioRunning(): boolean;

/** Calls `listener` as the audio starts or stops. For `useSyncExternalStore`. */
export declare function subscribeAudio(listener: () => void): () => void;

/**
 * A sound for a click, or one that lasts as long as a transition. Play it on
 * the click. Or start it when the motion starts and stop it when the motion
 * ends. Each value given as a pair travels from its first to its second on
 * the spring, then holds.
 */
export declare function sound(options: SoundOptions): Sound;
```

## Example

```tsx
'use client';

import { Button } from '@fabiocv/ui/button';
import {
  PENTATONIC,
  sound,
  TECH,
  TECH_CLOSE,
  TECH_HOVER,
} from '@fabiocv/ui/sound';

const open = sound({ ...TECH, notes: PENTATONIC });
const close = sound({ ...TECH_CLOSE, notes: PENTATONIC });
const hover = sound({ ...TECH_HOVER, notes: PENTATONIC });

export function SoundDemo() {
  return (
    <>
      <Button onClick={open.play}>Open</Button>
      <Button onClick={close.play}>Close</Button>
      <Button onClick={hover.play}>Hover</Button>
    </>
  );
}
```

See it at https://ui.fabio.cv/sound/.
