Skip to main content

Getting Started

soundhub is a TypeScript library that wraps the Web Audio API in a single SoundHub class. It handles loading, playback, volume, fading, panning, spatial positioning, sprites, groups and events, without you having to wire up audio nodes by hand. It also ducks music under a voice and varies repeated sounds.

The main package is 23 KB gzipped and has no dependencies. Two optional entry points sit next to it: soundhub/ui with interface sounds that need no files (1.5 KB), and soundhub/howler, which runs Howler.js code on soundhub (3.7 KB). Coming from Howler.js? Start with Migrating from Howler.js.

Every method on the interface has its own page in this documentation. Use the sidebar to browse by topic, or the search box in the header to jump straight to a method.

Installationโ€‹

npm install soundhub
yarn add soundhub

Your first soundโ€‹

import { SoundHub, SoundEventsEnum, type SoundEvent } from 'soundhub';

const soundHub = new SoundHub();

soundHub.addEventListener(SoundEventsEnum.LOADED, (event: SoundEvent) => {
console.log('Sound loaded', event);
});

await soundHub.loadSounds([{ id: 'music', url: '/sounds/music.mp3' }]);
soundHub.play('music');
Browsers block audio until the user interacts

Most browsers suspend the audio context until the visitor clicks, taps or presses a key. Trigger your first play() from a real user action, or call resumeContext once the visitor interacts with the page.

Where to go nextโ€‹

TopicStart here
โฏ๏ธ Playbackplay ยท pause ยท stop ยท seek
๐ŸŽ›๏ธ All sounds at oncestopAllSounds ยท pauseAllSounds
๐Ÿ”Š Volume & mutesetSoundVolume ยท setGlobalVolume ยท toggleMute
๐ŸŒ— FadingfadeIn ยท fadeOut
๐Ÿ” Looping & playback ratesetLoop ยท setPlaybackRate
๐Ÿ“ฅ Loading soundsloadSounds ยท loadSound ยท registerSound ยท canPlay
๐Ÿ“Š State & progressisPlaying ยท getProgress ยท startProgressTracking
๐ŸŽš๏ธ Pan & balancesetPan ยท setGlobalPan
๐ŸŽฏ Spatial audiosetSpatialPosition ยท setListenerPosition ยท setSpatialOrientation
๐ŸŽถ Sound spritesSound sprites ยท setSoundSprite
๐Ÿ‘ฅ Sound groupscreateSoundGroup ยท addToSoundGroup
๐Ÿฆ† DuckingDucking ยท duck
๐ŸŽฒ VariationsVariations ยท createVariations
๐Ÿ”” Interface soundsInterface sounds ยท addUiSounds
๐ŸŽง Audio context & nodesresumeContext ยท getMasterOutput
๐Ÿ›ก๏ธ Master limiterMaster limiter ยท setMasterLimiter
๐Ÿ“ก EventsaddEventListener ยท SoundEvent
๐Ÿ”„ Reset & cleanupreset ยท destroy
๐Ÿ”ง UtilitiesgetConfig ยท setDebugMode
๐Ÿ“˜ Types & InterfacesPlayOptions ยท SoundHubConfig
๐Ÿ”€ Migrating from Howler.jsMigrating from Howler.js ยท Howl ยท Howler

Try it liveโ€‹

Want to hear it before you install anything? The live demo lets you play with volume, panning, fading, sprites and spatial audio in the browser.

Many pages in this documentation also carry a Try it section with a working player, so you can hear what a method does while you read about it. Start with play or fadeIn.

New in 6.5.0โ€‹

WhatWhere
Turn music down while a voice plays, with duck('music', { when: 'voice' })Ducking ยท duck
Several takes under one name, with a pitch and volume spreadVariations ยท createVariations
Add an AudioBuffer you already have as a soundaddBuffer
soundhub/ui: twelve interface sounds rendered in the browserInterface sounds
soundhub/howler: the Howler.js API on a shared hubMigrating from Howler.js
Two new events, duck_started and duck_ended, for 40 in allsoundEvents

New in 6.4.0โ€‹

WhatWhere
Every time is a position in the file, at any playback rate, for buffered sounds and streams alikegetCurrentTime ยท seek
A muted sound stays muted when you play it againmute ยท toggleMute
defaultVolume and defaultPan apply to each sound, not a second time to the masterSoundHubConfig
Eight more fixes, among them playing again after a fade to silence and a rate change without an eventChangelog

New in 6.3.0โ€‹

WhatWhere
Types that work with moduleResolution node16 and nodenext, and a CommonJS build for require()Changelog
Hover text in your editor for every method and optionSoundHubConfig ยท PlayOptions
Ten fixes, among them muting twice, fading out a stream and playing again after a seekChangelog

New in 6.2.0โ€‹

WhatWhere
overlap, the new name for createNewInstancePlayOptions
A list of urls per sound, so the browser picks the formatloadSound ยท canPlay
Register a sound now, fetch it laterregisterSound ยท getLoadState
A listener you can move through the scenesetListenerPosition
Sounds that point somewhere, for the cone settingssetSpatialOrientation
Request headers, an idle timeout and an instance ceilingSoundHubConfig