AudioGraph

View source

World resource that provides a browser AudioContext and directed graph for connecting native Web Audio nodes.

AudioPlugin makes this resource available in the app’s default world. Use it to create native nodes, reserve graph slots, and connect audio sources to AudioDestinationNode or another graph node. Retrieve the installed resource with world.getResource(AudioGraph) rather than constructing a second graph for normal playback.

Examples

import { AudioGraph } from '@wimaengine/audio'

// Read the graph resource from the world's installed resources.
function connectGainToOutput(world) {
  const graph = world.getResource(AudioGraph)
  const gain = graph.add(graph.getContext().createGain())

  graph.connect(gain, graph.getRoot())
  return gain
}

Constructor


constructor(options?: AudioContextOptions)

View source

Creates an audio graph with a fresh AudioContext.

The context’s AudioContext#destination is added as the first graph node and becomes the value returned by getRoot. Additional slots are created when callers use add. This constructor is browser-only because it immediately creates an AudioContext.

A global pointerdown listener calls AudioContext.resume() to handle browser autoplay restrictions. The listener remains installed until the context reports running after an interaction.

Parameters

NameDescriptionDefault
options?——

Properties


graph: Graph<AudioGraphNode, undefined>

View source

Directed graph of native Web Audio nodes and their outgoing connections. A node weight can be undefined while a source slot is reserved. The public methods maintain the edge metadata and make native AudioNode.connect() calls when the relevant slot weights exist.

Default: ...

Methods


getRoot(): number

View source

Returns the graph ID of the Web Audio context destination.

Pass this ID as the destination of connect when a source should play directly through the browser’s audio output.

getContext(): AudioContext

View source

Returns the Web Audio context owned by this graph.

Use the context to create native nodes before registering them with add, or to create replacement nodes for update.

add(node: AudioGraphNode): number

View source

Adds a native Web Audio node or an empty placeholder slot to the graph.

The returned numeric ID is stable for this slot while it remains in the graph. Pass undefined to reserve a slot before the native source exists; its graph edges can then be created with connect and completed later with update.

Parameters

NameDescriptionDefault
node

Native node to store, or undefined for a placeholder.

—

Returns

The new graph-node ID.

connect(from: number, to: number): void

View source

Records an outgoing graph edge and connects the current native nodes.

Both IDs must identify slots already returned by add, or the root returned by getRoot. The graph edge is stored even when either slot currently contains undefined. Native connect() is called only when both nodes exist. If the source slot is later populated with update, that replacement is connected to its existing outgoing neighbours; populating only a destination slot does not rewire its incoming neighbours.

Parameters

NameDescriptionDefault
from

ID of the source graph slot.

—
to

ID of the destination graph slot.

—

get(id: number): AudioGraphNode

View source

Returns the native node currently stored at a graph ID.

A reserved slot, an unknown ID, or a slot cleared with update returns undefined.

Parameters

NameDescriptionDefault
id

Graph-node ID to look up.

—

Returns

The stored native node, or undefined.

update(id: number, value: AudioGraphNode): void

View source

Replaces the native node stored at a graph ID.

If the ID is not present, this method does nothing. When a previous node exists, an AudioScheduledSourceNode is stopped and then the previous node is disconnected. A replacement node is connected to each existing outgoing neighbour that currently has a native node before it is stored. Incoming edges are preserved but are not reconnected by this method. Passing undefined clears the slot while preserving its graph edges, so the ID can be reused later. This resource does not expose graph node removal.

Parameters

NameDescriptionDefault
id

Graph-node ID to update.

—
value

Replacement native node, or undefined.

—