Audio. Sound

Sound Object - Stores a sound for later

  • this can be used to load and play wave, mp3, and ogg files
  • it can also create sounds using the ZzFX sound generator
  • can attenuate and apply stereo panning to sounds
  • sound instance control with pause/resume capability

Create sounds using the ZzFX Sound Designer.

Constructor

new Sound(assetopt, randomnessopt, rangeopt, taperopt, onloadCallbackopt)

Create a sound object and cache the audio for later use

Parameters:
NameTypeAttributesDefaultDescription
assetstring | URL | Array<optional>

Filename or URL of an audio file, or a zzfx array

randomnessnumber<optional>

How much to randomize frequency each time sound plays, for zzfx sounds it overrides the array's own randomness, which is used if undefined

rangenumber<optional>
soundDefaultRange

World space max range of sound

tapernumber<optional>
soundDefaultTaper

At what percentage of range should it start tapering

onloadCallbackSoundLoadCallback<optional>

callback function to call when sound is loaded

Example
// load an audio asset file
const sound_example = new Sound('sound.mp3');

// create a zzfx sound
const sound_example = new Sound([.5,.5]);

// play a sound
sound_example.play();

Members

loadedPercent

Properties
TypeDescription
number

Percentage of this sound currently loaded, sounds fetched from a url stay at 0 until decoding completes

onloadCallback :SoundLoadCallback|undefined

Type:
  • SoundLoadCallback | undefined
Properties
TypeDescription
SoundLoadCallback | undefined

function to call when sound is loaded

output :AudioNode|AudioEffectNodes|undefined

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

Node or effect to route every play of this sound through instead of the master gain

  • Where this sound's audio goes, unlike AudioEffect.output which is an effect's own node, effects chain with connect()

randomness :number

Type:
  • number
Properties
TypeDescription
number

How much to randomize frequency each time sound plays

range

Properties
TypeDescription
number

World space max range of sound

sampleBuffer :AudioBuffer|undefined

Type:
  • AudioBuffer | undefined
Properties
TypeDescription
AudioBuffer | undefined

Decoded audio shared by every play of this sound

sampleChannels

Sample data for each channel Sounds keep their samples in an audio buffer, so reading this rebuilds the arrays from it and caches them. The copies are safe to hold onto, playing a sound detaches the buffer's own channel arrays.

sampleChannels

sampleLength

Properties
TypeDescription
number

How many samples per channel this sound has

sampleRate

Properties
TypeDescription
number

Sample rate for this sound

taper

Properties
TypeDescription
number

At what percentage of range should it start tapering

Methods

buildSampleBuffer()

Move this sound's samples into an audio buffer that every play can share Does nothing if there is already a buffer or no samples to build one from

getDuration() → {number}

Get how long this sound is in seconds

Returns:
  • How long the sound is in seconds (0 if loading)
Type: 
number

isLoaded() → {boolean}

Check if sound is loaded, for sounds fetched from a url

Returns:
  • True if sound is loaded and ready to play
Type: 
boolean

(async) loadSound(filename) → {Promise}

Loads a sound from a URL and decodes it into sample data.

Parameters:
NameTypeDescription
filenamestring
Returns:
Type: 
Promise

play(posopt, volumeopt, pitchopt, randomnessScaleopt, loopopt, pausedopt) → {SoundInstance|undefined}

Play the sound

  • Browsers hold audio until the first user input, a sound played before it returns a paused instance that starts on its own once audio runs, unless paused or stopped first; a one shot that would have ended by then is dropped, and only the newest play of each sound waits, so a sound played every frame starts once
Parameters:
NameTypeAttributesDefaultDescription
posVector2<optional>

World space position to play the sound if any

volumenumber<optional>
1

How much to scale volume by

pitchnumber<optional>
1

How much to scale pitch by

randomnessScalenumber<optional>
1

How much to scale pitch randomness

loopboolean<optional>
false

Should the sound loop?

pausedboolean<optional>
false

Should the sound start paused

Returns:
  • The sound instance, or undefined if sound is disabled, not loaded, out of range, or running in headless mode
Type: 
SoundInstance | undefined

playLoop(posopt, volumeopt, pitchopt, randomnessScaleopt, pausedopt) → {SoundInstance|undefined}

Play the sound on a loop, the same as play with loop on; stop or change it through the SoundInstance returned

Parameters:
NameTypeAttributesDefaultDescription
posVector2<optional>

World space position to play the sound if any

volumenumber<optional>
1

How much to scale volume by

pitchnumber<optional>
1

How much to scale pitch by

randomnessScalenumber<optional>
1

How much to scale pitch randomness

pausedboolean<optional>
false

Should the sound start paused

Returns:
  • The sound instance, or undefined if sound is disabled, not loaded, out of range, or running in headless mode
Type: 
SoundInstance | undefined

playMusic(volumeopt, loopopt, pausedopt) → {SoundInstance|undefined}

Play a music track that loops by default

Parameters:
NameTypeAttributesDefaultDescription
volumenumber<optional>
1

Volume to play the music at

loopboolean<optional>
true

Should the music loop?

pausedboolean<optional>
false

Should the music start paused

Returns:
  • The sound instance, or undefined if sound is disabled, not loaded, or running in headless mode
Type: 
SoundInstance | undefined

playNote(semitoneOffsetopt, posopt, volumeopt) → {SoundInstance|undefined}

Play the sound as a musical note with a semitone offset This can be used to play music with chromatic scales

Parameters:
NameTypeAttributesDefaultDescription
semitoneOffsetnumber<optional>
0

How many semitones to offset pitch

posVector2<optional>

World space position to play the sound if any

volumenumber<optional>
1

How much to scale volume by

Returns:
  • The sound instance, or undefined if sound is disabled, not loaded, out of range, or running in headless mode
Type: 
SoundInstance | undefined