Skip to content

Sources

A Source represents a single emitting sound source in the acoustic scene. It stores position, orientation, and simulation settings, and it produces a SteamAudioNode that you connect into the Web Audio graph.

Create a source from a world, then create an audio node for it.

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
})
const
const source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
({
SourceSettings.hrtf?: boolean | undefined
hrtf
: true,
SourceSettings.directSimulation?: boolean | DirectSimulationSettings | undefined
directSimulation
: {
DirectSimulationSettings.airAbsorption?: boolean | undefined
airAbsorption
: true,
DirectSimulationSettings.occlusion?: false | "raycast" | "volumetric" | undefined
occlusion
: 'raycast',
DirectSimulationSettings.transmission?: false | {
type?: "frequency-dependent" | "frequency-independent";
} | undefined
transmission
: {
type?: "frequency-dependent" | "frequency-independent" | undefined
type
: 'frequency-dependent' },
},
})
const
const node: SteamAudioNode
node
=
const world: World
world
.
function createNode(sourceValue: Source): SteamAudioNode
createNode
(
const source: Source
source
)
const
const oscillator: OscillatorNode
oscillator
= new
var OscillatorNode: new (context: BaseAudioContext, options?: OscillatorOptions) => OscillatorNode

The OscillatorNode interface represents a periodic waveform, such as a sine wave. It is an AudioScheduledSourceNode audio-processing module that causes a specified frequency of a given wave to be created—in effect, a constant tone.

MDN Reference

OscillatorNode
(
const audioContext: AudioContext
audioContext
, {
OscillatorOptions.frequency?: number | undefined
frequency
: 440 })
const oscillator: OscillatorNode
oscillator
.
AudioNode.connect(destinationNode: AudioNode, output?: number, input?: number): AudioNode (+1 overload)

The connect() method of the AudioNode interface lets you connect one of the node's outputs to a target, which may be either another AudioNode (thereby directing the sound data to the specified node) or an AudioParam, so that the node's output data is automatically used to change the value of that parameter over time.

MDN Reference

connect
(
const node: SteamAudioNode
node
)
const node: SteamAudioNode
node
.
AudioNode.connect(destinationNode: AudioNode, output?: number, input?: number): AudioNode (+1 overload)

The connect() method of the AudioNode interface lets you connect one of the node's outputs to a target, which may be either another AudioNode (thereby directing the sound data to the specified node) or an AudioParam, so that the node's output data is automatically used to change the value of that parameter over time.

MDN Reference

connect
(
const audioContext: AudioContext
audioContext
.
BaseAudioContext.destination: AudioDestinationNode

The destination property of the BaseAudioContext interface returns an AudioDestinationNode representing the final destination of all audio in the context. It often represents an actual audio-rendering device such as your device's speakers.

MDN Reference

destination
)
const oscillator: OscillatorNode
oscillator
.
AudioScheduledSourceNode.start(when?: number): void

The start() method on AudioScheduledSourceNode schedules a sound to begin playback at the specified time. If no time is specified, then the sound begins playing immediately.

MDN Reference

start
()

Sources are created with an upper bound set by world.maxSources. Creating more sources than the world allows throws a SteamAudioError.

The simulator needs the source position and orientation every frame. Three Steam Audio uses the same right-handed, Y-up coordinate system as Three.js.

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 world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const source: Source
source
.
Source.setPosition: (position: Vector3Like) => void
setPosition
(
const object: {
position: Vector3;
quaternion: Quaternion;
}
object
.
position: Vector3
position
)
const source: Source
source
.
Source.setOrientation: (orientation: QuaternionLike) => void
setOrientation
(
const object: {
position: Vector3;
quaternion: Quaternion;
}
object
.
quaternion: Quaternion
quaternion
)
// Or both at once:
const source: Source
source
.
Source.setTransform: (position: Vector3Like, orientation: QuaternionLike) => void
setTransform
(
const object: {
position: Vector3;
quaternion: Quaternion;
}
object
.
position: Vector3
position
,
const object: {
position: Vector3;
quaternion: Quaternion;
}
object
.
quaternion: Quaternion
quaternion
)

Direct simulation covers everything that happens along the straight line from source to listener:

