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 | Array<optional>

Filename of audio file or zzfx array

randomnessnumber<optional>

How much to randomize frequency each time sound plays, for zzfx sounds the zzfx default 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

onloadCallback

Properties
TypeDescription
SoundLoadCallback

function to call when sound is loaded

randomness

Properties
TypeDescription
number

How much to randomize frequency each time sound plays

range

Properties
TypeDescription
number

World space max range of sound

sampleRate

Properties
TypeDescription
number

Sample rate for this sound

taper

Properties
TypeDescription
number

At what percentage of range should it start tapering

Methods

getDuration() → {number}

Get how long this sound is in seconds

Returns:
  • How long the sound is in seconds (undefined 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.<void>}

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

Parameters:
NameTypeDescription
filenamestring
Returns:
Type: 
Promise.<void>

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

Play the sound Sounds may not play until a user interaction occurs

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 audio source node
Type: 
SoundInstance

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

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
Type: 
SoundInstance

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

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
Type: 
SoundInstance