Constructor
new SoundInstance(sound, volumeopt, rateopt, panopt, loopopt, pausedopt)
Create a sound instance
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
sound | Sound | The sound object | ||
volume | number | <optional> | 1 | How much to scale volume by |
rate | number | <optional> | 1 | The playback rate to use |
pan | number | <optional> | 0 | How much to apply stereo panning |
loop | boolean | <optional> | false | Should the sound loop? |
paused | boolean | <optional> | false | Should the sound start paused? |
- Source
// 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
- GainNode |
undefined
| Type | Description |
|---|---|
| GainNode | | Gain node for the sound, undefined once it is stopped or paused |
- Source
loop
Properties| Type | Description |
|---|---|
| boolean | Should the sound loop |
- Source
onendedCallback :AudioEndedCallback|undefined
- AudioEndedCallback |
undefined
| Type | Description |
|---|---|
| AudioEndedCallback | | 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 |
- Source
output :AudioNode|AudioEffectNodes|undefined
- AudioNode |
AudioEffectNodes | undefined
| Type | Description |
|---|---|
| AudioNode | | Node or effect to route this instance through, copied from the sound |
- Source
pan
Properties| Type | Description |
|---|---|
| number | How much to apply stereo panning |
- Source
pannerNode :StereoPannerNode|undefined
- StereoPannerNode |
undefined
| Type | Description |
|---|---|
| StereoPannerNode | | Stereo panner for the sound, undefined once it is stopped or paused |
- Source
pausedTime :number|undefined
- number |
undefined
| Type | Description |
|---|---|
| number | | Where it is in the sound while not playing, in the sound's own seconds, undefined while playing |
- Source
rate
Properties| Type | Description |
|---|---|
| number | The playback rate to use |
- Source
sound
Properties| Type | Description |
|---|---|
| Sound | The sound object |
- Source
source :AudioBufferSourceNode|undefined
- AudioBufferSourceNode |
undefined
| Type | Description |
|---|---|
| AudioBufferSourceNode | | Source node of the audio, undefined while not playing |
- Source
startOffset
Properties| Type | Description |
|---|---|
| number | Where it was in the sound at startTime, in the sound's own seconds |
- Source
startTime :number|undefined
- number |
undefined
| Type | Description |
|---|---|
| number | Audio context time its place was last taken at, while playing |
- Source
volume
Properties| Type | Description |
|---|---|
| number | How much to scale volume by |
- Source
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
- Source
- 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
- Source
- Length in seconds (0 if loading)
- Type:
- number
getSource() → {AudioBufferSourceNode|undefined}
Get source of this sound instance
- Source
- The source, or undefined while not playing
- Type:
- AudioBufferSourceNode |
undefined
isPaused() → {boolean}
Check if this instance is paused or stopped (not currently playing)
- Source
- True if not playing
- Type:
- boolean
isPlaying() → {boolean}
Check if this instance is currently playing
- Source
- True if playing
- Type:
- boolean
pause()
Pause this sound instance
- Source
resume()
Resume this sound instance
- Source
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
| Name | Type | Description |
|---|---|---|
pan | number | -1 is left, 0 is center, 1 is right, clamped to that range |
- Source
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
| Name | Type | Description |
|---|---|---|
rate | number | 1 is normal, 2 is twice as fast and an octave up |
- Source
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
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
volume | number | |||
fadeTime | number | <optional> | 0 | Seconds to fade to the new volume over |
- Source
start(offsetopt)
Start playing the sound instance from a place in the sound
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
offset | number | <optional> | 0 | Where to start in the sound, in its own seconds whatever the rate |
- Source
stop(fadeTimeopt)
Stop this sound instance and reset position to the start
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fadeTime | number | <optional> | 0 | Seconds to fade out over before stopping |
- Source