Mixing and sound banks
Every sound plays through a bus: sfx, voice, music or ui. A bus has its own volume, so a game can offer a music slider without touching a single sound, and music dips by itself while a voice plays. The ui bus is for menus, and a pause never holds it.
Buses
An AudioComponent picks its bus in options.bus (default sfx).
There are two layers of volume:
- The player's, from a volume menu:
master,music,sfx,voice,uiandmutedin settings. Saved between visits and applied by the engine. This is what a settings screen writes. - The game's own mix:
setBusVolume, for a cutscene that lifts the music or a level that wants quieter effects. Never saved.
A bus plays at the game's volume times the player's, through the master:
import { Behaviour } from "@relu-interactives/spatial-ecs";
export class VolumeMenu extends Behaviour {
// A player's slider: saved, and applied at once.
setMusic(volume: number) {
this.settings.update({ audio: { music: volume } });
}
toggleMute() {
this.settings.update({ audio: { muted: !this.settings.section("audio").muted } });
}
// The game's mix: a boss fight's music, louder for now.
bossMusic() {
this.world.getAudioSystem()?.setBusVolume("music", 1.2);
}
}Sound stays suspended until the player's first tap, click or key, as browsers require; the engine resumes it then. A game in a background tab goes quiet while audio.muteInBackground is on.
Sound banks
A game usually wants a kind of sound ("hit", "footstep"), not one file: a few takes so it doesn't repeat, a chance so it doesn't fire every time, a cooldown so a flurry doesn't stack. A .soundbank.json asset holds that:
{
"version": 1,
"slots": {
"hit": {
"bus": "sfx",
"takes": [{ "file": "assets/sfx/hit-1.mp3" }, { "file": "assets/sfx/hit-2.mp3", "volume": 0.8 }],
"cooldownMs": 80
},
"taunt": {
"bus": "voice",
"chance": 0.5,
"priority": 1,
"takes": [{ "file": "assets/voice/taunt.mp3" }]
}
}
}file paths start at assets/. Files a bank names ship with a publish.
| Field | Default | Description |
|---|---|---|
takes | The recordings. The same take never plays twice running when there's another. | |
bus | sfx | The volume group. |
chance | 1 | Probability it plays at all. |
cooldownMs | 0 | Asks within this long of the last play are ignored. |
volume | 1 | Linear gain; a take's volume multiplies it. |
priority | 0 | On the voice bus, a lower priority stays quiet while a higher one speaks, and an equal or higher one cuts in. |
Load a bank once, then play slots by name. playSlot says whether it sounded; a slot kept quiet by its chance or cooldown isn't an error.
import { Behaviour } from "@relu-interactives/spatial-ecs";
export class Fighter extends Behaviour {
data = { sounds: { path: "", type: "file" as const } };
protected init() {
void this.loadSoundBank(this.data.sounds);
}
takeHit() {
this.playSlot("hit", { position: this.transform.position });
}
}Without position, a slot plays evenly everywhere. Audio needs a user gesture before a browser will play it, so play from input, not from init.
Pausing
world.pause() holds everything playing, on the sfx, voice and music buses, and world.resume() continues it from the same point. Music ducked under a held voice ducks again for the rest of the line.
Sounds on the ui bus are never held, so the click of the button that opened a menu finishes. Sounds started during a pause play on any bus. See Pausing the Game.