By default, a physics-based inverse-distance model is used. You can disable it, tune the minimum distance, or provide a custom curve:

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
// Inverse distance, custom minimum distance.
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
distanceAttenuation?: false | DistanceAttenuationSettings | undefined
distanceAttenuation
: {
model: "inverse"
model
: 'inverse',
minDistance?: number | undefined
minDistance
: 1 },
})
// Custom curve.
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
distanceAttenuation?: false | DistanceAttenuationSettings | undefined
distanceAttenuation
: {
model: "curve"
model
: 'curve',
minDistance: number
minDistance
: 1,
maxDistance: number
maxDistance
: 18,
curve: (distance: number) => number
curve
:
distance: number
distance
=>
var Math: Math

An intrinsic object that provides basic mathematics functionality and constants.

Math
.
Math.max(...values: number[]): number

Returns the larger of a set of supplied numeric expressions.

@paramvalues Numeric expressions to be evaluated.

max
(0, 1 -
distance: number
distance
/ 18) ** 1.5,
},
})
// Disable entirely.
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
distanceAttenuation?: false | DistanceAttenuationSettings | undefined
distanceAttenuation
: false })

Air absorption models how high frequencies attenuate faster than low frequencies over distance. Enable the default physics-based model or supply your own coefficients or curves.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
directSimulation?: boolean | DirectSimulationSettings | undefined
directSimulation
: {
DirectSimulationSettings.airAbsorption?: boolean | undefined
airAbsorption
: true,
DirectSimulationSettings.airAbsorptionModel?: AirAbsorptionSettings | undefined
airAbsorptionModel
: {
model?: "default" | undefined
model
: 'default' },
},
})

A source can emit sound with a directional pattern. Three Steam Audio supports a weighted dipole pattern controlled by dipoleWeight and dipolePower.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
directivity?: {
dipolePower?: number;
dipoleWeight?: number;
} | undefined
directivity
: {
dipoleWeight?: number | undefined
dipoleWeight
: 0.5,
dipolePower?: number | undefined
dipolePower
: 2,
},
})

Occlusion traces rays from listener to source to determine how much of the source is visible. Transmission applies a material-dependent filter to the occluded portion of the sound.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
directSimulation?: boolean | DirectSimulationSettings | undefined
directSimulation
: {
DirectSimulationSettings.occlusion?: false | "raycast" | "volumetric" | undefined
occlusion
: 'raycast',
// Or volumetric occlusion:
// occlusion: 'volumetric',
// occlusionRadius: 1,
// occlusionSamples: 16,
DirectSimulationSettings.transmission?: false | {
type?: "frequency-dependent" | "frequency-independent";
} | undefined
transmission
: {
type?: "frequency-dependent" | "frequency-independent" | undefined
type
: 'frequency-dependent' },
},
})

Blend between fully spatialized (HRTF) and unspatialized audio. A value of 1 is fully spatialized; 0 is unspatialized.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const source: Source
source
.
Source.setSettings: (settings: Partial<SourceSettings>) => void
setSettings
({
spatialBlend?: number | undefined
spatialBlend
: 0.75 })

You can read the latest direct simulation results from a source. This is useful for debugging, UI meters, or driving visual effects.

import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const
const outputs: DirectOutputs
outputs
=
const source: Source
source
.
Source.getDirectOutputs: (target?: DirectOutputs) => DirectOutputs
getDirectOutputs
()
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const outputs: DirectOutputs
outputs
.
DirectOutputs.distanceAttenuation: number
distanceAttenuation
)
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const outputs: DirectOutputs
outputs
.
DirectOutputs.airAbsorption: [number, number, number]
airAbsorption
) // [low, mid, high]
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const outputs: DirectOutputs
outputs
.
DirectOutputs.occlusion: number
occlusion
)
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const outputs: DirectOutputs
outputs
.
DirectOutputs.transmission: [number, number, number]
transmission
) // [low, mid, high]
var console: Console
console
.
Console.log(...data: any[]): void

The console.log() static method outputs a message to the console.

MDN Reference

log
(
const outputs: DirectOutputs
outputs
.
DirectOutputs.directivity: number
directivity
)
import {
const createWorld: (options: WorldOptions) => Promise<World>
createWorld
} from 'three-steam-audio'
const
const world: World
world
= await
function createWorld(options: WorldOptions): Promise<World>
createWorld
({
WorldOptions.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 source: Source
source
=
const world: World
world
.
function createSource(settings?: SourceSettings): Source
createSource
()
const
const node: SteamAudioNode
node
=
const world: World
world
.
function createNode(sourceValue: Source): SteamAudioNode
createNode
(
const source: Source
source
)
const node: SteamAudioNode
node
.
SteamAudioNode.dispose(): void
dispose
() // disconnects the audio node
const source: Source
source
.
Source.dispose: () => void
dispose
() // releases the source