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.
const world = new SpatialAudio(audioContext);
const falls = world.emitter(fallsBuffer, 320, 96, { range: 400, volume: 0.6 });
// each frame: world.update(hero);Constructors ​
Constructor ​
new SpatialAudio(context, destination?): SpatialAudio;Defined in: src/audio/spatial.ts:88
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
context | BaseAudioContext | undefined |
destination | AudioNode | context.destination |
Returns ​
SpatialAudio
Properties ​
context ​
readonly context: BaseAudioContext;Defined in: src/audio/spatial.ts:89
output ​
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 ​
get listener(): Point;Defined in: src/audio/spatial.ts:97
The listener's place as of the last update.
Returns ​
Point
size ​
Get Signature ​
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() ​
destroy(): void;Defined in: src/audio/spatial.ts:130
Returns ​
void
emitter() ​
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 ​
| Parameter | Type |
|---|---|
buffer | AudioBuffer |
x | number |
y | number |
options | SpatialEmitterOptions |
Returns ​
play() ​
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 ​
| Parameter | Type |
|---|---|
buffer | AudioBuffer |
x | number |
y | number |
options | Omit<SpatialEmitterOptions, "loop"> |
Returns ​
stopAll() ​
stopAll(fadeMs?): void;Defined in: src/audio/spatial.ts:126
Stops every emitter.
Parameters ​
| Parameter | Type | Default value |
|---|---|---|
fadeMs | number | 0 |
Returns ​
void
update() ​
update(listener?): void;Defined in: src/audio/spatial.ts:120
Moves the listener and re-mixes every emitter. Call once a frame.
Parameters ​
| Parameter | Type |
|---|---|
listener | Point |
Returns ​
void