LittleJS 1.28.0

Audio

LittleJS Audio System

  • Play audio files (mp3, ogg, wave) and generate sounds with ZzFX
  • ZzFX sound generator integration: ZzFX
  • Sound caching for fast playback and memory efficiency
  • Volume control with attenuation and stereo panning
  • 2D spatial audio based on camera position with distance-based falloff
  • Sound instance management (pause, resume, stop)
  • Speech synthesis for text-to-speech
  • Music playback from audio files, looping by default
  • Web Audio API integration with master gain control
  • Sounds and the master bus can route through effects, see the audio effects plugin

Classes

Functions

Members

Constants

Typedefs

Functions

audioIsRunning() → booleanengineAudio.js:58

Check if the audio context is running and available for playback

Returns
  • True if the audio context is running, false when there is none
createAudioBuffer(sampleChannels, sampleRate) → AudioBufferengineAudio.js:845

Copy arrays of samples into a new audio buffer

sampleChannels Array

Array of arrays of samples (for stereo playback)

sampleRate number optional, default audioDefaultSampleRate

Sample rate for the sound

Returns
  • The audio buffer holding the samples
getNoteFrequency(semitoneOffset, rootFrequency) → numberengineAudio.js:798

Get frequency of a note on a musical scale

semitoneOffset number

How many semitones away from the root note

rootFrequency number optional, default 220

Frequency at semitone offset 0

Returns
  • The frequency of the note
playAudioBuffer(buffer, volume, rate, pan, loop, gainNode, offset, onended, output, pannerNode) → AudioBufferSourceNode|undefinedengineAudio.js:868

Play an audio buffer with given settings The buffer can be shared by any number of sounds playing at once

buffer AudioBuffer

The audio buffer to play

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

True if the sound should loop when it reaches the end

gainNode GainNode optional

Optional gain node for volume control while playing (disconnected when the sound ends)

offset number optional, default 0

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

onended AudioEndedCallback optional

Callback for when the sound ends

output AudioNode|AudioEffectNodes optional

Node or effect to connect the gain to instead of the master gain

pannerNode StereoPannerNode optional

Optional stereo panner for panning while playing, its pan already set (disconnected when the sound ends)

Returns
  • The source node of the sound played, undefined if play fails
playSamples(sampleChannels, volume, rate, pan, loop, sampleRate, gainNode, offset, onended, output, pannerNode) → AudioBufferSourceNode|undefinedengineAudio.js:823

Play cached audio samples with given settings

sampleChannels Array

Array of arrays of samples to play (for stereo playback)

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

True if the sound should loop when it reaches the end

sampleRate number optional, default audioDefaultSampleRate

Sample rate for the sound

gainNode GainNode optional

Optional gain node for volume control while playing (disconnected when the sound ends)

offset number optional, default 0

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

onended AudioEndedCallback optional

Callback for when the sound ends

output AudioNode|AudioEffectNodes optional

Node or effect to connect the gain to instead of the master gain

pannerNode StereoPannerNode optional

Optional stereo panner for panning while playing, its pan already set (disconnected when the sound ends)

Returns
  • The source node of the sound played, undefined if play fails
setAudioMasterEffect(input, output)engineAudio.js:133

Route all sound through an effect between the master gain and the speakers

  • Pass a node or an effect, or the first and last of a chain, each a node or an effect
  • With one argument a node is both ends, and an effect uses its own input and output
  • The output node is disconnected from everything else first, so it only feeds the speakers
  • The two ends of a chain must already be connected to each other, like effectA.connect(effectB)
  • Call with no arguments to remove the effect, an effect that was the master goes back to feeding the master gain
input AudioNode|AudioEffectNodes optional

Node or effect the master gain connects to

output AudioNode|AudioEffectNodes optional

Node or effect that connects to the audio destination, defaults to the input's output

speak(text, volume, rate, pitch, language) → SpeechSynthesisUtterance|undefinedengineAudio.js:765

Speak text with passed in settings

text string

The text to speak

volume number optional, default 1

How much to scale volume by

rate number optional, default 1

How quickly to speak

pitch number optional, default 1

How much to change the pitch by

language string optional

The language/accent to use (examples: en, it, ru, ja, zh)

Returns
  • The utterance that was spoken, or undefined if speech is unavailable

Stop all queued speech

zzfx(…zzfxSound) → AudioBufferSourceNode|undefinedengineAudio.js:927

Generate and play a ZzFX sound

Create sounds using the ZzFX Sound Designer.

…zzfxSound Array

Array of ZzFX parameters, ex. [.5,.5]

Returns
  • The audio node of the sound played, undefined if play fails
zzfxG(volume, randomness, frequency, attack, sustain, release, shape, shapeCurve, slide, deltaSlide, pitchJump, pitchJumpTime, repeatTime, noise, modulation, bitCrush, delay, sustainVolume, decay, tremolo, filter) → Float32ArrayengineAudio.js:954

Generate samples for a ZzFX sound

volume number optional, default 1

Volume scale (percent)

randomness number optional, default 0.05

How much to randomize frequency (percent Hz)

frequency number optional, default 220

Frequency of sound (Hz)

attack number optional, default 0

Attack time, how fast sound starts (seconds)

sustain number optional, default 0

Sustain time, how long sound holds (seconds)

release number optional, default 0.1

Release time, how fast sound fades out (seconds)

shape number optional, default 0

Shape of the sound wave

shapeCurve number optional, default 1

Squareness of wave (0=square, 1=normal, 2=pointy)

slide number optional, default 0

How much to slide frequency, 1 is about 500 Hz a second, as ZzFX has it

deltaSlide number optional, default 0

How much to change slide each second, in the units of slide

pitchJump number optional, default 0

Frequency of pitch jump (Hz)

pitchJumpTime number optional, default 0

Time of pitch jump (seconds)

repeatTime number optional, default 0

Resets some parameters periodically (seconds)

noise number optional, default 0

How much random noise to add (percent)

modulation number optional, default 0

Frequency of modulation wave, negative flips phase (Hz)

bitCrush number optional, default 0

Resamples at a lower frequency in (samples*100)

delay number optional, default 0

Overlap sound with itself for reverb and flanger effects (seconds)

sustainVolume number optional, default 1

Volume level for sustain (percent)

decay number optional, default 0

Decay time, how long to reach sustain after attack (seconds)

tremolo number optional, default 0

Trembling effect, rate controlled by repeat time (percent)

filter number optional, default 0

Filter cutoff, positive for HPF, negative for LPF; the cutoff is about twice this in Hz, as ZzFX has it, and its resonance can take the samples past 1

Returns
  • The audio samples

Members

audioContext : AudioContextengineAudio.js:21

Audio context used by the engine, undefined outside a browser, where the engine runs headless

audioMasterGain : GainNodeengineAudio.js:29

Master gain node for all audio to pass through, made at load so effects can connect to it any time

Constants

audioDefaultSampleRate default 44100engineAudio.js:53

Default sample rate used for sounds

Typedefs

AudioEffectNodes : ObjectengineAudio.js:120

Anything with input and output audio nodes, like an effect from the audio effects plugin

AudioEndedCallback(source)engineAudio.js:803
source AudioBufferSourceNode
SoundLoadCallback(sound)engineAudio.js:208
sound Sound