# Distortion

> A distortion device: four curves, a drive, a tone, a mix and an output, a panel that draws the transfer curve, a Web Audio engine and tools for agents.

```tsx
import { useState } from "react";
import {
  type DistortionState,
  distortionDevice,
} from "@/components/audio-ui/devices/distortion/device";
import { createDistortionEngine } from "@/components/audio-ui/devices/distortion/engine";
import { DistortionPanel } from "@/components/audio-ui/devices/distortion/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 distortion you can plug in: its state, a panel that draws its transfer
 * curve and an engine that plays it, each its own file and none knowing the
 * others. Play the loop and turn the drive: the curve bends as the sound does.
 */
export function BlockDistortion() {
  const [state, setState] = useState<DistortionState>(() =>
    distortionDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createDistortionEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, distortionDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <DistortionPanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

Bends the sound into one of four curves. The more the drive, the more the peaks round off, or fold back. 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/distortion
```

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

**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/distortion-device @audio-ui/distortion-panel @audio-ui/distortion-engine
```

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

```tsx title="components/examples/block-distortion.tsx"
import { useState } from "react";
import {
  type DistortionState,
  distortionDevice,
} from "@/components/audio-ui/devices/distortion/device";
import { createDistortionEngine } from "@/components/audio-ui/devices/distortion/engine";
import { DistortionPanel } from "@/components/audio-ui/devices/distortion/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 distortion you can plug in: its state, a panel that draws its transfer
 * curve and an engine that plays it, each its own file and none knowing the
 * others. Play the loop and turn the drive: the curve bends as the sound does.
 */
export function BlockDistortion() {
  const [state, setState] = useState<DistortionState>(() =>
    distortionDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createDistortionEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, distortionDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <DistortionPanel onValueChange={setState} signal={signal} 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 distortion 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. |
| Curve | Tanh, Cubic, Hard, Fold | Tanh | The shape the drive pushes the signal into, picked with a row of buttons, each drawn from its curve. Tanh, the default, rounds the peaks off gently. Cubic rounds them off sooner and holds them flat at full scale; a quiet input comes out half as loud again. Hard holds the peaks flat at full scale, with a corner. Fold reflects whatever passes full scale back inside it, again and again as the drive grows. |
| Drive | 0 dB to 40 dB | 12 dB | How hard the signal is pushed into the curve. |
| Tone | 500 Hz to 20 kHz | 6 kHz | Closes the highs after the curve. |
| Mix | 0 % to 100 % | 100 % | The balance of the distorted signal and the clean one. |
| Output | -24 dB to 12 dB | -6 dB | The level of the output. |

## Panel

The panel draws the transfer curve, the output for each input, against the straight line of a clean sound, and picks the curve with buttons drawn from each. `distortionShape(curve, input)` is a curve at an input after the drive, and `distortionCurve(state)` is the whole curve with the mix and the output.

```tsx
import { DistortionPanel } from "@/components/audio-ui/devices/distortion/panel";
import { distortionDevice } from "@/components/audio-ui/devices/distortion/device";

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

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

## Engine

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

The drive is a gain before the table of the curve, so turning it never changes the table; only picking another curve does. The tables are written by the curves of `@audio/effect-distortion` (`distortionTable(curve)`) over the whole range of the drive, so a fold keeps folding at full drive. The shaper oversamples 4x to keep the curve from aliasing, which is the [latency](/docs/devices#latency) of the device.
