AudioPlayerSnapshot

View source

Persistence data used to reconstruct an AudioPlayer component.

A snapshot captures the player’s graph-slot IDs and its asset handle as serializable data. It is not a live player, asset handle, or Web Audio node; restoring it creates a new component and delegates handle restoration to Handle#fromSnapshot.

Examples

import { World } from '@wimaengine/ecs'
import { AudioPlayer, AudioPlayerSnapshot } from '@wimaengine/audio'

const world = new World()
const serial = AudioPlayerSnapshot.serialize(
  AudioPlayer.toSnapshot(new AudioPlayer(), world)
)

if (AudioPlayerSnapshot.validateSerial(serial)) {
  const player = AudioPlayer.fromSnapshot(AudioPlayerSnapshot.deserialize(serial), world)
  console.log(player.audio) // undefined: the source player had no handle.
}

Constructor


constructor(sourceNode: number, attach: number, audio: HandleSnapshot)

View source

Creates persistence data from the player’s three stored fields.

The constructor assigns sourceNode, attach, and audio directly. It does not validate IDs, resolve the asset, or inspect an audio graph.

Parameters

NameDescriptionDefault
sourceNode——
attach——
audio——

Properties


sourceNode: number

View source

Graph slot ID recorded from the player’s AudioGraph.

This identifies a slot, not a native AudioBufferSourceNode. The ID may be undefined when no source slot had been assigned. Restoring the snapshot copies the recorded number but does not create or remap a slot in the destination world’s graph, so the value describes prior graph state rather than a guaranteed live slot.

attach: number

View source

Graph destination ID recorded from the player’s attach setting.

undefined means that the player would connect a newly reserved slot to the graph root. It is persisted as an ID only; restoration does not reconnect the graph edge or verify that the destination exists.

Methods


staticfromSnapshot(snapshot: AudioPlayerSnapshot, world: World, sceneAssetId?: AssetId): AudioPlayer

View source

Restores an AudioPlayer from this snapshot.

attach and sourceNode are copied into the new component, but no graph slot, native source, or graph edge is rebuilt. If audio is present, restoration follows Handle#fromSnapshot: path-backed data uses the world’s asset server, while a numeric scene-local locator needs sceneAssetId and a matching scene mapping. An unmapped numeric locator leaves the restored player’s audio undefined.

Parameters

NameDescriptionDefault
snapshot

Snapshot being restored.

—
world

World used to restore the optional handle.

—
sceneAssetId?

Scene ID for a numeric handle locator.

—

Returns

Restored player state.

staticserialize(value: AudioPlayerSnapshot): AudioPlayerSnapshotSerial

View source

Converts a snapshot into its plain serializable object.

The returned object has exactly sourceNode, attach, and audio fields; it is not a JSON string. The nested handle is flattened with HandleSnapshot.serialize when present. This method does not resolve a handle or validate that any graph ID is live.

Parameters

NameDescriptionDefault
value

Snapshot to serialize.

—

Returns

Plain snapshot data.

staticdeserialize(value: AudioPlayerSnapshotSerial, out?: AudioPlayerSnapshot): AudioPlayerSnapshot

View source

Restores snapshot fields from a plain serializable object.

This method copies the three fields and reuses out when supplied. A nested audio value is rehydrated with HandleSnapshot.deserialize; no asset handle or graph node is restored at this stage. Validate untrusted data with AudioPlayerSnapshot.validateSerial first.

Parameters

NameDescriptionDefault
value

Plain snapshot data.

—
out?

Snapshot object to populate.

...

Returns

Populated snapshot.

staticvalidateSerial(value: unknown): value is AudioPlayerSnapshotSerial

View source

Checks the fields and primitive types expected by AudioPlayerSnapshot.deserialize.

It requires all three fields. sourceNode and attach may be undefined or any JavaScript number, and audio may be undefined or a valid HandleSnapshot serial shape. This is a structural check only: it does not verify that a graph ID is live, that IDs belong to the same graph, or that the nested asset locator can be resolved in a world.

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

Whether the value is valid.