# Compressor

> A compressor device: five models, threshold, ratio, attack, release, knee, make-up and mix, a panel that draws its transfer curve, a Web Audio engine and tools for agents.

```tsx
import { useState } from "react";
import {
  type CompressorState,
  compressorDevice,
} from "@/components/audio-ui/devices/compressor/device";
import { createCompressorEngine } from "@/components/audio-ui/devices/compressor/engine";
import { CompressorPanel } from "@/components/audio-ui/devices/compressor/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 compressor you can plug in: its state, a panel that draws it, an engine
 * that plays it, side by side. Each is its own file and none knows the others:
 * the state is a plain object, so a host keeps its own and uses what it needs.
 * Play it from its session and turn the knobs: the dot rides the curve at the
 * level going in, and the meters at the edge show what goes in, what comes
 * off and what comes out.
 */
export function BlockCompressor() {
  const [state, setState] = useState<CompressorState>(() =>
    compressorDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createCompressorEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, compressorDevice);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <CompressorPanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

Brings loud parts down. Above the threshold, the level rises by one decibel for every `ratio` decibels in. Its model is one of five from [audio.js](https://audio.js.org), each with its own detector and timing. To duck one sound by another, use the [Ducker](/docs/components/base/ducker). 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/compressor
```

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

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

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

```tsx title="components/examples/block-compressor.tsx"
import { useState } from "react";
import {
  type CompressorState,
  compressorDevice,
} from "@/components/audio-ui/devices/compressor/device";
import { createCompressorEngine } from "@/components/audio-ui/devices/compressor/engine";
import { CompressorPanel } from "@/components/audio-ui/devices/compressor/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 compressor you can plug in: its state, a panel that draws it, an engine
 * that plays it, side by side. Each is its own file and none knows the others:
 * the state is a plain object, so a host keeps its own and uses what it needs.
 * Play it from its session and turn the knobs: the dot rides the curve at the
 * level going in, and the meters at the edge show what goes in, what comes
 * off and what comes out.
 */
export function BlockCompressor() {
  const [state, setState] = useState<CompressorState>(() =>
    compressorDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createCompressorEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, compressorDevice);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <CompressorPanel 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 compressor 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. |
| Model | Smooth, VCA, FET, Opto, Vari-Mu | Smooth | How the compressor follows the level and moves the gain. See [Models](#models). It is picked in the title bar, and the controls the model does not use are drawn off. |
| Threshold | -60 dB to 0 dB | -18 dB | The level above which the compressor works. |
| Ratio | 1:1 to 20:1 | 4:1 | How much the level is brought down above the threshold. At 4:1, 4 dB in give 1 dB out. |
| Attack | 0.1 ms to 100 ms | 5 ms | How fast the compressor takes the gain down once the level is over the threshold. |
| Release | 10 ms to 1000 ms | 80 ms | How fast the gain comes back once the level is under it. |
| Knee | 0 dB to 40 dB | 6 dB | How gradually the compressor starts to work around the threshold. 0 is a hard knee. |
| Make-up | 0 dB to 24 dB | 0 dB | Makes up for the gain taken off. |
| Mix | 0 % to 100 % | 100 % | The balance of the compressed signal and the dry one, for parallel compression. |

## Models

| Model | Package | Detector | Character |
| --- | --- | --- | --- |
| Smooth | `@audio/dynamics-compressor` | peak | The default. The gain reduction is smoothed in decibels, so the attack and the release hold at any depth of compression. |
| VCA | `@audio/dynamics-vca` | peak | Clean and precise, with a near-hard knee. |
| FET | `@audio/dynamics-fet` | peak | Very fast, made for short attacks and high ratios. |
| Opto | `@audio/dynamics-opto` | RMS | A wide soft knee, and a release that slows the longer it has been reducing. It has no Release of its own. |
| Vari-Mu | `@audio/dynamics-varimu` | RMS, of its own output | A very soft knee, and a ratio that rises as it is pushed. It has no Ratio of its own. |

Every model takes the knobs as they are set. The model runs in an `AudioWorklet`, the processor of its package's manifest as published, and Make-up goes on after it. Every value changes as it plays, smoothed by the package, so a knob turned is heard as it goes; a new model takes over in a 30 ms fade, after first playing the last 100 ms of sound, so it starts where the old one was.

## Panel

The panel draws the transfer curve the state plays: the threshold, the dashed unity line, and a dot that rides the curve at the level going in. Pass `signal`, the device's live sound from [`useDeviceSignal`](/docs/hooks/use-device-signal), to show the dot. Below the threshold the curve sits above unity by the make-up gain. The model is picked in its title bar.

```tsx
import { CompressorPanel } from "@/components/audio-ui/devices/compressor/panel";
import { compressorDevice } from "@/components/audio-ui/devices/compressor/device";

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

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

## Engine

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

```ts
engine.reduction; // dB being taken off, read per frame
```

What goes on each node comes from `compressorSettings(state)`. The models have no look-ahead, so the device adds no [latency](/docs/devices#latency). Until their worklet is ready, the signal passes unchanged. `engine.reduction` reads the gain the model takes off, measured from its output against its input about every 10 ms.

## How close the curve is

The curve is the static curve: the level once the gain has settled on a steady level. It is measured from the model itself, its processor run on a steady level until it settles (`modelCurve`), so the vari-mu, which follows its own output, draws what it plays. Below the threshold it is exact.

A real detector follows a signal with an attack and a release, so what you hear sits above the curve, by how much depending on the detector. Measured on a sine with its peak 12 dB over the threshold, at the defaults:

| Model | Above the curve | Why |
| --- | --- | --- |
| Smooth | under 0.1 dB | Its gain reduction is smoothed in decibels, after the curve |
| Opto | about 2.3 dB | It follows the RMS level, 3 dB under a sine's peak |
| Vari-Mu | about 1.5 dB | It follows the RMS level of its own output |
| VCA, FET | 4.4 dB at 100 Hz to 5.6 dB at 1 kHz | Their detectors follow the waveform itself, so the gain comes back between its peaks |
