utils-sound wraps Howler.js to provide a structured audio system with three playback modes: looping music, looping sound effects, and one-shot sound effects. Volume levels are automatically synced to the global stateSound from state-shared.
createSound()
Factory function typed by your game’s sound name union. Create one instance per game.Return value
load()
Must be called once after assets have loaded. Receives aLoadedAudio object from stateApp.loadedAssets (populated by pixi-svelte).
Howl instance and three Player instances:
| Player | Loop | Behaviour |
|---|---|---|
players.music | Yes | Only one music track plays at a time. Starting a new track pauses the current one. |
players.loop | Yes | Starts a looping sound effect; does nothing if already playing. |
players.once | No | Plays a one-shot sound; auto-removes from map on end. Supports forcePlay to restart. |
Player methods
Each player insound.players exposes:
Playback examples
Type reference
Volume system
Volume is controlled globally viastateSound from state-shared. All three players observe this state via Svelte $effect.
sound.volumeEffect() inside a Svelte component (e.g. the root Game.svelte) to register the reactive effects that keep player volume in sync:
Enable / disable
enableEffect() registers a $effect that calls Howler.mute(true) whenever the tab is hidden or the AudioContext is not running (e.g. the browser blocked autoplay), and Howler.mute(false) when both conditions clear:
components-ui-pixi expose a ButtonSoundSwitch component that lets the player toggle sound on/off by writing to stateSound.volumeValueMaster.
