Skip to content

World and Initialization

A World is the top-level object that owns the Steam Audio WASM runtime, the acoustic scene, the simulator, and the listener. All other objects — sources, acoustic meshes, and audio nodes — are created from a world.

To create a world, pass an AudioContext to createWorld(). The function is asynchronous because it loads the WebAssembly module, fetches the worklet script, and adds the processor to the audio context.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const audioContext: AudioContext
audioContext
= new
var AudioContext: new (contextOptions?: AudioContextOptions) => AudioContext

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.

MDN Reference

AudioContext
()
await
const audioContext: AudioContext
audioContext
.
AudioContext.resume(): Promise<void>

The resume() method of the AudioContext interface resumes the progression of time in an audio context that has previously been suspended.

MDN Reference

resume
()
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.audioContext: AudioContext
audioContext
})

The most important options control how much simulation work the world performs.

OptionDefaultDescription
audioContextrequiredThe AudioContext that all audio nodes created from this world will use.
maxSources32Maximum simultaneous sources.
frameSize1024Number of samples processed per audio frame.
simulationRate60How often direct simulation runs, in Hz.
reflectionRate10How often reflection simulation runs, in Hz.
quality'medium'Affects maxOcclusionSamples (low: 32, medium: 128, high: 256).
reflectionsfalseEnable and configure real-time reflections. See Reflections and Reverb.

If reflections are enabled, you must also specify their upper bounds at creation time:

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const audioContext: AudioContext
audioContext
= new
var AudioContext: new (contextOptions?: AudioContextOptions) => AudioContext

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.

MDN Reference

AudioContext
()
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.audioContext: AudioContext
audioContext
,
WorldOptions.reflections?: false | {
diffuseSamples?: number;
maxDuration?: number;
maxOrder?: number;
maxRays?: number;
} | undefined
reflections
: {
maxRays?: number | undefined
maxRays
: 4096,
maxDuration?: number | undefined
maxDuration
: 2,
maxOrder?: number | undefined
maxOrder
: 1,
diffuseSamples?: number | undefined
diffuseSamples
: 32,
},
})

These values reserve simulation memory. At runtime you can reduce rays, bounces, duration, and order via world.setReflectionSettings(), but you cannot exceed the initial maximums.

Direct-path simulation and reflection simulation are driven by world.step(delta). Call it every frame with the elapsed time in seconds. The world accumulates time internally and only runs the simulator when enough time has passed for the configured rate.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
import {
class Vector3

@example const a = new THREE.Vector3( 1, 0, 0 ); const b = new THREE.Vector3( 0, 1, 0 ); const c = new THREE.Vector3(); c.crossVectors( a, b );

Vector3
,
class Quaternion

Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages.

@example const quaternion = new THREE.Quaternion(); quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 ); const vector = new THREE.Vector3( 1, 0, 0 ); vector.applyQuaternion( quaternion );

Quaternion
} from 'three'
const
const audioContext: AudioContext
audioContext
= new
var AudioContext: new (contextOptions?: AudioContextOptions) => AudioContext

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode.

MDN Reference

AudioContext
()
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.audioContext: AudioContext
audioContext
})
const
const source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const
const animate: (delta: number) => void
animate
= (
delta: number
delta
: number) => {
function requestAnimationFrame(callback: FrameRequestCallback): number
requestAnimationFrame
(
const animate: (delta: number) => void
animate
)
const world: World
world
.
listener: Listener
listener
.
Listener.setPosition: (position: Vector3Like) => void
setPosition
(
const camera: {
position: Vector3;
quaternion: Quaternion;
}
camera
.
position: Vector3
position
)
const world: World
world
.
listener: Listener
listener
.
Listener.setOrientation: (orientation: QuaternionLike) => void
setOrientation
(
const camera: {
position: Vector3;
quaternion: Quaternion;
}
camera
.
quaternion: Quaternion
quaternion
)
const source: Source
source
.
Source.setPosition: (position: Vector3Like) => void
setPosition
(
const sourceObject: {
position: Vector3;
quaternion: Quaternion;
}
sourceObject
.
position: Vector3
position
)
const source: Source
source
.
Source.setOrientation: (orientation: QuaternionLike) => void
setOrientation
(
const sourceObject: {
position: Vector3;
quaternion: Quaternion;
}
sourceObject
.
quaternion: Quaternion
quaternion
)
const world: World
world
.
scene: AcousticSceneImpl
scene
.
AcousticSceneImpl.commit(): void
commit
()
const world: World
world
.
function step(delta: number): void
step
(
delta: number
delta
)
}
function requestAnimationFrame(callback: FrameRequestCallback): number
requestAnimationFrame
(
const animate: (delta: number) => void
animate
)

After creating a world with reflections enabled, you can reduce quality to save CPU or increase it temporarily for special moments:

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const world: World
world
.
function setReflectionSettings(settings: RuntimeSimulationSettings): void
setReflectionSettings
({
RuntimeSimulationSettings.rays?: number | undefined
rays
: 2048,
RuntimeSimulationSettings.bounces?: number | undefined
bounces
: 8,
RuntimeSimulationSettings.duration?: number | undefined
duration
: 1.5,
RuntimeSimulationSettings.order?: number | undefined
order
: 1,
RuntimeSimulationSettings.irradianceMinDistance?: number | undefined
irradianceMinDistance
: 1,
})

Each value is clamped to the maximum declared when the world was created.

A world holds native and Web Audio resources. Dispose it when the scene unloads to release WASM memory, audio nodes, and worklets.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const world: World
world
.
function dispose(): void
dispose
()