Lab

useSynthVoices

Polyphonic synth voices on the Web Audio API: start and release notes with an envelope, a velocity and a pitch bend.

pnpm dlx shadcn@latest add @audio-ui/use-synth-voices
import { useSynthVoices } from "@/hooks/use-synth-voices";
Fileshooks/use-synth-voices.ts
Depends onenvelope-ramp
npm@audio-ui/engine
Used byblock-piano-roll, piano-roll-playback-demo, spectrum-analyzer-demo, use-piano-roll-playback
ExportKindWhat it is
UseSynthVoicesOptionsinterface
UseSynthVoicesReturninterface
useSynthVoicesfunctionA polyphonic pool of oscillator+gain voices, one per held note, each shaped by an Envelope's points: attack/decay plays up to SUSTAIN_SPLIT_X and holds there while the note is down, release plays the rest on note-off.

Usage

useSynthVoices plays notes on a pool of voices, one per held pitch: an oscillator through a gain shaped by an envelope. The attack and the decay play when the note starts and hold at the sustain level; the release plays when it ends.

import { useMasterBus } from "@/hooks/use-master-bus";
import { useSynthVoices } from "@/hooks/use-synth-voices";

const output = useMasterBus(0.7);
const voices = useSynthVoices({ destination: output, oscillatorType: "triangle" });

<Piano
  onNoteOff={(pitch) => voices.releaseNote(pitch, envelope)}
  onNoteOn={(pitch) => voices.triggerNote(pitch, envelope)}
/>;

envelope is the points of an Envelope, { x, y } from 0 to 1, so the shape a user draws is the shape the note plays: the points before the middle (x 0.5) are the attack and the decay, the points after it the release. Give time to triggerNote and releaseNote to schedule a note ahead, as a transport clock does; leave it out to play now.

Options · 5

Returns · 4

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