Skip to content

Audio

A 3D positional audio source attached to the active camera's AudioListener.

ts
import {
  Behaviour,
  
} from "@relu-interactives/spatial-ecs";

export default class SpawnSound extends Behaviour {
  init() {
    const objects = this.world.getObjectManagementSystem();
    objects?.requestCreate("audio", {
      name: "Ambience",
      path: "/assets/audio/ambience.mp3",
      transform: { position: { x: 0, y: 1, z: 0 } },
      options: {
        autoplay: true,
        loop: true,
        volume: 0.5,
        positional: true,
        distanceModel: "linear",
        refDistance: 1,
        rolloffFactor: 1,
      },
    });
  }
}

Payload

FieldTypeDefaultDescription
namestring"Audio"Display name.
pathstring""URL or local-asset path to the audio clip (MP3, WAV, OGG).
transformTransformDataidentityPosition / rotation / scale (position drives spatialization).
parentIdEntityId | nullnullOptional parent entity.

options

OptionTypeDefaultDescription
autoplaybooleanfalseStart playback as soon as the clip is loaded.
loopbooleanfalseLoop playback indefinitely.
volumenumber (0–1)1Linear gain.
pitchnumber1Playback rate multiplier (1 = normal speed/pitch).
positionalbooleanfalseWhen true, audio is spatialized using the entity's transform.
refDistancenumber1Distance at which volume is unattenuated (positional only).
distanceModel"linear" | "inverse" | "exponential""inverse"WebAudio distance model (positional only).
rolloffFactornumber1How quickly volume falls off past refDistance.

⚠️ Common mistake — wrong payload shape

path and options must be top-level keys on the payload. Never nest them inside an audio: sub-object.

ts
// ✅ Correct
objects?.requestCreate("audio", {
  name: "SFX",
  path: "localasset://assets/sfx/hit.mp3",
  options: { autoplay: true, volume: 0.8 },
});

// ❌ Wrong — do NOT wrap inside audio:{}
objects?.requestCreate("audio", {
  name: "SFX",
  audio: { path: "...", options: { autoplay: true } }, // ← this will be ignored
});

Components attached

Transform, Object3DRef, Name, EntityType, ParentId, Selectable, Audio.