# Electric piano

> A tine electric piano device: a body that barks, a bell that pings, a pickup that can be driven and a tremolo, with four presets, a panel with a keyboard, a Web Audio engine and tools for agents.

```tsx
"use client";

import { noteToFrequency } from "@audio-ui/core/pitch";
import { useCallback, useEffect, useRef, useState } from "react";
import { ElectricPianoPanel } from "@/components/audio-ui/devices/electric-piano/panel";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useMidiInput } from "@/hooks/use-midi-input";
import {
  type ElectricPianoNote,
  type ElectricPianoState,
  electricPianoDevice,
} from "@/components/audio-ui/devices/electric-piano/device";
import {
  createElectricPianoEngine,
  type ElectricPianoEngine,
} from "@/components/audio-ui/devices/electric-piano/engine";
import { webAudio } from "@/lib/web-audio";

/** How many notes ring at once: an eleventh takes the voice held longest. */
const VOICES = 10;
/** The note the panel's keyboard holds: one at a time, under its own name. */
const PANEL_NOTE = "panel";

interface Voice {
  engine: ElectricPianoEngine;
  holding: string | null;
  since: number;
}

/**
 * A tine electric piano you can plug in: its state, a panel that draws and
 * plays it, and a Web Audio engine that makes the sound, each its own file.
 * The engine plays one voice; ten of them play chords. Play the keyboard, or
 * connect a MIDI keyboard and play chords on it: a hard key barks and pings.
 */
export function BlockElectricPiano() {
  const [state, setState] = useState<ElectricPianoState>(() =>
    electricPianoDevice.defaults()
  );
  const master = useMasterBus(1);
  const [voices, setVoices] = useState<Voice[] | null>(null);
  const stateRef = useRef(state);
  stateRef.current = state;

  useEffect(() => {
    const ctx = webAudio.getContext();
    if (!(ctx && master)) {
      return;
    }
    const made = Array.from({ length: VOICES }, () => {
      const engine = createElectricPianoEngine(ctx, stateRef.current);
      engine.output.connect(master);
      return { engine, holding: null, since: 0 };
    });
    setVoices(made);
    return () => {
      for (const voice of made) {
        voice.engine.dispose();
      }
      setVoices(null);
    };
  }, [master]);

  // Every voice follows the state, whoever changed it: a control or a call.
  useEffect(() => {
    for (const voice of voices ?? []) {
      voice.engine.update(state);
    }
  }, [state, voices]);

  /** A note on the voice that holds it, else a free one, else the one held longest. */
  const noteOn = useCallback(
    (note: ElectricPianoNote, id: string) => {
      if (!voices) {
        return;
      }
      const voice =
        voices.find((item) => item.holding === id) ??
        voices.find((item) => item.holding === null) ??
        voices.reduce((a, b) => (a.since <= b.since ? a : b));
      if (voice.holding !== null && voice.holding !== id) {
        voice.engine.noteOff();
      }
      voice.holding = id;
      voice.since = performance.now();
      voice.engine.noteOn(note, { id });
    },
    [voices]
  );
  const noteOff = useCallback(
    (id: string) => {
      const voice = voices?.find((item) => item.holding === id);
      if (voice) {
        voice.holding = null;
        voice.engine.noteOff({ id });
      }
    },
    [voices]
  );

  const playKey = useCallback(
    (note: ElectricPianoNote) => noteOn(note, PANEL_NOTE),
    [noteOn]
  );
  const releaseKey = useCallback(() => noteOff(PANEL_NOTE), [noteOff]);
  const midi = useMidiInput({
    onNoteOff: (pitch: number) => noteOff(`midi-${pitch}`),
    onNoteOn: (pitch: number, velocity: number) =>
      noteOn({ frequency: noteToFrequency(pitch), velocity }, `midi-${pitch}`),
  });

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <ElectricPianoPanel
        activeNotes={midi.activeNotes}
        onNoteOff={releaseKey}
        onNoteOn={playKey}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

A tine electric piano. A key strikes a tine and a pickup hears it ring: a body on the same pitch, which barks when the key is struck hard, and a bell fourteen times up, which pings and is gone in a tenth of a second. The note rings down by itself, longer in the bass, through a pickup that can be driven and a tremolo. Start from one of its four presets (Warm, Bell, Bark, Mellow), then shape it. 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/electric-piano
```

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

**Manual**

**1. Install the packages:**

```bash
npm install @audio-ui/react @audio-ui/core
```

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

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

