Audio

LittleJS Audio System

  • ZzFX Sound Effects - ZzFX Sound Effect Generator
  • ZzFXM Music - ZzFXM Music System
  • Caches sounds and music for fast playback
  • Can attenuate and apply stereo panning to sounds
  • Ability to play mp3, ogg, and wave files
  • Speech synthesis functions

Members

(static) audioContext :AudioContext

Audio context used by the engine

Type:
  • AudioContext

(static) audioGainNode :GainNode

Master gain node for all audio to pass through

Type:
  • GainNode

(static, constant) zzfxR

Sample rate used for all ZzFX sounds

Default Value
  • 44100

Methods

(static) getNoteFrequency(semitoneOffset, rootFrequencyopt) → {Number}

Get frequency of a note on a musical scale

Parameters:
NameTypeAttributesDefaultDescription
semitoneOffsetNumber

How many semitones away from the root note

rootFrequencyNumber<optional>
220

Frequency at semitone offset 0

Returns:
  • The frequency of the note
Type: 
Number

(static) playAudioFile(filename, volumeopt, loopopt) → {SoundWave}

Play an mp3, ogg, or wav audio from a local file or url

Parameters:
NameTypeAttributesDefaultDescription
filenameString

Location of sound file to play

volumeNumber<optional>
1

How much to scale volume by

loopBoolean<optional>
false

True if the music should loop

Returns:
  • The sound object for this file
Type: 
SoundWave

(static) playSamples(sampleChannels, volumeopt, rateopt, panopt, loopopt, sampleRateopt, gainNodeopt) → {AudioBufferSourceNode}

Play cached audio samples with given settings

Parameters:
NameTypeAttributesDefaultDescription
sampleChannelsArray

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

volumeNumber<optional>
1

How much to scale volume by

rateNumber<optional>
1

The playback rate to use

panNumber<optional>
0

How much to apply stereo panning

loopBoolean<optional>
false

True if the sound should loop when it reaches the end

sampleRateNumber<optional>
44100

Sample rate for the sound

gainNodeGainNode<optional>

Optional gain node for volume control while playing

Returns:
  • The audio node of the sound played
Type: 
AudioBufferSourceNode

(static) speak(text, languageopt, volumeopt, rateopt, pitchopt) → {SpeechSynthesisUtterance}

Speak text with passed in settings

Parameters:
NameTypeAttributesDefaultDescription
textString

The text to speak

languageString<optional>

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

volumeNumber<optional>
1

How much to scale volume by

rateNumber<optional>
1

How quickly to speak

pitchNumber<optional>
1

How much to change the pitch by

Returns:
  • The utterance that was spoken
Type: 
SpeechSynthesisUtterance

(static) speakStop()

Stop all queued speech

(static) zzfx(…zzfxSound) → {AudioBufferSourceNode}

Generate and play a ZzFX sound

Create sounds using the ZzFX Sound Designer.

Parameters:
NameTypeAttributesDescription
zzfxSoundArray<repeatable>

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

Returns:
  • The audio node of the sound played
Type: 
AudioBufferSourceNode

(static) zzfxG(volumeopt, randomnessopt, frequencyopt, attackopt, sustainopt, releaseopt, shapeopt, shapeCurveopt, slideopt, deltaSlideopt, pitchJumpopt, pitchJumpTimeopt, repeatTimeopt, noiseopt, modulationopt, bitCrushopt, delayopt, sustainVolumeopt, decayopt, tremoloopt, filteropt) → {Array}

Generate samples for a ZzFX sound

Parameters:
NameTypeAttributesDefaultDescription
volumeNumber<optional>
1

Volume scale (percent)

randomnessNumber<optional>
0.05

How much to randomize frequency (percent Hz)

frequencyNumber<optional>
220

Frequency of sound (Hz)

attackNumber<optional>
0

Attack time, how fast sound starts (seconds)

sustainNumber<optional>
0

Sustain time, how long sound holds (seconds)

releaseNumber<optional>
0.1

Release time, how fast sound fades out (seconds)

shapeNumber<optional>
0

Shape of the sound wave

shapeCurveNumber<optional>
1

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

slideNumber<optional>
0

How much to slide frequency (kHz/s)

deltaSlideNumber<optional>
0

How much to change slide (kHz/s/s)

pitchJumpNumber<optional>
0

Frequency of pitch jump (Hz)

pitchJumpTimeNumber<optional>
0

Time of pitch jump (seconds)

repeatTimeNumber<optional>
0

Resets some parameters periodically (seconds)

noiseNumber<optional>
0

How much random noise to add (percent)

modulationNumber<optional>
0

Frequency of modulation wave, negative flips phase (Hz)

bitCrushNumber<optional>
0

Resamples at a lower frequency in (samples*100)

delayNumber<optional>
0

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

sustainVolumeNumber<optional>
1

Volume level for sustain (percent)

decayNumber<optional>
0

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

tremoloNumber<optional>
0

Trembling effect, rate controlled by repeat time (precent)

filterNumber<optional>
0

Filter cutoff frequency, positive for HPF, negative for LPF (Hz)

Returns:
  • Array of audio samples
Type: 
Array

(static) zzfxM(instruments, patterns, sequence, BPMopt) → {Array}

Generate samples for a ZzFM song with given parameters

Parameters:
NameTypeAttributesDefaultDescription
instrumentsArray

Array of ZzFX sound paramaters

patternsArray

Array of pattern data

sequenceArray

Array of pattern indexes

BPMNumber<optional>
125

Playback speed of the song in BPM

Returns:
  • Left and right channel sample data
Type: 
Array