Skip to content

1681-engine / index / SpatialAudio

Class: SpatialAudio ​

Defined in: src/audio/spatial.ts:82

Sounds placed in the world, heard from a listener that moves: a waterfall, a campfire, a machine humming, or a one-shot at a spot. Each emitter is panned to its side, fades with distance and loses its highs far away (spatialMix), eased each update so a moving source does not click. A loop out of range stops its source to save the voice and starts again at the point it would have reached, so it seems to have played on.

ts
const world = new SpatialAudio(audioContext);
const falls = world.emitter(fallsBuffer, 320, 96, { range: 400, volume: 0.6 });
// each frame: world.update(hero);

Constructors ​

Constructor ​

ts
new SpatialAudio(context, destination?): SpatialAudio;

Defined in: src/audio/spatial.ts:88

Parameters ​

ParameterTypeDefault value
contextBaseAudioContextundefined
destinationAudioNodecontext.destination

Returns ​

SpatialAudio

Properties ​

context ​

ts
readonly context: BaseAudioContext;

Defined in: src/audio/spatial.ts:89


output ​

ts
readonly output: GainNode;

Defined in: src/audio/spatial.ts:84

Every emitter's gain goes through this: the game's volume setting.

Accessors ​

listener ​

Get Signature ​

ts
get listener(): Point;

Defined in: src/audio/spatial.ts:97

The listener's place as of the last update.

Returns ​

Point


size ​

Get Signature ​

ts
get size(): number;

Defined in: src/audio/spatial.ts:102

How many emitters are alive (playing or waiting out of range).

Returns ​

number

Methods ​

destroy() ​

ts
destroy(): void;

Defined in: src/audio/spatial.ts:130

Returns ​

void


emitter() ​

ts
emitter(
   buffer, 
   x, 
   y, 
   options?
): SpatialEmitter;

Defined in: src/audio/spatial.ts:107

A sound at (x, y), looping by default. Starts at once when in range of the listener.

Parameters ​

ParameterType
bufferAudioBuffer
xnumber
ynumber
optionsSpatialEmitterOptions

Returns ​

SpatialEmitter


play() ​

ts
play(
   buffer, 
   x, 
   y, 
   options?
): SpatialEmitter;

Defined in: src/audio/spatial.ts:115

A one-shot at (x, y), dropped when it ends; skipped (and done at once) when out of range.

Parameters ​

ParameterType
bufferAudioBuffer
xnumber
ynumber
optionsOmit<SpatialEmitterOptions, "loop">

Returns ​

SpatialEmitter


stopAll() ​

ts
stopAll(fadeMs?): void;

Defined in: src/audio/spatial.ts:126

Stops every emitter.

Parameters ​

ParameterTypeDefault value
fadeMsnumber0

Returns ​

void


update() ​

ts
update(listener?): void;

Defined in: src/audio/spatial.ts:120

Moves the listener and re-mixes every emitter. Call once a frame.

Parameters ​

ParameterType
listenerPoint

Returns ​

void

Released under the MIT License.