# Pocket synth

> A synth device: a waveform, a filter and a volume, a panel with a pad that plays it, a Web Audio engine with noteOn and noteOff, and tools for agents.

```tsx
import { useCallback, useState } from "react";
import {
  type PocketSynthNote,
  type PocketSynthState,
  pocketSynthDevice,
} from "@/components/audio-ui/devices/pocket-synth/device";
import { createPocketSynthEngine } from "@/components/audio-ui/devices/pocket-synth/engine";
import { PocketSynthPanel } from "@/components/audio-ui/devices/pocket-synth/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A synth you can plug in: its state, a panel that draws and plays it and an
 * engine that makes the sound, each its own file and none knowing the others.
 * Press the pad and move: pitch goes across, the filter cutoff up.
 */
export function BlockPocketSynth() {
  const [state, setState] = useState<PocketSynthState>(() =>
    pocketSynthDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createPocketSynthEngine,
    destination: master,
    state,
  });

  const play = useCallback(
    (note: PocketSynthNote) => engine?.noteOn(note),
    [engine]
  );
  const release = useCallback(() => engine?.noteOff(), [engine]);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <PocketSynthPanel
        onNoteOff={release}
        onNoteOn={play}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

One oscillator through a resonant filter. Press the pad: the pitch goes across and the filter cutoff goes up. 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/pocket-synth
```

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

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

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

```tsx title="components/examples/block-pocket-synth.tsx"
import { useCallback, useState } from "react";
import {
  type PocketSynthNote,
  type PocketSynthState,
  pocketSynthDevice,
} from "@/components/audio-ui/devices/pocket-synth/device";
import { createPocketSynthEngine } from "@/components/audio-ui/devices/pocket-synth/engine";
import { PocketSynthPanel } from "@/components/audio-ui/devices/pocket-synth/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A synth you can plug in: its state, a panel that draws and plays it and an
 * engine that makes the sound, each its own file and none knowing the others.
 * Press the pad and move: pitch goes across, the filter cutoff up.
 */
export function BlockPocketSynth() {
  const [state, setState] = useState<PocketSynthState>(() =>
    pocketSynthDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createPocketSynthEngine,
    destination: master,
    state,
  });

  const play = useCallback(
    (note: PocketSynthNote) => engine?.noteOn(note),
    [engine]
  );
  const release = useCallback(() => engine?.noteOff(), [engine]);

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <PocketSynthPanel
        onNoteOff={release}
        onNoteOn={play}
        onValueChange={setState}
        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 pocket synth 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 |
| --- | --- | --- | --- |
| Wave | Sine, Triangle, Saw, Square | Square | The wave of the oscillator. |
| Filter | All pass, Low pass, High pass, Band pass | All pass | The type of the filter. |
| Volume | 0% to 100% | 50% | The level of the output. |

## Panel

The panel has a pad that plays the synth: its position gives the note, so the pitch goes across and the cutoff goes up. `pocketSynthPitch(position)` and `pocketSynthCutoff(position)` turn a position from 0 to 1 into hertz. With `activeNotes`, the notes held now, such as on a MIDI keyboard, take the pad's point to the pitch of the last one, and `pocketSynthPosition(frequency)` is where a pitch sits. The panel sends notes out and does not make the sound, so it plays through any engine.

```tsx
import { PocketSynthPanel } from "@/components/audio-ui/devices/pocket-synth/panel";
import { pocketSynthDevice } from "@/components/audio-ui/devices/pocket-synth/device";

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

<PocketSynthPanel 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.noteOn(note); // starts a note now, or glides the one being held
engine.noteOff(); // lets it go

// Scheduled ahead on the audio clock, by a host that plays notes from a song:
engine.noteOn({ ...note, velocity: 0.8 }, { at: time, id: "c4" });
engine.noteOff({ at: time + length, id: "c4" });
```

One voice at a time. `noteOn` starts a note, or glides the one being held to the new one, and `noteOff` releases it. Both take an optional `{ at, id }`: `at` is a time of the audio clock, now when there is none, and a `noteOff` whose `id` is not the held note's lets nothing go, since the held note took over from it. A note plays as loud as its `velocity`, from 0 to 1. The wave and the filter type of a state change under a held note. An agent plays the synth with the `play` tool.
