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.
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');
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โ
| Topic | Start here |
|---|---|
| โฏ๏ธ Playback | play ยท pause ยท stop ยท seek |
| ๐๏ธ All sounds at once | stopAllSounds ยท pauseAllSounds |
| ๐ Volume & mute | setSoundVolume ยท setGlobalVolume ยท toggleMute |
| ๐ Fading | fadeIn ยท fadeOut |
| ๐ Looping & playback rate | setLoop ยท setPlaybackRate |
| ๐ฅ Loading sounds | loadSounds ยท loadSound |
| ๐ State & progress | isPlaying ยท getProgress ยท startProgressTracking |
| ๐๏ธ Pan & balance | setPan ยท setGlobalPan |
| ๐ฏ Spatial audio | setSpatialPosition ยท isSpatialAudioSupported |
| ๐ถ Sound sprites | Sound sprites ยท setSoundSprite |
| ๐ฅ Sound groups | createSoundGroup ยท addToSoundGroup |
| ๐ง Audio context & nodes | resumeContext ยท getMasterOutput |
| ๐ก๏ธ Master limiter | Master limiter ยท setMasterLimiter |
| ๐ก Events | addEventListener ยท SoundEvent |
| ๐ Reset & cleanup | reset ยท destroy |
| ๐ง Utilities | getConfig ยท setDebugMode |
| ๐ Types & Interfaces | PlayOptions ยท SoundHubConfig |
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.