Skip to content

Audio

vgai audio is built on the Web Audio API via Three.js’s AudioListener. Setup (packages/engine/src/setup/setup-audio.ts) creates the listener, a master gain, and three category buses.

music, sfx, and voice buses feed a master gain into the listener; spatial sounds attenuate with distance
The bus hierarchy and spatial attenuation.

The AudioContext provides:

  • listener — a THREE.AudioListener (attached to the camera, so spatial audio is heard from the camera’s position).
  • masterGain — the final gain node everything routes through.
  • busesmusic, sfx, and voice gain nodes, each independently adjustable.
  • resume() — call on the first user interaction; browsers start the audio context suspended until a gesture.

An entity plays sound via the audio schema (packages/engine/src/scene/schema/audio.ts):

FieldMeaning
srcpath to a WAV / MP3 / OGG file
spatialenable 3D positional audio
volume0–1 playback volume
refDistancedistance at which attenuation begins (spatial only)
rolloffFactorhow fast volume falls with distance (spatial only)
maxDistancedistance beyond which it’s inaudible (spatial only)
looploop the clip
autoplaystart when the scene loads

For spatial sounds, refDistance / rolloffFactor / maxDistance shape the falloff curve; non-spatial sounds play at a constant volume (UI, music).

packages/engine/src/audio/ ships synthesized sound generators so you can prototype without asset files. They’re grouped by purpose and re-exported from audio/index.ts:

  • ambientwind, rain, hum, nature
  • impactsbounce, crash, explosion, hit
  • movementfootstep, jump, whoosh
  • musicalarpeggio, bassPulse, stinger
  • ui-soundsuiClick, uiError, uiHover, uiSuccess
  • vehiclebrakeSqueal, engineDrone, tireSkid
  • weaponsgunshot, laser, chargeUp