# Wave shaper

> A wave shaper synth device: a waveform, a drive, a tone, a detune, a mix 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 WaveShaperNote,
  type WaveShaperState,
  waveShaperDevice,
} from "@/components/audio-ui/devices/wave-shaper/device";
import { createWaveShaperEngine } from "@/components/audio-ui/devices/wave-shaper/engine";
import { WaveShaperPanel } from "@/components/audio-ui/devices/wave-shaper/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A wave shaper 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 resonance up.
 */
export function BlockWaveShaper() {
  const [state, setState] = useState<WaveShaperState>(() =>
    waveShaperDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createWaveShaperEngine,
    destination: master,
    state,
  });

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

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

Two detuned oscillators, a distortion mixed in with the clean sound, and a low-pass filter. Press the pad: the pitch goes across and the resonance 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/wave-shaper
```

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

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

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

```tsx title="components/examples/block-wave-shaper.tsx"
import { useCallback, useState } from "react";
import {
  type WaveShaperNote,
  type WaveShaperState,
  waveShaperDevice,
} from "@/components/audio-ui/devices/wave-shaper/device";
import { createWaveShaperEngine } from "@/components/audio-ui/devices/wave-shaper/engine";
import { WaveShaperPanel } from "@/components/audio-ui/devices/wave-shaper/panel";
import { useDeviceEngines } from "@/hooks/use-device-engines";
import { useMasterBus } from "@/hooks/use-master-bus";

/**
 * A wave shaper 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 resonance up.
 */
export function BlockWaveShaper() {
  const [state, setState] = useState<WaveShaperState>(() =>
    waveShaperDevice.defaults()
  );
  const master = useMasterBus(1);
  const [engine] = useDeviceEngines({
    create: createWaveShaperEngine,
    destination: master,
    state,
  });

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

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <WaveShaperPanel
        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 wave shaper 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 | Sine | The wave of the oscillators. |
| Drive | 0 to 100 | 1 | How much of the distortion is pushed into the sound. |
| Tone | 0 to 100 | 50 | The cutoff of the low-pass filter. |
| Detune | -50 ct to 50 ct | 0 ct | How far apart the two oscillators are tuned. |
| Mix | 0 % to 100 % | 50 % | The balance of the distorted signal and the clean one. |
| Volume | 0% to 100% | 50% | The level of the output. |

## Panel

The panel has a pad that plays the synth: the pitch goes across and the resonance goes up. `waveShaperPitch(position)` and `waveShaperResonance(position)` turn a position from 0 to 1 into a note. 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 `waveShaperPosition(frequency)` is where a pitch sits.

```tsx
import { WaveShaperPanel } from "@/components/audio-ui/devices/wave-shaper/panel";
import { waveShaperDevice } from "@/components/audio-ui/devices/wave-shaper/device";

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

<WaveShaperPanel 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, as the [pocket synth](/docs/components/base/pocket-synth). The distortion curve is a table of 44,100 points, rebuilt only when the drive changes. The shaper oversamples 4x, which delays its path by 4 ms, and the clean path waits as long so the mix does not comb.
