audio/ui
Toggle Mode D
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/polyphony
Copy to Clipboardimport { createPolyphony } from "@/lib/polyphony";
| Files | lib/polyphony.ts |
|---|---|
| Depends on | Nothing |
| npm | Nothing |
| Used by | use-polyphony, voice-pool |
| Export | Kind | What it is |
|---|---|---|
VoiceEngine | interface | What a voice plays a note on: the engine of an instrument, which holds one note at a time. |
Polyphony | interface | Notes played as chords on engines that hold one note each, every note by an id its note off gives again. |
createPolyphony | function | Plays 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.
Copy to Clipboardimport { 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.