SoundInstanceOne playing instance of a Sound. Returned by sound.play(), source.play() and renderer.playAt().
Keep the instance when you want to control the sound while it plays: stop a loop, change its volume, or let it follow a moving object. For fire-and-forget sounds you can ignore it.
Once the instance has ended, its audio nodes are released and its spatial source (from playAt()) is reused for other sounds. From then on the instance can no longer be controlled: source returns null and the setters do nothing.
// A rocket that whooshes while it flies.
const whoosh = renderer.playAt(rocketLoop, rocket.position, { loop: true });
function frame() {
whoosh?.setPosition(rocket.x, rocket.y, rocket.z);
}
function onImpact() {
whoosh?.stop(0.1);
renderer.playAt(explosion, rocket.position);
}
Instances are created by Sound; you do not create them yourself.
id: stringA unique id, automatically generated when the instance is started.
sound: SoundThe Sound this instance plays.
startTime: numberAudioContext time at which the instance started (or will start, when when was given).
loop: booleanWhether the instance loops.
stop(fadeTime?: number): voidStops the instance after a short fade. A fade time of 0 stops it immediately. A suspended loop (see Loop virtualization) ends at once, since it has no audio node to fade. Stopping an instance that has already stopped does nothing.
fadeTime?: number - Fade-out in seconds. Defaults to 0.02.voidsetVolume(volume: number): SoundInstanceChanges the volume of this instance (floored at 0). For an instance from playAt() this sets the volume of its spatial source, otherwise the instance's own gain is smoothed to the new value.
volume: numberSoundInstance - The same instance.setPosition(x: number, y: number, z?: number): SoundInstanceMoves the spatial source of an instance started with renderer.playAt(). Does nothing for instances from sound.play() (not positioned) and source.play() (they move with their source), and for instances that have ended.
x: numbery: numberz?: number - Defaults to the current z.SoundInstance - The same instance.onEnded(callback: () => void): SoundInstanceCalls callback once the instance has ended, either because the sound has finished or because it was stopped. If the instance has already ended, the callback is called immediately.
callback: () => voidSoundInstance - The same instance.suspend(): voidReleases the AudioBufferSourceNode of a looping instance. The loop keeps running "virtually". Does nothing for one-shots. Called by the renderer; see Loop virtualization.
voidresume(): voidRecreates the AudioBufferSourceNode of a suspended loop, at the position the loop would have been at. Called by the renderer.
voidget isPlaying(): booleanfalse once the instance has been stopped or has ended. A suspended loop counts as playing.
get isSuspended(): booleanWhether the instance is a loop whose audio node is released because nobody can hear it.
get volume(): numberThe current volume of this instance, including the random volumeVariation of its sound.
get source(): SpatialAudioSource | nullThe SpatialAudioSource this instance is heard through. null for instances from sound.play(), and after the instance has ended.
get loudness(): numberThe loudness used by steal: "quietest": the rendered gain (volume x distance attenuation) for spatial instances, the volume otherwise.
A looping instance on a spatial source keeps an AudioBufferSourceNode alive for as long as it plays. With hundreds of ambience loops spread over a map, most of them are too far away to hear.
When a source has been without a voice for loopVirtualizationDelay seconds (default 0.5), the renderer suspends its looping instances: the AudioBufferSourceNode is stopped and released. A source has no voice when it is inaudible, or virtual because of the voice budget. Once the source gets a voice again, the renderer resumes them: a new node is started at the position the loop would have been at, so the loop stays in time. A loop of 2 seconds that was suspended 1.1 seconds after it started continues at 1.1 seconds.
One-shots are never suspended; they are short enough to play out. Set loopVirtualizationDelay: Infinity in the renderer options to disable it. renderer.getStats().suspendedLoops shows how many loops are suspended.
Loops of AudioClips attached to a source are not virtualized. Use Sound for ambience loops in a game world.
This class does not emit any events. Use onEnded().