# rack

> Racks: devices in parallel chains, each with its volume, pan, switch and solo, lined up on the slowest and summed, and eight macros that turn parameters of the devices inside over a stretch of their travel; the engine that plays one on a track's chain.

```bash
npx shadcn@latest add @audio-ui/rack
```

```ts
import { heardChains, macroPositions, playedState, deviceLatency, rackLatency, /* … */ } from "@/lib/rack";
```

- Files: `lib/rack.ts`
- Depends on: `arrangement`, `device-library`; npm: `@audio-ui/engine`
- Used by: `arrangement-engine`, `drum-rack`, `instrument-rack`

| Export | Kind | What it is |
| --- | --- | --- |
| `FindDevice` | type | Finds a device of the library by the id a track device stores. |
| `heardChains` | function | The chains a solo lets through: the soloed ones, or all of them while none is. |
| `MacroPositions` | type | Where on its travel each mapped parameter sits, by device then parameter, from a rack's macros. |
| `macroPositions` | function | The positions a rack's macros put their parameters at, over the ones a rack around it already set. |
| `playedState` | function | A device's state as it plays: its own, with what the macros mapped to it set. |
| `deviceLatency` | function | How long a device delays the sound as it plays, in seconds: a rack, as long as its slowest chain. |
| `rackLatency` | function | How long a rack delays the sound, in seconds: its slowest chain, each device on it in turn, switched on or off. |
| `RackEngine` | interface | A rack playing on a context: one link of a chain, its sound in at `input` and out at `output`. |
| `createRackEngine` | function | Plays racks on `ctx`: each chain takes the rack's input, its devices in series (one switched off passes the sound on, as late as it would make it), then waits for the slowest chain so all of them meet on the same frame, then its volume, pan and switch, summed at the output. |

## Usage

Devices in parallel chains, each with its own volume, pan, switch and solo, lined up on the slowest and summed, and eight macros mapped onto the parameters inside, each over a range. It is what plays a rack on a track's chain, in the [arrangement engine](/docs/lib/arrangement-engine); its face is the [Rack panel](/docs/components/base/rack).

```ts
import { createRackEngine, rackLatency } from "@/lib/rack";

const engine = createRackEngine(ctx, libraryDevice); // find: a device of the library by its id
source.connect(engine.input);
engine.output.connect(destination);
engine.update(rack, { tempo }); // the rack as the document holds it
```

`macroPositions` gives each mapped parameter's value for the macros' positions, `heardChains` the chains a solo leaves heard, and `rackLatency(rack, find, sampleRate)` how late the slowest chain is, for delay compensation; a rack's engine also says so with `latency()`.
