Lab

useMasterBus

A master output bus with a safety limiter: every source connects into one gain node that a single fader governs.

pnpm dlx shadcn@latest add @audio-ui/use-master-bus
import { useMasterBus } from "@/hooks/use-master-bus";
Fileshooks/use-master-bus.ts
Depends onweb-audio
npmNothing
Used byblock-auto-pan, block-bass-enhancer, block-binaural-panner, block-channel-eq, block-chorus, block-compander, block-compressor, block-deesser, block-delay, block-distortion, block-drum-rack, block-drum-synth, block-ducker, block-electric-piano, block-equalizer, block-exciter, block-expander, block-filter, block-fm-synth, block-frequency-shifter, block-gate, block-graphic-eq, block-guitar, block-limiter, block-lofi, block-mid-side, block-mono-synth, block-multiband, block-organ, block-overdrive, block-phaser, block-piano-roll, block-pocket-synth, block-poly-synth, block-redux, block-reverb, block-rotary, block-sampler, block-saturator, block-string-machine, block-stutter, block-synth-panel, block-tape-keys, block-tilt-eq, block-transient, block-unlimiter, block-utility, block-vibe, block-wah, block-wave-shaper, pad-demo, piano-roll-playback-demo, sequencer, spectrum-analyzer-demo
ExportKindWhat it is
useMasterBusfunctionA master output bus: one persistent GainNode kept in sync with a volume control, then a safety limiter, then the shared AudioContext's destination.

Usage

Connect every sound of a screen into one master bus, never straight into ctx.destination. One fader then sets the level of everything, and a limiter at the end keeps a loud mix from clipping.

import { useMasterBus } from "@/hooks/use-master-bus";

const [volume, setVolume] = useState(0.7);
const output = useMasterBus(volume);

const voices = useSynthVoices({ destination: output });

<Fader aria-label="Volume" max={1} min={0} onValueChange={setVolume} value={volume} />;

The bus is a gain, a limiter and a trim that cancels the limiter's make-up gain, so below its threshold (-3 dBFS) the sound passes unchanged. A change of volume ramps over 10 ms, so dragging the fader never clicks. The limiter adds a few milliseconds of latency.

Props · 2

Built by Ouest Labs, the source code is available on GitHub.
RSS