Audio UI
The devices of a track, in the order the sound goes through them, with a way to add one from a library. Each slot shows the device's own panel, the face it has on its own page, so a device looks the same alone and in a chain. The chain keeps no list and makes no sound: it draws the slots you give it, in the order you give it, and a move, a removal or a device dropped on it comes back through a callback. Your engine follows the same list, whether it runs on Web Audio, in an AudioWorklet or in WASM.
Installation
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/device-chain
The library is an item of its own: see Device browser. The block above, which plays a loop through the chain, is @audio-ui/block-device-chain.
Anatomy
Copy to Clipboard<DeviceChain> <DeviceChainSlot> <CompressorPanel> <DeviceChainSlotControls /> </CompressorPanel> </DeviceChainSlot> <DeviceChainAdd /> </DeviceChain>
Usage
A slot is an id, the device it holds and its state. Keep the list, give the chain its ids, and put the device's panel in each slot with DeviceChainSlotControls at the end of its header: the children of a panel go there. The devices are items of their own: npx shadcn@latest add @audio-ui/compressor @audio-ui/delay for the two below.
Copy to Clipboardimport { useState } from "react"; import { DeviceChain, DeviceChainAdd, DeviceChainSlot, DeviceChainSlotControls, } from "@/components/audio-ui/device-chain"; import { type CompressorState, compressorDevice, } from "@/components/audio-ui/devices/compressor/device"; import { CompressorPanel } from "@/components/audio-ui/devices/compressor/panel"; import { type DelayState, delayDevice, } from "@/components/audio-ui/devices/delay/device"; import { DelayPanel } from "@/components/audio-ui/devices/delay/panel"; type Slot = | { device: "compressor"; id: string; state: CompressorState } | { device: "delay"; id: string; state: DelayState }; const [slots, setSlots] = useState<Slot[]>([ { device: "compressor", id: "a", state: compressorDevice.defaults() }, { device: "delay", id: "b", state: delayDevice.defaults() }, ]); const move = (id: string, index: number) => setSlots((current) => { const moved = current.find((slot) => slot.id === id); const rest = current.filter((slot) => slot.id !== id); return moved ? [...rest.slice(0, index), moved, ...rest.slice(index)] : current; }); const change = (id: string, state: Slot["state"]) => setSlots((current) => current.map((slot) => (slot.id === id ? ({ ...slot, state } as Slot) : slot)) ); <DeviceChain aria-label="Devices" onMove={move} onRemove={(id) => setSlots((current) => current.filter((slot) => slot.id !== id))} order={slots.map((slot) => slot.id)} > {slots.map((slot) => ( <DeviceChainSlot enabled={slot.state.enabled} key={slot.id} onEnabledChange={(enabled) => change(slot.id, { ...slot.state, enabled })} title={slot.device === "compressor" ? compressorDevice.name : delayDevice.name} value={slot.id} > {slot.device === "compressor" ? ( <CompressorPanel className="w-96" onValueChange={(state) => change(slot.id, state)} value={slot.state} > <DeviceChainSlotControls /> </CompressorPanel> ) : ( <DelayPanel className="w-80" onValueChange={(state) => change(slot.id, state)} value={slot.state} > <DeviceChainSlotControls /> </DelayPanel> )} </DeviceChainSlot> ))} <DeviceChainAdd onClick={openLibrary} /> </DeviceChain>;
onMove(id, index) gives the place the slot lands in, counted once it has left its own: take it out of the list, then put it back at index. Leave out onMove and the slots stay where they are; leave out onRemove and none can be removed. The panel sets the width of its slot, so give it one with className. enabled and onEnabledChange are for the on and off square of the folded strip; open, the panel's own square does it.
A device of your own
A device with no panel of its own, such as the tremolo of Your own device, is drawn from its parameters by the frame, the header and the panel of @audio-ui/device-panel, so any device goes in a chain the day it is defined:
Copy to Clipboardimport { DeviceFrame, DeviceHeader, DevicePanel, } from "@/components/audio-ui/device-panel"; <DeviceChainSlot title={tremolo.name} value={slot.id}> <DeviceFrame className="h-full w-72"> <DeviceHeader activator="on" device={tremolo} onValueChange={change} value={state}> <DeviceChainSlotControls /> </DeviceHeader> <DevicePanel className="p-2" device={tremolo} exclude={["on"]} onValueChange={change} value={state} /> </DeviceFrame> </DeviceChainSlot>;
Every device
device-panels (npx shadcn@latest add @audio-ui/device-panels) is every device's panel by the device's stable id, with the width it is drawn at in a row, and device-library is every device with its engine, by shelf. Together they draw and play a chain from nothing but the ids and states a project stores. Each panel is loaded the first time it is drawn, so a page loads the panels of the devices it shows and no other: draw it inside a Suspense.
Copy to Clipboardimport { Suspense } from "react"; import { devicePanel } from "@/components/audio-ui/device-panels"; import { DEVICE_SHELVES } from "@/lib/device-library"; const shelves = DEVICE_SHELVES.map((shelf) => ({ items: shelf.devices.map((entry) => entry.device), label: shelf.label, })); {track.devices.map((device) => { const face = devicePanel(device.device); return face ? ( <DeviceChainSlot className={face.width} key={device.id} title={device.device} value={device.id}> <Suspense fallback={null}> <face.Panel onValueChange={(state) => setDevice(device.id, state)} value={device.state}> <DeviceChainSlotControls /> </face.Panel> </Suspense> </DeviceChainSlot> ) : null; })}
Playing the chain
The block holds an arrangement of one track, whose chain is the devices on screen, and the arrangement's engine plays a loop through it: a device added, moved, removed or turned reaches the engine as a command of the arrangement, and the sound follows it. The engines are the devices' own (<name>-engine), the same ones their pages play.
Over an engine of your own, keep the same list and send it the order and the states: the chain and the panels never touch the audio.
A track's devices
device-view (npx shadcn@latest add @audio-ui/device-view) is the chain of one track of an arrangement, controlled: each device on its own panel, racks with their macros, chains and zones, a drum rack with its pads, a device dropped from the library where it lands, and, at the end of the chain, a way to open the library or add a rack. It is the view the workstation shows under its tracks. Every change is a command of the arrangement, so it is one step of undo and an engine that follows the arrangement follows it. With activeNotes, the notes held now on the track, such as on a MIDI keyboard, light up its instrument's keys or pads.
Copy to Clipboard<DeviceView arrangement={arrangement} // useArrangement onDeviceMoved={(track, device, parameters) => {}} // a knob turned by hand stops following its envelope onHoldNote={(track, note) => {}} // an instrument's pad holds a note; null lets it go onNoteOff={(track, pitch) => {}} onNoteOn={(track, pitch, velocity) => {}} onOpenLibrary={() => {}} // the library, ready to search onPadFiles={(track, rack, note, files) => {}} // files dropped on a pad of a drum rack overridden={automation.overridden} position={engine.position} track={selectedTrack} activeNotes={heldNotes} // the notes held now on the track: its instrument's keys or pads light up />
What a view remembers per device, such as a slot folded, the chain picked in a rack or whether its macros show, lasts as long as the view. Give useEntry a hook with the shape of useState by an entry name and a device id to keep them, in storage for example, so they come back.
Racks
A device can be a rack: chains of devices side by side, summed, with eight macros. Its face goes in a slot like any device, with a chain of its own inside it. That chain has no scroll of its own: the rack is as wide as its devices, and the outermost chain scrolls them all. See Rack.
Interaction
Chain · 8
Escape puts it back. With onReceive, a device goes in or out of a rack's chain the same way.Each slot is a group named after its device. Its title bar carries one button, the fold button, "Fold Compressor", which is also the device's stop for the keyboard and says its keys: the arrows move the device, Delete removes it. A move or a removal is announced ("Compressor moved to 2 of 3."). After a move the focus stays on the fold button, after a removal it goes to the device that takes the place, and after a fold to the button that unfolds it. The library's list is a list box, and its shelves are toggle buttons.
API reference
DeviceChain
Props · 7
index, counted once it has left its place. Leave it out and the slots stay where they are.DeviceChainSlot
Props · 7
order.folded to control it.Parts
Components · 2
DeviceChainSlotControlsDeviceChainAddonClick. Its children replace the words.