audio/ui
Toggle Mode D
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/master-bus
Copy to Clipboardimport { 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.
Copy to Clipboardimport { 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 makes one on the page's context and keeps its volume in sync with a value.