Skip to main content

setGlobalVolume

Set the master volume. Every sound passes through the master gain, so this scales the whole mix at once. Use it for a main volume slider.

setGlobalVolume(volume: number): void;

Parameters​

ParameterTypeDefaultDescription
volumenumberrequiredMaster volume from 0 (silent) to 1 (full). Values outside that range are clamped.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
await soundHub.loadSound('music', '/audio/theme.mp3');

soundHub.play('music', { loop: true });

soundHub.setGlobalVolume(0.5); // everything at half
soundHub.setGlobalVolume(1); // full again

Try it​

Try it

Master volume

Idle
60%
01

The sound itself plays at volume 1. The slider sets the master gain every sound passes through.

Code
Your clicks show up here as soundhub calls

Good to know​

  • A sound's own volume and the master volume multiply. A sound at 0.5 with the master at 0.5 plays at a quarter.
  • While the hub is globally muted, the value is stored and used when you unmute, but nothing becomes audible. Use unmuteAllSounds for that.
  • The change is immediate. For a smooth change, use fadeGlobalIn or fadeGlobalOut.
  • Dispatches SoundEventsEnum.MASTER_VOLUME_CHANGED ('master_volume_changed') with isMaster: true.
  • The master volume starts at defaultVolume from the config, 1 by default.

See also​