Skip to main content

fadeGlobalOut

Lower the master volume over a number of seconds, so the whole mix fades out. Use it at the end of a scene or before a page change.

fadeGlobalOut(duration?: number, startVolume?: number, endVolume?: number): void;

Parameters​

ParameterTypeDefaultDescription
durationnumberfadeOutDuration from the config (0.5)Length of the fade in seconds. 0 or less does nothing.
startVolumenumbercurrent master volumeMaster volume to start from.
endVolumenumber0Master volume to end at.

Returns​

Nothing.

Example​

import { SoundHub, SoundEventsEnum } from 'soundhub';

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

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

// Stop everything once the fade has finished
soundHub.once(SoundEventsEnum.FADE_MASTER_OUT_COMPLETED, () => {
soundHub.stopAllSounds();
});

soundHub.fadeGlobalOut(3);

Try it​

Try it

Fade the master volume

Idle
Direction
3 s
0.5 s8 s
Master gain1.00

The fade runs on the master gain, so every sound on the page follows it.

Code
Your clicks show up here as soundhub calls

Good to know​

  • The fade does not stop any sound. They keep playing at master volume 0 until you stop them.
  • A fade that ends at 0 counts as a global mute. The master stays silent for sounds you play later, until you call fadeGlobalIn, unmuteAllSounds or toggleGlobalMute. All three go back to the volume the fade started from.
  • A running fadeGlobalIn or fadeGlobalOut is replaced.
  • Every step dispatches master_volume_changed, and the end dispatches SoundEventsEnum.FADE_MASTER_OUT_COMPLETED ('fade_master_out_completed').

See also​