Lab

polyphony

Chords on an instrument whose engine plays one voice: a note takes the voice that holds it, else a free one, else the one held longest, whose note is let go first. No sound and no engine of its own: it plays the engines it is given.

pnpm dlx shadcn@latest add @audio-ui/polyphony
import { createPolyphony } from "@/lib/polyphony";
Fileslib/polyphony.ts
Depends onNothing
npm@audio-ui/engine
Used byuse-polyphony, voice-pool
ExportKindWhat it is
VoiceEngineinterfaceWhat a voice plays a note on: the engine of an instrument, which holds one note at a time.
PolyphonyinterfaceNotes played as chords on engines that hold one note each, every note by an id its note off gives again.
createPolyphonyfunctionPlays chords on engines, one note each: what turns an instrument whose engine plays one voice into a polyphonic one.

Usage

Plays chords on engines that hold one note each. A note takes the voice that holds it already, else a free one, else the one held longest, whose note is let go first. It makes no sound and no engine: it takes engines you made and connected, so it schedules ahead on the audio clock as well as it plays live.

import { createPolyphony } from "@/lib/polyphony";

const voices = createPolyphony(engines);
voices.noteOn({ frequency: 440, velocity: 0.8 }, "a4", time);
voices.noteOff("a4", time + 0.5);
voices.release(stopTime); // every note held, as a stop does

Notes are ordered by when they came, not by a clock. On a page, usePolyphony makes the engines and gives you noteOn and noteOff; for a device of the library, voice-pool plays on it.

Built by Ouest Labs, the source code is available on GitHub.
RSS