# Delay

> A delay device: four modes, a time or a note value, a feedback, a tone and a mix, a panel that draws the echoes with a handle, a Web Audio engine and tools for agents.

```tsx
import { useState } from "react";
import {
  type DelayState,
  delayDevice,
} from "@/components/audio-ui/devices/delay/device";
import { createDelayEngine } from "@/components/audio-ui/devices/delay/engine";
import { DelayPanel } from "@/components/audio-ui/devices/delay/panel";
import { useDeviceLoop } from "@/hooks/use-device-loop";
import { useDeviceSignal } from "@/hooks/use-device-signal";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useDemoSound } from "@/hooks/use-demo-source";

const MASTER_VOLUME = 0.7;
/** A dotted eighth: the echo that has made a thousand guitars sound larger. */
const START = { division: "1/8d", sync: true };

/**
 * A delay you can plug in: its state, a panel that draws the echoes it makes
 * and an engine that plays it, each its own file and none knowing the others.
 * Play the loop and drag the handle: across for the time, up for the feedback.
 * It starts synced to the loop's tempo, on a dotted eighth.
 */
export function BlockDelay() {
  const [state, setState] = useState<DelayState>(
    () => delayDevice.apply(delayDevice.defaults(), START).state
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine, tempo } = useDeviceLoop({
    create: createDelayEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, delayDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <DelayPanel
        onValueChange={setState}
        signal={signal}
        tempo={tempo}
        value={state}
      />
    </div>
  );
}
```

Repeats the sound. The echoes fade by the feedback, each darker than the one before. It is a [device](/docs/devices): a state, a panel, an engine and tools, each its own file.

## Installation

**CLI**

```bash
npx shadcn@latest add @audio-ui/delay
```

It installs the device's state, its engine and its panel to `components/audio-ui/devices/delay/`. The demo above, which wires them to a loop, is `@audio-ui/block-delay`.

**Manual**

**1. Install the packages:**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/react@0.1.0-beta.14
```

**2. Add the pieces you want. All of them, or only the state and the panel if your engine is elsewhere:**

```bash
npx shadcn@latest add @audio-ui/delay-device @audio-ui/delay-panel @audio-ui/delay-engine
```

**3. Copy and paste the demo that wires them, if you want it.**

```tsx title="components/examples/block-delay.tsx"
import { useState } from "react";
import {
  type DelayState,
  delayDevice,
} from "@/components/audio-ui/devices/delay/device";
import { createDelayEngine } from "@/components/audio-ui/devices/delay/engine";
import { DelayPanel } from "@/components/audio-ui/devices/delay/panel";
import { useDeviceLoop } from "@/hooks/use-device-loop";
import { useDeviceSignal } from "@/hooks/use-device-signal";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useDemoSound } from "@/hooks/use-demo-source";

const MASTER_VOLUME = 0.7;
/** A dotted eighth: the echo that has made a thousand guitars sound larger. */
const START = { division: "1/8d", sync: true };

/**
 * A delay you can plug in: its state, a panel that draws the echoes it makes
 * and an engine that plays it, each its own file and none knowing the others.
 * Play the loop and drag the handle: across for the time, up for the feedback.
 * It starts synced to the loop's tempo, on a dotted eighth.
 */
