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.
Creating a world
Section titled “Creating 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.
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.
resume()
const const world: World
world = await function createWorld(options: WorldOptions): Promise<World>
createWorld({ WorldOptions.audioContext: AudioContext
audioContext })import { const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense, function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState } from 'react'import { function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas } from '@react-three/fiber'import { const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio } from 'three-steam-audio/react'
export const const App: () => JSX.Element
App = () => { const [const audioContext: AudioContext
audioContext] = useState<AudioContext>(initialState: AudioContext | (() => AudioContext)): [AudioContext, Dispatch<SetStateAction<AudioContext>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState(() => 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.
AudioContext())
return ( <function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas> <const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense SuspenseProps.fallback?: ReactNode
A fallback react tree to show when a Suspense child (like React.lazy) suspends
fallback={null}> <const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio audioContext: AudioContext
audioContext={const audioContext: AudioContext
audioContext}> {/* scene contents */} </const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio> </const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense> </function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas> )}<SteamAudio> creates and owns the world, and runs world.step(delta) automatically each frame. You can also pass an existing world with <SteamAudio world={world}> if you created it yourself.
World options
Section titled “World options”The most important options control how much simulation work the world performs.
| Option | Default | Description |
|---|---|---|
audioContext | required | The AudioContext that all audio nodes created from this world will use. |
maxSources | 32 | Maximum simultaneous sources. |
frameSize | 1024 | Number of samples processed per audio frame. |
simulationRate | 60 | How often direct simulation runs, in Hz. |
reflectionRate | 10 | How often reflection simulation runs, in Hz. |
quality | 'medium' | Affects maxOcclusionSamples (low: 32, medium: 128, high: 256). |
reflections | false | Enable 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.
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, },})import { const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense, function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState } from 'react'import { function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas } from '@react-three/fiber'import { const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio } from 'three-steam-audio/react'
export const const App: () => JSX.Element
App = () => { const [const audioContext: AudioContext
audioContext] = useState<AudioContext>(initialState: AudioContext | (() => AudioContext)): [AudioContext, Dispatch<SetStateAction<AudioContext>>] (+1 overload)
Returns a stateful value, and a function to update it.
useState(() => 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.
AudioContext())
return ( <function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas> <const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense SuspenseProps.fallback?: ReactNode
A fallback react tree to show when a Suspense child (like React.lazy) suspends
fallback={null}> <const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio audioContext: AudioContext
audioContext={const audioContext: AudioContext
audioContext} options?: Omit<WorldOptions, "audioContext"> | undefined
options={{ 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, }, }} > {/* scene contents */} </const SteamAudio: (props: SteamAudioProps) => import("react").JSX.Element
SteamAudio> </const Suspense: ExoticComponent<SuspenseProps>
Lets you display a fallback until its children have finished loading.
Suspense> </function Canvas(props: CanvasProps): import("react").JSX.Element
A DOM canvas which accepts threejs elements as children.
Canvas> )}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.
Stepping simulation
Section titled “Stepping simulation”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
Vector3, class Quaternion
Implementation of a quaternion. This is used for rotating things without incurring in the dreaded gimbal lock issue, amongst other advantages.
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.
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)// <SteamAudio> calls world.step(delta) automatically each frame.// You only need to call it yourself if you pass an owned world// into <SteamAudio world={world}>.Tuning reflections at runtime
Section titled “Tuning reflections at runtime”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,})import { const useSteamAudio: () => SteamAudioContextValue
useSteamAudio } from 'three-steam-audio/react'
export const const Settings: () => null
Settings = () => { const { const world: World
world } = function useSteamAudio(): SteamAudioContextValue
useSteamAudio()
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, })
return null}Each value is clamped to the maximum declared when the world was created.
Cleanup
Section titled “Cleanup”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()// <SteamAudio> disposes its owned world automatically when unmounted.// If you passed an existing world, you are responsible for disposing it.