AudioCommands

View source

Provides direct audio playback controls as a world resource.

Use this resource to play, stop, mute, and unmute a resolved Audio asset. It does not resolve asset handles; use AudioAssets or Assets first when playback begins with a handle.

Examples

import { AudioCommands } from '@wimaengine/audio'

// `sound` is a loaded Audio asset, not an asset handle.
function playLoop(world, sound) {
  const commands = world.getResource(AudioCommands)
  const playback = commands.play(sound, true)

  // This disconnects the source from the command mix. It does not call the
  // native AudioBufferSourceNode.stop() method.
  return () => commands.stop(playback)
}

Constructor


constructor(ctx?: AudioContext)

View source

Creates a command resource with a master gain connected to the context’s destination.

When no context is supplied, a new AudioContext is created. The supplied context is stored as-is. The constructor does not load assets or start playback.

Parameters

NameDescriptionDefault
ctx?

Context to use, or a new context when omitted.

...

Methods


play(sound: Audio, loop?: boolean, delay?: number, offset?: number, duration?: number): number

View source

Creates and starts a buffer source for an audio asset.

The source reads sound.audiobuffer, connects to this resource’s master gain node, and forwards the timing values to AudioBufferSourceNode.start(when, offset, duration). delay is passed as when directly; it is not added to the context’s current time. The loop flag is assigned to the source after it is started.

The returned ID indexes the resource’s playback mapping, which points to the source in AudioCommands#audio. It is not the source-array index itself. A source that reaches its end is not removed or marked in this mapping, and the resource exposes no completion event for it.

Parameters

NameDescriptionDefault
sound

Decoded audio asset to play.

—
loop?

Whether the source should loop.

false
delay?

Value passed as Web Audio’s when argument.

0
offset?

Start offset in the audio buffer, in seconds.

0
duration?

Playback duration, in seconds.

sound.audiobuffer.duration

Returns

ID to pass to AudioCommands#stop.

stop(id: number): void

View source

Disconnects a playback source from the master gain node.

This is the only cleanup operation provided for a playback ID. It does not call the source’s native stop() method, remove it from AudioCommands#audio, or reclaim its array slot. It only sets the playback mapping to -1 after disconnecting the source. Pass a live ID returned by AudioCommands#play; an unknown or already-stopped ID does not no-op safely because no source can be disconnected.

Parameters

NameDescriptionDefault
id

ID returned by AudioCommands#play.

—

mute(): void

View source

Silences all command-driven playback through the master gain.

The current gain value is saved in internalVolume, then the gain is set to zero. Sources continue running and are neither stopped nor disconnected. Calling mute() again while already muted saves zero, so a later AudioCommands#unmute restores zero.

unmute(): void

View source

Restores the gain value saved by AudioCommands#mute.

Before the first mute, the saved value is 1. This changes only the master gain; it does not restart, stop, disconnect, or otherwise alter individual sources.

getContext(): AudioContext

View source

Returns the Web Audio context used by this resource.

The returned context may be suspended until the browser permits audio playback.

Returns

The command resource’s audio context.