Skip to content

Sound

An audio clip a sprite or the stage can play, such as a pop or a music loop. Sounds can be started and left to finish, played to completion, or shaped with effects like pitch and pan.

The Sound engine class. Most members are available as free functions; this page documents the underlying API for advanced use.

Sound.audioContext: AudioContext

The shared Web Audio context that all sounds play through, created on first use.

Since 1.0.0

Sound.connect(target: AudioNode): void

Routes the sound’s audio output into the given audio node (such as an effect chain).

Since 1.0.0

Sound.downloadMyAudioBuffer(): Promise<AudioBuffer | null>

Downloads and decodes the sound’s audio data so it is ready to play.

Since 1.0.0

Sound.duration: number

How long the sound lasts, in seconds (0 until the audio has downloaded).

Since 1.0.0

Sound.isConnectedTo(target: AudioNode): boolean

Checks whether the sound is currently routed to the given audio node.

Since 1.0.0

Sound.name: string

The name used to refer to this sound, as shown in the sound list.

Since 1.0.0

Sound.playUntilDone(): Yielding<void>

Plays the sound and waits until it has completely finished playing.

This is a Yielding generator that pauses until the sound finishes, so engine code awaits it with yield*.

Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.

Since 1.0.0

Example

yield* this.mySound.playUntilDone();
Sound.setPlaybackRate(value: number): void

Changes how fast the sound plays, which also changes its pitch.

Since 1.0.0

Sound.start(): Yielding<boolean>

Begins playing the sound and returns once it has actually started.

This is a Yielding generator that pauses until the sound has begun playing, so engine code awaits it with yield*.

Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.

Since 1.0.0

Example

yield* this.mySound.start();
Sound.stop(): void

Immediately stops the sound if it is currently playing.

Since 1.0.0

Sound.url: string

The location the sound’s audio data is downloaded from.

Since 1.0.0