Skip to main content

fadeIn

Raise the volume of one sound over a number of seconds. If the sound is not playing yet, fadeIn starts it, so one call gives you a soft start.

fadeIn(id: string, duration: number, startVolume?: number, endVolume?: number, skipDispatchEvent?: boolean): void;

Parameters​

ParameterTypeDefaultDescription
idstringrequiredID of the sound or stream.
durationnumberrequiredLength of the fade in seconds.
startVolumenumbersee belowVolume to start from. Without it the fade starts at the current volume, or at 0 when the sound is already at or above the end volume.
endVolumenumberthe sound's volumeVolume to end at. When the sound's volume is 0 (muted, or faded out), the default is 1.
skipDispatchEventbooleanfalseWhen true, no fade_in_completed event is dispatched.

Returns​

Nothing.

Example​

import { SoundHub } from 'soundhub';

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

// Starts the sound and fades it from 0 to its volume over 3 seconds
soundHub.fadeIn('music', 3);

// Or pick the range: from 0.2 to 0.8 over 2 seconds
soundHub.fadeIn('music', 2, 0.2, 0.8);
tip

To fade in every time a sound starts, pass fadeInDuration to play in the PlayOptions instead.

Try it​

Try it

Fade a sound in

Idle
3 s
0.5 s8 s
Gain0.00

The sound starts at volume 0 and the meter follows its gain node up to 1.

Code
Your clicks show up here as soundhub calls

Good to know​

  • A fade that is already running on the sound is replaced. When that was a fade out, the fade in starts from the volume it had reached.
  • At the end the volume is stored on the sound and in its play options, so the next play() uses it.
  • Every step dispatches volume_changed, and the end dispatches SoundEventsEnum.FADE_IN_COMPLETED ('fade_in_completed'). setSoundVolume and stop cancel the fade without the completed event.
  • On a stream from loadStream the fade starts at 0 unless you pass startVolume, and ends at the stream's volume.
  • For an id that is not loaded, the error is logged, stored for getLastError and dispatched as an error event.

See also​