LabDAW

Device chain

A track's devices in a row, each drawn by its own panel: move, fold, switch off and remove them, and drop a device where it goes. Controlled, with no audio in it.

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

pnpm 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

<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.

import { 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:

import {
  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.

import { 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.

<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

Drag a device by its title bar
Moves it. A label with its name follows the pointer, the device shows dimmed where it would land, the others aside, and the row scrolls near its edges. Escape puts it back. With onReceive, a device goes in or out of a rack's chain the same way.
Hold a device over a chain
The chain that would take it lights up, with a line where it lands, its end included. Anywhere over a rack is into the rack: its chain lights up, from its title bar and its list of chains too.
ArrowLeft / ArrowRight on the fold button
Move the device a place.
Delete / Backspace on the fold button
Remove the device.
Right-click a device
Move it left or right, or remove it.
Click a device's title bar
Folds it, outside its buttons: a drag of the title bar moves it instead.
Fold button
Folds the device to a strip with its square, its name down it and its arrow at the bottom; a click anywhere on the strip, outside its buttons, unfolds it.
Drop a device from the library
Adds it where it is dropped: between two devices, or at the end.

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

DeviceChainSlot

Props · 7

Parts

Components · 2

DeviceChainSlotControls
The slot's fold button, which the keyboard also moves and removes the device with, for the end of the device's own header.
DeviceChainAdd
The end of the chain: where a device dragged from a library is dropped, and a button that opens the library through onClick. Its children replace the words.
Built by Ouest Labs, the source code is available on GitHub.
RSS