Create a sound object and cache the audio for later use
asset string|URL|Array<(number|undefined)> optionalFilename or URL of an audio file, or a zzfx array
randomness number optionalHow much to randomize frequency each time sound plays, for zzfx sounds it overrides the array's own randomness, which is used if undefined
range number optional, default soundDefaultRangeWorld space max range of sound
taper number optional, default soundDefaultTaperAt what percentage of range should it start tapering
onloadCallback SoundLoadCallback optionalCalled once the sound has loaded, or its file failed to, so a
game counting its sounds goes on; isLoaded says which
// load an audio asset file
const music = new Sound('sound.mp3');
// create a zzfx sound
const blip = new Sound([.5,.5]);
// play a sound
blip.play();
Percentage of this sound currently loaded, sounds
fetched from a url stay at 0 until decoding completes
function to call when sound is loaded
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()
How much to randomize frequency each time sound plays
World space max range of sound, 0 for no limit; a range that is set is the sound's own, in 3D too
No range was given, so it got soundDefaultRange; played in 3D such a sound is heard
to render3D.soundDefaultRange instead, which is further; setting range makes it false
Decoded audio shared by every play of this sound
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.
How many samples per channel this sound has
Sample rate for this sound
At what percentage of range should it start tapering
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
Get how long this sound is in seconds
- Returns
- How long the sound is in seconds (0 if loading)
Check if sound is loaded, for sounds fetched from a url
- Returns
- True if sound is loaded and ready to play
Loads a sound from a URL and decodes it into sample data
filename string
Play the sound on a loop, the same as play with loop on; stop or change it through the SoundInstance returned
pos Vector2 optionalWorld space position to play the sound if any
volume number optional, default 1How much to scale volume by
pitch number optional, default 1How much to scale pitch by
randomnessScale number optional, default 1How much to scale pitch randomness
paused boolean optional, default falseShould the sound start paused
- Returns
- The sound instance, or undefined if sound is disabled, not loaded, out of range, or running in headless mode
Play a music track that loops by default
volume number optional, default 1Volume to play the music at
loop boolean optional, default trueShould the music loop?
paused boolean optional, default falseShould the music start paused
- Returns
- The sound instance, or undefined if sound is disabled, not loaded, or running in headless mode
Play the sound as a musical note with a semitone offset
This can be used to play music with chromatic scales
semitoneOffset number optional, default 0How many semitones to offset pitch
pos Vector2 optionalWorld space position to play the sound if any
volume number optional, default 1How 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