# Gate

> A gate device: threshold, hysteresis, floor, attack, hold, release and look-ahead, a panel that draws its transfer curve, a Web Audio engine with a sidechain, and tools for agents.

```tsx
import { useState } from "react";
import {
  type GateState,
  gateDevice,
} from "@/components/audio-ui/devices/gate/device";
import { createGateEngine } from "@/components/audio-ui/devices/gate/engine";
import { GatePanel } from "@/components/audio-ui/devices/gate/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 loop whose tails fall under the threshold, so the gate closes between the hits. */
const START = {
  floorDb: -70,
  attackMs: 1,
  holdMs: 20,
  releaseMs: 80,
  thresholdDb: -24,
};

/**
 * A gate you can plug in: its state, a panel that draws its transfer curve
 * with a handle on the threshold and an engine that plays it, each its own
 * file. It plays a groove: raise the threshold and the tails of the hits are
 * cut shorter.
 */
export function BlockGate() {
  const [state, setState] = useState<GateState>(
    () => gateDevice.apply(gateDevice.defaults(), START).state
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createGateEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, gateDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <GatePanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

Cuts the sound between hits. Above the threshold the signal passes, and below it the signal is turned down to the floor. It is the gate of [audio.js](https://audio.js.org), which closes only under a lower threshold than the one it opens at. A key connected to the sidechain, such as a kick, opens and closes it in place of the sound: it is only listened to, never heard. 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/gate
```

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

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

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

```tsx title="components/examples/block-gate.tsx"
import { useState } from "react";
import {
  type GateState,
  gateDevice,
} from "@/components/audio-ui/devices/gate/device";
import { createGateEngine } from "@/components/audio-ui/devices/gate/engine";
import { GatePanel } from "@/components/audio-ui/devices/gate/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 loop whose tails fall under the threshold, so the gate closes between the hits. */
const START = {
  floorDb: -70,
  attackMs: 1,
  holdMs: 20,
  releaseMs: 80,
  thresholdDb: -24,
};

/**
 * A gate you can plug in: its state, a panel that draws its transfer curve
 * with a handle on the threshold and an engine that plays it, each its own
 * file. It plays a groove: raise the threshold and the tails of the hits are
 * cut shorter.
 */
export function BlockGate() {
  const [state, setState] = useState<GateState>(
    () => gateDevice.apply(gateDevice.defaults(), START).state
  );
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createGateEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, gateDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <GatePanel 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 gate 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. |
| Threshold | -70 dB to 0 dB | -36 dB | The level above which the gate opens. Drag the handle on the plot across to set it. |
| Hysteresis | 0 dB to 24 dB | 6 dB | How far under the threshold the level must fall for the gate to close. The plot draws that level as a dotted line. |
| Floor | -80 dB to 0 dB | -60 dB | How far the signal is turned down while the gate is closed. |
| Attack | 0.1 ms to 50 ms | 1 ms | How fast the gate opens. |
| Hold | 0 ms to 500 ms | 30 ms | How long the gate stays open after the level drops under the threshold. |
| Release | 5 ms to 2000 ms | 120 ms | How fast the gate closes after the hold. |
| Look-ahead | 0 ms to 10 ms | 0 ms | Delays the signal so the gate is open before a transient arrives. It adds the same [latency](/docs/devices#latency). |

## Panel

The panel draws the transfer curve against the straight line of a clean sound, with a handle on the threshold.

```tsx
import { GatePanel } from "@/components/audio-ui/devices/gate/panel";
import { gateDevice } from "@/components/audio-ui/devices/gate/device";

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

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

## Engine

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

The browser has no gate node. The engine is the processor of `@audio/dynamics-gate`'s manifest as published, in an `AudioWorklet` with two inputs, the sound and the key, with its own look-ahead. With nothing on `engine.sidechain`, it follows the sound itself. Until the worklet is ready the gate stays open. The threshold, the hysteresis, the floor, the attack, the hold and the release change as it plays. A new look-ahead builds it again: at most every 50 ms, the new one first playing the last 100 ms of sound, then taking over in a 30 ms fade. `gateSettings(state)` gives the numbers the engine puts on it.
