pnpm dlx shadcn@latest add @audio-ui/use-polyphony
import { usePolyphony } from "@/hooks/use-polyphony";| Files | hooks/use-polyphony.ts |
|---|---|
| Depends on | polyphony, use-device-engines |
| npm | @audio-ui/engine |
| Used by | block-electric-piano, block-fm-synth, block-guitar, block-organ, block-poly-synth, block-sampler, block-string-machine, block-tape-keys |
| Export | Kind | What it is |
|---|---|---|
PolyphonyEngine | type | What a voice needs of an instrument's engine: it plays one note at a time. |
UsePolyphonyOptions | interface | |
UsePolyphonyReturn | interface | |
usePolyphony | function | An instrument whose engine plays one voice, played as chords: voices engines made into destination (useDeviceEngines), each note on a voice of its own (createPolyphony). |
Usage
The engine of an instrument device plays one voice. usePolyphony plays chords on it: it makes as many engines as notes sound at once into a destination, keeps every one following the state, and gives each note a voice of its own: the one that holds it, else a free one, else the one held longest, whose note is let go first. The engines are disposed when the destination changes and on unmount.
import { createPolySynthEngine } from "@/components/audio-ui/devices/poly-synth/engine";
import { PolySynthPanel } from "@/components/audio-ui/devices/poly-synth/panel";
import { useMasterBus } from "@/hooks/use-master-bus";
import { usePolyphony } from "@/hooks/use-polyphony";
const output = useMasterBus(1);
const { noteOff, noteOn } = usePolyphony({
create: createPolySynthEngine,
destination: output,
state,
});
<PolySynthPanel
onNoteOff={() => noteOff("panel")}
onNoteOn={(note) => noteOn(note, "panel")}
onValueChange={setState}
value={state}
/>;A note is named by an id its note off gives again, such as midi-60 for a key of a MIDI keyboard. To schedule notes ahead on the audio clock, or outside React, createPolyphony(engines) from @/lib/polyphony (installed with the hook) does the same with engines you made and connected: noteOn(note, id, at?), noteOff(id, at?) and release(at?). The arrangement's engine plays a MIDI track's instrument this way.
Options · 4
createPolySynthEngine.Returns · 3
id now, on a voice of its own. note is the engine's own, such as { frequency, velocity }.id. A note no voice holds lets nothing go.