# String machine

> A string machine device: a violin, a viola, a cello and a bass register, a brightness filter, a slow bow and an ensemble, with eight 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 { StringMachinePanel } from "@/components/audio-ui/devices/string-machine/panel";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useMidiInput } from "@/hooks/use-midi-input";
import {
  type StringMachineNote,
  type StringMachineState,
  stringMachineDevice,
} from "@/components/audio-ui/devices/string-machine/device";
import {
  createStringMachineEngine,
  type StringMachineEngine,
} from "@/components/audio-ui/devices/string-machine/engine";
import { webAudio } from "@/lib/web-audio";

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

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

/**
 * A string machine 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; eight of them play chords. Play the keyboard, or
 * connect a MIDI keyboard and play chords on it.
 */
export function BlockStringMachine() {
  const [state, setState] = useState<StringMachineState>(() =>
    stringMachineDevice.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 = createStringMachineEngine(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: StringMachineNote, 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: StringMachineNote) => 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">
      <StringMachinePanel
        activeNotes={midi.activeNotes}
        onNoteOff={releaseKey}
        onNoteOn={playKey}
        onValueChange={setState}
        value={state}
      />
    </div>
  );
}
```

A string machine. Every key sounds four registers, a violin and a viola at its pitch, a cello an octave down and a bass two, each at its own level. The sum bows in slowly and lets go slowly, through a brightness filter, then an ensemble: three delay lines swayed by three slow oscillators a third of a turn apart, which thicken the sound as a section of players does. Start from one of its eight presets (Strings, Violins, Cellos, Full, Swell, Pizz, Dark, Choir), 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/string-machine
```

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

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

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

```tsx title="components/examples/block-string-machine.tsx"
"use client";

import { noteToFrequency } from "@audio-ui/core/pitch";
import { useCallback, useEffect, useRef, useState } from "react";
import { StringMachinePanel } from "@/components/audio-ui/devices/string-machine/panel";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useMidiInput } from "@/hooks/use-midi-input";
import {
  type StringMachineNote,
  type StringMachineState,
  stringMachineDevice,
} from "@/components/audio-ui/devices/string-machine/device";
import {
  createStringMachineEngine,
  type StringMachineEngine,
} from "@/components/audio-ui/devices/string-machine/engine";
import { webAudio } from "@/lib/web-audio";

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

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

/**
 * A string machine 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; eight of them play chords. Play the keyboard, or
 * connect a MIDI keyboard and play chords on it.
 */
export function BlockStringMachine() {
  const [state, setState] = useState<StringMachineState>(() =>
    stringMachineDevice.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 = createStringMachineEngine(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: StringMachineNote, 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: StringMachineNote) => 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">
      <StringMachinePanel
        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 string machine 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 sliders do, and shows the value it sets in the unit the sound takes.

| Control | Range | Default | What it does |
| --- | --- | --- | --- |
| Violin | 0 to 10 | 8 | The level of the violin: two saws at the key's pitch, a little apart. |
| Viola | 0 to 10 | 4 | The level of the viola, a saw at the key's pitch. |
| Cello | 0 to 10 | 5 | The level of the cello, an octave down. |
| Bass | 0 to 10 | 0 | The level of the bass, a triangle two octaves down. |
| Brightness | 700 Hz to 9.8 kHz | 3.4 kHz | Where the low-pass filter closes, from 700 Hz to 9.8 kHz. |
| Ensemble | 0 to 10 | 6 | How much the ensemble thickens the sound, and how far it sways. |
| Attack | 10 ms to 1.81 s | 298 ms | How long a note takes to bow in. |
| Release | 80 ms to 2.48 s | 680 ms | How long a note takes to fade once let go. |
| 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 other controls out by what they do (registers, tone, bow, output) and has a keyboard of two octaves that plays the machine. `stringMachinePreset(name)` gives a preset's whole state, and `STRING_MACHINE_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 { StringMachinePanel } from "@/components/audio-ui/devices/string-machine/panel";
import { stringMachineDevice } from "@/components/audio-ui/devices/string-machine/device";

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

<StringMachinePanel 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.8 }); // bows a note in
engine.noteOff(); // lets it go, over the release

// 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, through its own ensemble. 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 eight. A note bows in over its attack and lets go over the release from where it had got to, so a short note never clicks; a harder key leans on the level a little, as a bow does. A note while one is held cuts it and bows anew, and a `noteOff` whose `id` is not the held note's lets nothing go. A register's level and the brightness move a held note. An agent plays the machine with the `play` tool.
