Audio UI
One oscillator through a resonant filter. Press the pad: the pitch goes across and the filter cutoff goes up. It is a device: a state, a panel, an engine and tools, each its own file.
Installation
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/pocket-synth
It installs the device's state, its engine and its panel to components/audio-ui/devices/pocket-synth/. The demo above, which wires them to a loop, is @audio-ui/block-pocket-synth.
Controls
Controls · 3
Panel
The panel has a pad that plays the synth: its position gives the note, so the pitch goes across and the cutoff goes up. pocketSynthPitch(position) and pocketSynthCutoff(position) turn a position from 0 to 1 into hertz. With activeNotes, the notes held now, such as on a MIDI keyboard, take the pad's point to the pitch of the last one, and pocketSynthPosition(frequency) is where a pitch sits. The panel sends notes out and does not make the sound, so it plays through any engine.
Copy to Clipboardimport { PocketSynthPanel } from "@/components/audio-ui/devices/pocket-synth/panel"; import { pocketSynthDevice } from "@/components/audio-ui/devices/pocket-synth/device"; const [state, setState] = useState(() => pocketSynthDevice.defaults()); <PocketSynthPanel onValueChange={setState} value={state} />;
Engine
Create it, connect it and update it as for any device: see The engine.
Copy to Clipboardengine.noteOn(note); // starts a note now, or glides the one being held engine.noteOff(); // lets it go // Scheduled ahead on the audio clock, by a host that plays notes from a song: engine.noteOn({ ...note, velocity: 0.8 }, { at: time, id: "c4" }); engine.noteOff({ at: time + length, id: "c4" });
One voice at a time. noteOn starts a note, or glides the one being held to the new one, and noteOff releases it. Both take an optional { at, id }: at is a time of the audio clock, now when there is none, and a noteOff whose id is not the held note's lets nothing go, since the held note took over from it. A note plays as loud as its velocity, from 0 to 1. The wave and the filter type of a state change under a held note. An agent plays the synth with the play tool.