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.unpause();
instance.stop();

Members

gainNode

Properties
TypeDescription
GainNode

Gain node for the sound

loop

Properties
TypeDescription
boolean

Should the sound loop

pan

Properties
TypeDescription
number

How much to apply stereo panning

pausedTime

Properties
TypeDescription
number

Timestamp for audio context when paused

rate

Properties
TypeDescription
number

The playback rate to use

sound

Properties
TypeDescription
Sound

The sound object

source

Properties
TypeDescription
AudioBufferSourceNode

Source node of the audio

startTime

Properties
TypeDescription
number

Timestamp for audio context when started

volume

Properties
TypeDescription
number

How much to scale volume by

Methods

getCurrentTime() → {number}

Get the current playback time in seconds

Returns:
  • Current playback time
Type: 
number

getDuration() → {number}

Get the total duration of this sound

Returns:
  • Total duration in seconds
Type: 
number

getSource() → {AudioBufferSourceNode}

Get source of this sound instance

Returns:
Type: 
AudioBufferSourceNode

isPaused() → {boolean}

Check if this instance is paused and was not stopped

Returns:
  • True if paused
Type: 
boolean

isPlaying() → {boolean}

Check if this instance is currently playing

Returns:
  • True if playing
Type: 
boolean

pause()

Pause this sound instance

resume()

Unpauses this sound instance

setVolume(volume)

Set the volume of this sound instance

Parameters:
NameTypeDescription
volumenumber

start(offsetopt)

Start playing the sound instance from the offset time

Parameters:
NameTypeAttributesDefaultDescription
offsetnumber<optional>
0

Offset in seconds to start playback from

stop()

Stop this sound instance and reset position to the start