Skip to main content

fadeGlobalIn

Raise the master volume over a number of seconds. Every sound goes through the master, so the whole mix fades in. Use it at the start of a scene, or to come back from a global mute.

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

Parameters​

ParameterTypeDefaultDescription
durationnumberfadeInDuration from the config (0.5)Length of the fade in seconds.
startVolumenumber0Master volume to start from.
endVolumenumbersee belowMaster volume to end at. Without it the fade ends at the current master volume, or at the remembered one when the master is at 0.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

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

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

// Fade the whole mix in from silence over 3 seconds
soundHub.fadeGlobalIn(3, 0, 1);

// Later, after soundHub.muteAllSounds():
// fade back to the volume from before the mute
soundHub.fadeGlobalIn(2);

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​

  • startVolume defaults to 0, not to the current volume. With the master at 1, fadeGlobalIn(2) drops to silence first and then fades back up.
  • The remembered master volume is the one from before muteAllSounds or fadeGlobalOut, or the last value given to setGlobalVolume.
  • When the fade ends, the global mute is lifted. Until then getGlobalVolume still returns 0 if you started from a mute.
  • A running fadeGlobalOut or fadeGlobalIn is replaced.
  • Every step dispatches master_volume_changed, and the end dispatches SoundEventsEnum.FADE_MASTER_IN_COMPLETED ('fade_master_in_completed').

See also​