# Filter

> A filter device: a type, six circuits, a cutoff, a resonance and a drive, a panel with a handle on the curve and a live spectrum, a Web Audio engine and tools for agents.

```tsx
import { useState } from "react";
import {
  type FilterState,
  filterDevice,
} from "@/components/audio-ui/devices/filter/device";
import { createFilterEngine } from "@/components/audio-ui/devices/filter/engine";
import { FilterPanel } from "@/components/audio-ui/devices/filter/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 filter you can plug in: its state, a panel that draws the curve it plays
 * with a handle on the cutoff 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 cutoff, up for the resonance; the spectrum behind is what comes out.
 */
export function BlockFilter() {
  const [state, setState] = useState<FilterState>(() =>
    filterDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createFilterEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, filterDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <FilterPanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

Takes part of the spectrum out: a low-pass, a high-pass, a band-pass or a notch, through a circuit modeled on an analog one. 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/filter
```

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

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

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

```tsx title="components/examples/block-filter.tsx"
import { useState } from "react";
import {
  type FilterState,
  filterDevice,
} from "@/components/audio-ui/devices/filter/device";
import { createFilterEngine } from "@/components/audio-ui/devices/filter/engine";
import { FilterPanel } from "@/components/audio-ui/devices/filter/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 filter you can plug in: its state, a panel that draws the curve it plays
 * with a handle on the cutoff 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 cutoff, up for the resonance; the spectrum behind is what comes out.
 */
export function BlockFilter() {
  const [state, setState] = useState<FilterState>(() =>
    filterDevice.defaults()
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createFilterEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, filterDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <FilterPanel 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 filter 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. |
| Type | Low-pass, High-pass, Band-pass, Notch | Low-pass | The shape of the filter. |
| Circuit | Ladder 24, Ladder 12, Diode, Korg35, SEM | SEM | What the filter is made of, picked in the title bar. Ladder 24 is a Moog transistor ladder, four poles, 24 dB an octave, and Ladder 12 the same ladder at two poles, both with a low-pass, a high-pass and a band-pass. Diode is a diode ladder, the four poles of a TB-303, low-pass only. Korg35 is two poles with the saturation of the MS-20's feedback, low-pass and high-pass, with no resonant peak. SEM is the Oberheim SEM's state-variable filter, two poles, every type, its saturation in its feedback: the default. A circuit without the type plays the SEM, and comes back when the type does. |
| Cutoff | 20 Hz to 20 kHz | 1.2 kHz | The frequency where the filter starts to work. Drag the handle across to set it. |
| Resonance | 0.30 to 18.00 | 0.71 | How much the filter rings around the cutoff. Drag the handle up to raise it. The resonance is a Q: a ladder rings as high over its passband as a 2-pole filter at that Q, and the Moog and diode ladders start to sing on their own at the top. The SEM is that 2-pole filter at the same Q until it saturates. Every circuit plays at unity in its passband: a low-pass keeps its passband at 0 dB, where a ladder alone would lose its lows as the resonance rises, and a band-pass peaks at 0 dB at its cutoff. The Korg35 has no peak: its resonance saturates and darkens instead. |
| Drive | -20 dB to +20 dB | 0 dB | How hard the sound goes into a ladder's saturation, and the Korg35's. At 0 dB it plays as its circuit does; more drive bends and thickens a loud sound without raising its peak, and brings a quiet one up into the saturation. The SEM's saturation is its own. |

## Panel

The panel draws the curve of the filter, with a handle on it, and picks the circuit in its title bar, those without the type greyed out. Pass `signal`, the device's live sound from [`useDeviceSignal`](/docs/hooks/use-device-signal), to see the spectrum of what comes out behind the curve. `filterCurve(state)` is the curve it draws: an analog circuit's is measured on it, its response to a click too quiet to saturate, read at each frequency the plot draws.

```tsx
import { FilterPanel } from "@/components/audio-ui/devices/filter/panel";
import { filterDevice } from "@/components/audio-ui/devices/filter/device";

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

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

## Engine

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

A circuit runs on the audio thread: the kernels of `@audio/filter-moog-ladder`, `@audio/filter-diode-ladder`, `@audio/filter-korg35` and `@audio/filter-oberheim` in an `AudioWorklet` loaded from a blob, 128 frames at a time, about a percent of a core at most. A worklet cannot import, so the device carries their code as published (`FILTER_KERNELS`, in its `kernels.ts`), at the versions the registry was built with; the curve reads the packages as installed. A change of circuit or type is a message, the new one starting from rest. Until it has loaded, and in a context with no worklets, the signal passes unchanged: `ready` resolves once it is in the graph. `circuitSettings(state)` is what goes on it, and `circuitMakeup` how its level is brought back.
