logo

Install with the CLI:

bjs download Sound

Sound

One audio clip per entity: a flat background bed, or a positional sound that gets louder as you walk up.

What it does

SoundSystem loads the clip through the renderer adapter and plays it. A flat sound (spatial: false) is heard the same everywhere. A positional one rides its entity's mesh (from meshprimitive.created or mesh.loaded) and fades with distance from the listener. The listener rides the player's camera: the system attaches it when firstpersoncamera.created or arccamera.created arrives.

Browsers block audio until the user clicks or presses a key. If the audio engine will not start, the system emits sound.blocked once, waits quietly, and tries again on the first click or key press. Nothing throws.

Use it in a scene

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Fastest path

bjs download scene Sound

Pulls this exact scene into your project and runs it — no copy-paste. Add --all to grab the components and assets it needs too.

Or build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add Sound
    bjs download Sound

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "entities": {
    "Cabinet": {
      "components": {
        "MeshPrimitive": {
          "primitive": "box",
          "width": 1,
          "height": 2,
          "depth": 1,
          "position": [0, 1, 0]
        },
        "Sound": {
          "src": "/audio/attract-loop.mp3",
          "loop": true,
          "spatial": true,
          "minDistance": 1,
          "maxDistance": 12,
          "rolloff": 3
        }
      }
    }
  }
}

Props

  • src (string) — URL of the clip. Empty means no clip yet.
  • loop (boolean, default false).
  • autoplay (boolean, default true) — play once loaded.
  • volume (number, default 1).
  • spatial (boolean, default false) — place the sound at the entity's mesh.
  • minDistance (default 1), maxDistance (default 100), rolloff (default 1), distanceModel (linear | inverse | exponential, default inverse) — how a positional sound fades.
  • playing (boolean, optional) — live play/stop control from a panel or another system.

Events

  • emits sound.ready { entityId, src } — the clip loaded.
  • emits sound.blocked — the browser is holding audio until a user gesture.
  • emits sound.error { entityId, src } — the clip could not load.
  • listens sound.play / sound.stop { entityId }, sound.volume.set { entityId, volume }.
  • listens firstpersoncamera.created, arccamera.created — to place the listener.
  • listens meshprimitive.created, mesh.loaded — to place a positional sound.

Dependencies

None. A positional sound needs a MeshPrimitive or Mesh on the same entity.

More like this

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search