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
| Parameter | Type | Default | Description |
|---|---|---|---|
id | string | required | ID of the sound or stream. |
duration | number | required | Length of the fade in seconds. |
startVolume | number | see below | Volume 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. |
endVolume | number | the sound's volume | Volume to end at. When the sound's volume is 0 (muted, or faded out), the default is 1. |
skipDispatchEvent | boolean | false | When 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
Fade a sound in
IdleGain0.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 dispatchesSoundEventsEnum.FADE_IN_COMPLETED('fade_in_completed').setSoundVolumeandstopcancel the fade without the completed event. - On a stream from
loadStreamthe fade starts at0unless you passstartVolume, and ends at the stream's volume. - For an id that is not loaded, the error is logged, stored for
getLastErrorand dispatched as anerrorevent.
See also
fadeOut: the reverse, with an option to stop afterwards.fadeGlobalIn: fade the whole mix in.PlayOptions:fadeInDurationandfadeInStartVolume.