```tsx title="components/examples/block-electric-piano.tsx"
"use client";

import { noteToFrequency } from "@audio-ui/core/pitch";
import { useCallback, useEffect, useRef, useState } from "react";
import { ElectricPianoPanel } from "@/components/audio-ui/devices/electric-piano/panel";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useMidiInput } from "@/hooks/use-midi-input";
import {
  type ElectricPianoNote,
  type ElectricPianoState,
  electricPianoDevice,
} from "@/components/audio-ui/devices/electric-piano/device";
import {
  createElectricPianoEngine,
  type ElectricPianoEngine,
} from "@/components/audio-ui/devices/electric-piano/engine";
import { webAudio } from "@/lib/web-audio";

/** How many notes ring at once: an eleventh takes the voice held longest. */
const VOICES = 10;
/** The note the panel's keyboard holds: one at a time, under its own name. */
const PANEL_NOTE = "panel";

interface Voice {
  engine: ElectricPianoEngine;
  holding: string | null;
  since: number;
}

/**
 * A tine electric piano you can plug in: its state, a panel that draws and
 * plays it, and a Web Audio engine that makes the sound, each its own file.
 * The engine plays one voice; ten of them play chords. Play the keyboard, or
 * connect a MIDI keyboard and play chords on it: a hard key barks and pings.
 */
export function BlockElectricPiano() {
  const [state, setState] = useState<ElectricPianoState>(() =>
    electricPianoDevice.defaults()
  );
  const master = useMasterBus(1);
  const [voices, setVoices] = useState<Voice[] | null>(null);
  const stateRef = useRef(state);
  stateRef.current = state;

  useEffect(() => {
    const ctx = webAudio.getContext();
    if (!(ctx && master)) {
      return;
    }
    const made = Array.from({ length: VOICES }, () => {
      const engine = createElectricPianoEngine(ctx, stateRef.current);
      engine.output.connect(master);
      return { engine, holding: null, since: 0 };
    });
    setVoices(made);
    return () => {
      for (const voice of made) {
        voice.engine.dispose();
      }
      setVoices(null);
    };
  }, [master]);

  // Every voice follows the state, whoever changed it: a control or a call.
  useEffect(() => {
    for (const voice of voices ?? []) {
      voice.engine.update(state);
    }
  }, [state, voices]);

  /** A note on the voice that holds it, else a free one, else the one held longest. */
  const noteOn = useCallback(
    (note: ElectricPianoNote, id: string) => {
      if (!voices) {
        return;
      }
      const voice =
        voices.find((item) => item.holding === id) ??
        voices.find((item) => item.holding === null) ??
        voices.reduce((a, b) => (a.since <= b.since ? a : b));
      if (voice.holding !== null && voice.holding !== id) {
        voice.engine.noteOff();
      }
      voice.holding = id;
      voice.since = performance.now();
      voice.engine.noteOn(note, { id });
    },
    [voices]
  );
  const noteOff = useCallback(
    (id: string) => {
      const voice = voices?.find((item) => item.holding === id);
      if (voice) {
        voice.holding = null;
        voice.engine.noteOff({ id });
      }
    },
    [voices]
  );

  const playKey = useCallback(
    (note: ElectricPianoNote) => noteOn(note, PANEL_NOTE),
    [noteOn]
  );
  const releaseKey = useCallback(() => noteOff(PANEL_NOTE), [noteOff]);
  const midi = useMidiInput({
    onNoteOff: (pitch: number) => noteOff(`midi-${pitch}`),
    onNoteOn: (pitch: number, velocity: number) =>
      noteOn({ frequency: noteToFrequency(pitch), velocity }, `midi-${pitch}`),
  });

  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <ElectricPianoPanel
        activeNotes={midi.activeNotes}
        onNoteOff={releaseKey}
        onNoteOn={playKey}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

**Agent**

Give your agent the skill, and it picks the component, installs it and wires it to your audio:

```bash
npx skills add ouestlabs/audio-ui
```

Then ask it:

```text
Add the electric piano from audio/ui to my project and wire it to my audio code.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Controls

Every control reads 0 to 10, as a panel's knobs do, and shows the value it sets in the unit the sound takes.

| Control | Range | Default | What it does |
| --- | --- | --- | --- |
| Bark | 0 to 10 | 4 | How much the body growls when a key is struck hard. |
| Bell | 0 to 10 | 5 | How loud the hammer's ping on the tine. |
| Decay | 0.8 s to 5.8 s | 3.3 s | How long a note rings at middle C: the bass rings longer, the treble shorter. |
| Drive | ×1 to ×6 | ×2.5 | How hard the pickup and the preamp are driven. |
| Tremolo rate | 1.5 Hz to 9 Hz | 3.1 Hz | The tremolo's speed. |
| Tremolo depth | 0% to 90% | 27% | How far the tremolo swings the level. |
| Volume | -∞ dB to -3 dB | -14.1 dB | The level of the output. |

## Panel

The panel loads a preset from the menu in its title bar, lays the controls out by what they do (tone, pickup, tremolo, output) and has a keyboard of two octaves that plays the piano. `electricPianoPreset(name)` gives a preset's whole state, and `ELECTRIC_PIANO_PRESETS` lists them. With `activeNotes`, the notes held now, such as on a MIDI keyboard, light up their keys. The panel sends notes out and does not make the sound, so it plays through any engine.

```tsx
import { ElectricPianoPanel } from "@/components/audio-ui/devices/electric-piano/panel";
import { electricPianoDevice } from "@/components/audio-ui/devices/electric-piano/device";

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

<ElectricPianoPanel 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({ frequency: 261.63, velocity: 0.9 }); // strikes a key
engine.noteOff(); // lets it go: it falls silent in a fifth of a second

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

An engine plays one voice. For chords, make as many as the notes you hold and give each note its own: the [arrangement](/docs/components/base/arrangement)'s engine does it for a MIDI track, and the block above keeps ten. A note rises in three milliseconds and rings down by itself, even held; its bark and its bell are as bright as the key is hard. Let go, it falls silent from where it got to. A key struck again while its note rings cuts it and strikes anew. A note already ringing keeps the timbre it was struck with, while the drive, the tremolo and the volume move under it. An agent plays the piano with the `play` tool.
