LittleJS 1.28.0

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, volume, rate, pan, loop, paused)engineAudio.js:520

Create a sound instance

sound Sound

The sound object

volume number optional, default 1

How much to scale volume by

rate number optional, default 1

The playback rate to use

pan number optional, default 0

How much to apply stereo panning

loop boolean optional, default false

Should the sound loop?

paused boolean optional, default false

Should the sound start paused?

// 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

Methods

Members

gainNode : GainNode|undefinedengineAudio.js:556

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

loop : booleanengineAudio.js:545

Should the sound loop

onendedCallback : AudioEndedCallback|undefinedengineAudio.js:569

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|undefinedengineAudio.js:565

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

pan : numberengineAudio.js:543

How much to apply stereo panning

pannerNode : StereoPannerNode|undefinedengineAudio.js:559

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

pausedTime : number|undefinedengineAudio.js:548

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

rate : numberengineAudio.js:541

The playback rate to use

The sound object

source : AudioBufferSourceNode|undefinedengineAudio.js:562

Source node of the audio, undefined while not playing

startOffset : numberengineAudio.js:553

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

startTime : number|undefinedengineAudio.js:551

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

volume : numberengineAudio.js:539

How much to scale volume by

Methods

getCurrentTime() → numberengineAudio.js:734

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
getDuration() → numberengineAudio.js:747

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)
getSource() → AudioBufferSourceNode|undefinedengineAudio.js:752

Get source of this sound instance

Returns
  • The source, or undefined while not playing
isPaused() → booleanengineAudio.js:728

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

Returns
  • True if not playing
isPlaying() → booleanengineAudio.js:723

Check if this instance is currently playing

Returns
  • True if playing

Pause this sound instance

Resume this sound instance

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

  • A looping sound can follow its source across the screen this way
pan number

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

setRate(rate)engineAudio.js:654

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
rate number

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

setVolume(volume, fadeTime)engineAudio.js:631

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
volume number
fadeTime number optional, default 0

Seconds to fade to the new volume over

start(offset)engineAudio.js:590

Start playing the sound instance from a place in the sound

offset number optional, default 0

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

stop(fadeTime)engineAudio.js:670

Stop this sound instance and reset position to the start

fadeTime number optional, default 0

Seconds to fade out over before stopping