AudioOscillator

View source

Stores waveform settings and playback state for procedural Web Audio on an ECS entity.

Pair it with Timer to control when playback starts, pauses, or repeats. The component supplies the waveform, pitch, detuning, graph destination, and playback state used to create a browser oscillator.

Examples

import { World } from '@wimaengine/ecs'
import { AudioOscillator } from '@wimaengine/audio'
import { Timer, TimerMode } from '@wimaengine/time'

const world = new World()
const entity = world.spawn([
  new AudioOscillator({ frequency: 440 }),
  new Timer({ duration: 1, mode: TimerMode.Repeat })
])

// The timer controls when the oscillator starts and, in Once mode, when it
// stops.
console.log(world.get(entity, AudioOscillator).frequency) // 440

Constructor


constructor(options?: AudioOscillatorOptions)

View source

Creates an oscillator component with the supplied initial waveform, frequency, detuning, and graph destination. sourceNode starts undefined; pair the component with a Timer when playback should be controlled by timer state. The constructor stores the settings without validating waveform membership or numeric ranges.

Parameters

NameDescriptionDefault
options?

Waveform, frequency, detune, and destination graph node.

{}

Properties


sourceNode: number

View source

ID of this oscillator’s slot in the world’s AudioGraph. It is undefined until the component is associated with a graph slot. The slot can contain undefined while playback is paused or complete, so this value identifies the slot rather than the native OscillatorNode itself. With a paired Timer, playback changes create or clear the native oscillator stored in the slot. A paused or completed TimerMode.Once timer clears the slot’s current oscillator; a TimerMode.Repeat timer keeps it available until playback is paused or stopped.

attach: number

View source

Optional AudioGraph node that receives this oscillator. When undefined, the reserved slot connects to the graph’s destination root. The value is used when sourceNode is first assigned; changing it later does not rewire the existing graph edge.

detune: number

View source

Detuning passed to the Web Audio oscillator, in cents. The default is 0. This value is read when a new native oscillator is created; changing it does not update an already active node.

frequency: number

View source

Oscillator frequency passed to Web Audio, in hertz. The default is 440. This value is read when a new native oscillator is created; changing it does not update an already active node.

Methods


staticcopy(source: AudioOscillator, target?: AudioOscillator): AudioOscillator

View source

Copies oscillator settings and runtime graph-slot state into a component. All five fields are assigned as primitive values. This method does not create a graph node, reconnect attach, or duplicate a native oscillator.

Parameters

NameDescriptionDefault
source

Component to copy.

—
target?

Component to populate.

...

Returns

The populated target component.

staticclone(target: AudioOscillator): AudioOscillator

View source

Creates a component with the source’s five field values. This delegates to AudioOscillator.copy; sourceNode remains only an ID and no Web Audio node is duplicated.

Parameters

NameDescriptionDefault
target

Component to clone.

—

Returns

A new component with copied field values.

staticserialize(value: AudioOscillator): AudioOscillatorSerial

View source

Converts the component into its serialized field object. sourceNode is included as-is even though it identifies runtime graph state rather than a portable Web Audio node.

Parameters

NameDescriptionDefault
value

Component to serialize.

—

Returns

Serialized oscillator fields.

staticdeserialize(value: AudioOscillatorSerial, out?: AudioOscillator): AudioOscillator

View source

Restores serialized fields into a new or supplied component. Deserialization only assigns values; it does not validate them, rebuild a graph slot, or reconnect the restored sourceNode.

Parameters

NameDescriptionDefault
value

Serialized oscillator fields.

—
out?

Component to fill.

...

Returns

The restored component.

staticvalidateSerial(value: unknown): value is AudioOscillatorSerial

View source

Checks the field shape and primitive types accepted by AudioOscillator.deserialize. The check permits undefined or numeric node IDs, requires numeric waveform, detune, and frequency fields, and does not enforce finite values, numeric ranges, or valid AudioOscillatorType members.

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

Whether value has a valid serialized shape.