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
| Parameter | Type | Default | Description |
|---|---|---|---|
volume | number | required | Master 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
Master volume
IdleThe 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.5with the master at0.5plays at a quarter. - While the hub is globally muted, the value is stored and used when you unmute, but nothing becomes audible. Use
unmuteAllSoundsfor that. - The change is immediate. For a smooth change, use
fadeGlobalInorfadeGlobalOut. - Dispatches
SoundEventsEnum.MASTER_VOLUME_CHANGED('master_volume_changed') withisMaster: true. - The master volume starts at
defaultVolumefrom the config,1by default.
See also
getGlobalVolume: read the master volume.setSoundVolume: the volume of one sound.muteAllSounds: silence the mix and remember the volume.