export function BlockDelay() {
  const [state, setState] = useState<DelayState>(
    () => delayDevice.apply(delayDevice.defaults(), START).state
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine, tempo } = useDeviceLoop({
    create: createDelayEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, delayDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <DelayPanel
        onValueChange={setState}
        signal={signal}
        tempo={tempo}
        value={state}
      />
    </div>
  );
}
```

**Agent**

Paste this prompt to your agent. It does the rest: it installs the skill, adds the component and wires it to your audio.

```text
Install the audio/ui skill with `npx skills add ouestlabs/audio-ui`, then follow it to add the delay from audio/ui to my project and wire it to my audio code. Read the docs at https://www.audio-ui.dev/llms.txt first.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Controls

| Control | Range | Default | What it does |
| --- | --- | --- | --- |
| On | On or off | On | Turns the device on. Off, the signal passes through unchanged. |
| Mode | Echo, Ping-pong, Multitap, Grain | Echo | How it repeats, picked in the title bar. Echo, the default, repeats the sound after the time. Ping-pong takes the middle of the sound in on the left and bounces its echoes from side to side. Multitap echoes at half the time and at the time, at half and at three tenths of the level. Grain repeats it as short grains, transposed and scattered. |
| Time | 10 ms to 2000 ms | 350 ms | The time between the echoes. Drag the handle on the first echo across to set it. |
| Sync | On or off | Off | Takes the time from a note value and the tempo of the host instead of milliseconds. See [Tempo sync](/docs/devices#tempo-sync). |
| Note | 1/32 triplet, 1/32, 1/32 dotted, 1/16 triplet, 1/16, 1/16 dotted, 1/8 triplet, 1/8, 1/8 dotted, 1/4 triplet, 1/4, 1/4 dotted, 1/2 triplet, 1/2, 1/2 dotted, 1/1 triplet, 1/1, 1/1 dotted | 1/8 | The note value of the time, from 1/32 to a whole note, with triplets and dotted notes. |
| Feedback | 0 % to 95 % | 40 % | How much of the output goes back in. Drag the handle on the first echo up to raise it. |
| Tone | 200 Hz to 20 kHz | 6 kHz | Closes the highs of each repeat. |
| Mix | 0 % to 100 % | 30 % | The balance of the echoes and the dry signal. |
| Pitch | -24 st to 24 st | 0 st | The grain delay's transposition of each grain, in semitones. |
| Spray | 0 ms to 100 ms | 20 ms | How much later than the time a grain may read, at random: the grains scatter. |
| Jitter | 0 st to 12 st | 0 st | How far a grain's transposition may stray from the pitch, at random, in semitones. |
| Grain | 10 ms to 300 ms | 80 ms | How long each grain lasts. |

## Panel

The panel draws the echoes the state makes, one line each, fainter as they fade: a multitap's every half time. `delayTaps(state, windowMs)` returns them. With Sync on, the handle lands on the nearest note value and the Time control is drawn off. With Sync off, the note list is. The mode is picked in the title bar, and the grain delay's pitch, spray, jitter and grain size show with it alone.

```tsx
import { DelayPanel } from "@/components/audio-ui/devices/delay/panel";
import { delayDevice } from "@/components/audio-ui/devices/delay/device";

const [state, setState] = useState(() => delayDevice.defaults());

<DelayPanel onValueChange={setState} value={state} />;
```

## Engine

Create it, connect it and update it as for any device: see [The engine](/docs/devices#the-engine).

The repeats go round a low-pass filter, so each is darker than the one before. A change of time glides slowly, so the echo bends like a tape instead of clicking, and the feedback stops short of 95 % so it cannot run away. A note longer than the delay can hold (two seconds) is held to that. The `1/8d` the block starts on is a dotted eighth at the loop's 100 BPM, 450 ms.

The ping-pong, multitap and grain modes run on the audio thread: the kernels of `@audio/effect-pingpong`, `@audio/effect-multitap` and `@audio/effect-graindelay` in an `AudioWorklet` loaded from a blob, 128 frames at a time, their wet sound through a tone filter of its own. A worklet cannot import, so the device carries their code as published (`DELAY_KERNELS`, in its `kernels.ts`). A multitap's and a grain delay's lines are made as long as the longest settings need, so a multitap's time glides and a grain delay's grains follow every knob at once. A ping-pong's line is as long as its time: a new time waits for the knob to rest, then fades in on a fresh line as the old one fades out, as does a change of mode. The worklet is fed only while one of its modes plays, and until it has loaded, and in a context with no worklets, they play the echo: `ready` resolves once it is in the graph.
