Audio. SoundInstance

Sound Instance - Wraps an AudioBufferSourceNode for individual sound control Represents a single playing instance of a sound with pause/resume capabilities

Constructor

new SoundInstance(sound, volumeopt, rateopt, panopt, loopopt, pausedopt)

Create a sound instance

Parameters:
NameTypeAttributesDefaultDescription
soundSound

The sound object

volumenumber<optional>
1

How much to scale volume by

ratenumber<optional>
1

The playback rate to use

pannumber<optional>
0

How much to apply stereo panning

loopboolean<optional>
false

Should the sound loop?

pausedboolean<optional>
false

Should the sound start paused?

Example
// Play a sound and get an instance for control
const jumpSound = new Sound([.5,.5,220]);
const instance = jumpSound.play();

// Control the individual instance
instance.setVolume(.5);
instance.pause();
instance.resume();
instance.stop();

Members

gainNode :GainNode|undefined

Type:
  • GainNode | undefined
Properties
TypeDescription
GainNode | undefined

Gain node for the sound, undefined once it is stopped or paused

loop

Properties
TypeDescription
boolean

Should the sound loop

onendedCallback :AudioEndedCallback|undefined

Type:
  • AudioEndedCallback | undefined
Properties
TypeDescription
AudioEndedCallback | undefined

Called when this instance plays to its end, not when it is stopped or paused; it is read when the sound ends, so it can be set at any time

output :AudioNode|AudioEffectNodes|undefined

Type:
  • AudioNode | AudioEffectNodes | undefined
Properties
TypeDescription
AudioNode | AudioEffectNodes | undefined

Node or effect to route this instance through, copied from the sound

pan

Properties
TypeDescription
number

How much to apply stereo panning

pannerNode :StereoPannerNode|undefined

Type:
  • StereoPannerNode | undefined
Properties
TypeDescription
StereoPannerNode | undefined

Stereo panner for the sound, undefined once it is stopped or paused

pausedTime :number|undefined

Type:
  • number | undefined
Properties
TypeDescription
number | undefined

Where it is in the sound while not playing, in the sound's own seconds, undefined while playing

rate

Properties
TypeDescription
number

The playback rate to use

sound

Properties
TypeDescription
Sound

The sound object

source :AudioBufferSourceNode|undefined

Type:
  • AudioBufferSourceNode | undefined
Properties
TypeDescription
AudioBufferSourceNode | undefined

Source node of the audio, undefined while not playing

startOffset

Properties
TypeDescription
number

Where it was in the sound at startTime, in the sound's own seconds

startTime :number|undefined

Type:
  • number | undefined
Properties
TypeDescription
number

Audio context time its place was last taken at, while playing

volume

Properties
TypeDescription
number

How much to scale volume by

Methods

getCurrentTime() → {number}

Get where it is in the sound, in the sound's own seconds: at a rate of 2 it moves two seconds for each one that passes, and at 0 it stays put

Returns:
  • Seconds into the sound
Type: 
number

getDuration() → {number}

Get the length of the sound in its own seconds, the same at any rate; divide by the rate for how long it takes to play

Returns:
  • Length in seconds (0 if loading)
Type: 
number

getSource() → {AudioBufferSourceNode|undefined}

Get source of this sound instance

Returns:
  • The source, or undefined while not playing
Type: 
AudioBufferSourceNode | undefined

isPaused() → {boolean}

Check if this instance is paused or stopped (not currently playing)

Returns:
  • True if not playing
Type: 
boolean

isPlaying() → {boolean}

Check if this instance is currently playing

Returns:
  • True if playing
Type: 
boolean

pause()

Pause this sound instance

resume()

Resume this sound instance

setPan(pan)

Set the stereo pan of this sound instance, while it plays too

  • A looping sound can follow its source across the screen this way
Parameters:
NameTypeDescription
pannumber

-1 is left, 0 is center, 1 is right, clamped to that range

setRate(rate)

Set the playback rate of this sound instance, its speed and pitch, while it plays

  • A looping sound can follow something smoothly this way, like an engine with the speed
  • A rate of 0 freezes the sound in place, and it carries on from there when the rate comes back
Parameters:
NameTypeDescription
ratenumber

1 is normal, 2 is twice as fast and an octave up

setVolume(volume, fadeTimeopt)

Set the volume of this sound instance, with an optional fade to it

  • A fade ducks music under dialogue or cross fades two tracks without a click
Parameters:
NameTypeAttributesDefaultDescription
volumenumber
fadeTimenumber<optional>
0

Seconds to fade to the new volume over

start(offsetopt)

Start playing the sound instance from a place in the sound

Parameters:
NameTypeAttributesDefaultDescription
offsetnumber<optional>
0

Where to start in the sound, in its own seconds whatever the rate

stop(fadeTimeopt)

Stop this sound instance and reset position to the start

Parameters:
NameTypeAttributesDefaultDescription
fadeTimenumber<optional>
0

Seconds to fade out over before stopping