# master-bus

> A master output bus on a context: a gain for the volume, then a safety limiter, then the destination. Every source connects to its input, so one control governs the level and a loud mix cannot clip the output.

```bash
npx shadcn@latest add @audio-ui/master-bus  # React
npx @audio-ui/cli add master-bus  # Vue, Svelte, web
```

```ts
import { createMasterBus } from "@/lib/master-bus";
```

- Files: `lib/master-bus.ts`
- Depends on: nothing; npm: nothing
- Used by: `use-master-bus`

| Export | Kind | What it is |
| --- | --- | --- |
| `MasterBus` | interface | A master output bus: the node every source connects to, its volume, and what takes it away. |
| `createMasterBus` | function | A master output bus on `ctx`: a gain for the volume, then a safety limiter, then the destination. |

## Usage

Every sound of a screen (synth voices, drum hits, a device's engine) connects to the bus's `input` instead of `ctx.destination`. The bus is a gain for the volume, then a safety limiter: below -3 dBFS the sound passes unchanged, above it the peaks stay under full scale instead of clipping. It works the same in any framework.

```ts
import { createMasterBus } from "@/lib/master-bus";
import { webAudio } from "@/lib/web-audio";

const ctx = webAudio.getContext();
const bus = createMasterBus(ctx, 0.7);
engine.output.connect(bus.input);

bus.setVolume(0.5); // a short ramp, no click
bus.dispose(); // when the screen goes away
```

The limiter delays the sound by a few milliseconds. In React, [`useMasterBus`](/docs/hooks/use-master-bus) makes one on the page's context and keeps its volume in sync with a value.
