Skip to main content

SoundHubConfig

SoundHubConfig defines the options you pass when creating a SoundHub. Every option is optional; anything you leave out falls back to the default below.

const soundHub = new SoundHub({
defaultVolume: 0.8,
masterLimiter: true,
debug: true,
});

Try it

Change the configuration and create a manager with it. Each option below is applied to a freshly created SoundHub, so you can hear the difference immediately.

No manager created yet
Note: each run creates a brand new SoundHub and destroys the previous one, which is exactly what the config affects. Options that only apply while loading, such as fetchRetries, will not be audible here.

Playback defaults

These set the starting point for every sound you load. Individual calls to play() can still override them through PlayOptions.

PropertyTypeDefaultDescription
defaultVolumenumber1Starting volume for new sounds (0 to 1)
defaultPlaybackRatenumber1Starting playback rate
defaultStartTimenumber0Offset in seconds where playback begins
defaultDurationnumberundefinedHow long to play, in seconds. Undefined plays the full sound
defaultPannumber0Stereo pan, -1 (left) to 1 (right)
defaultPanTypeSoundPanTypeStereoStereo or spatial panning, see SoundPanType
defaultPanSpatialPosition{ x, y, z }{ x: 0, y: 0, z: 0 }Starting 3D position
fadeInDurationnumber0.5Default fade-in length in seconds
fadeOutDurationnumber0.5Default fade-out length in seconds
loopSoundsbooleanfalseLoop every sound by default
maxLoopsnumber-1Number of loops when looping. 0 or -1 is infinite
createNewInstancebooleanfalseLet each play() create an independent instance, so the same sound can overlap with itself
trackProgressbooleantrueEmit PROGRESS events during playback

Output

PropertyTypeDefaultDescription
masterLimiterbooleanfalseInsert a limiter before the output so overlapping sounds cannot clip. See Master Limiter
spatialAudiobooleantrueEnable 3D spatial audio features, when the browser supports them
pannerNodeConfigSoundPannerConfigsee belowDistance and cone settings for 3D sound, see Spatial Audio
tip

masterLimiter is off by default so that upgrading never changes how an existing project sounds. Turn it on when you play several sounds at the same time, such as a playable instrument or a busy game scene.

Loading

PropertyTypeDefaultDescription
webAudioPreferredbooleantruePrefer the Web Audio API over the HTML5 audio fallback
html5AudioFallbackbooleantrueFall back to an Audio element when Web Audio loading fails
maxParallelLoadsnumber10How many sounds to fetch at the same time
retryDelaynumber0.5Delay between retry attempts, in seconds
audioCachebooleantrueAllow the browser to cache the fetched audio
maxAudioSizenumber52428800Refuse files larger than this, in bytes (50 MB)

Network

PropertyTypeDefaultDescription
fetchRetriesnumber2Retries per failed fetch
fetchTimeoutnumber8Fetch timeout in seconds
corsProxystringundefinedURL of a CORS proxy for cross-origin audio
fetchStrategy'direct-first' | 'proxy-first' | 'direct-only''direct-first'Whether to try the direct URL or the proxy first
crossOrigin'anonymous' | 'use-credentials' | nullnullcrossOrigin attribute for the HTML5 fallback
credentialStrategy'auto' | 'omit' | 'include''auto'Which credentials mode to fetch with

Mobile and page lifecycle

PropertyTypeDefaultDescription
autoUnlockbooleantrueUnlock audio on the first user gesture, needed on mobile browsers
autoMuteOnHiddenbooleantrueMute when the tab goes to the background
autoResumeOnFocusbooleantrueUnmute when the tab becomes visible again

Debugging

PropertyTypeDefaultDescription
debugbooleanfalseLog what the sound manager is doing to the console

Reading the configuration back

getConfig() returns the merged configuration, so you see the defaults as well as your own values:

const soundHub = new SoundHub({ defaultVolume: 0.8 });

const config = soundHub.getConfig();

console.log(config.defaultVolume); // 0.8, your value
console.log(config.maxParallelLoads); // 10, the default
console.log(config.masterLimiter); // false, the default

The returned object is a copy, so changing it does not affect the manager. Use the dedicated methods instead:

soundHub.setDebugMode(true);
soundHub.setGlobalVolume(0.5);
soundHub.setMasterLimiter(true);
soundHub.setProgressUpdateInterval(100);