Familiar territory. WebAudio keeps the RPG Maker MZ API, so existing MZ knowledge, tutorials and plugin code apply. Reactor's additions are marked with descriptions below.
The audio object of Web Audio API.
function WebAudio()
Quick Reference
Setup & Creation 11
| initialize() | Sets up a new instance. |
| initialize() | Initializes the audio system. |
| _createContext() | Creates the context. |
| _createMasterGainNode() | Creates the master gain node. |
| _setupEventHandlers() | Setup event handlers. |
| _createDecoder() | Creates the decoder. |
| _createPannerNode() | Creates the panner node. |
| _createGainNode() | Creates the gain node. |
| _createAllSourceNodes() | Creates the all source nodes. |
| _createSourceNode() | Creates the source node. |
| _createEndTimer() | Creates the end timer. |
Getting Information 1
| _currentTime() | The current time. |
Checks (True / False) 6
| _shouldMuteOnHide() | Whether it should mute on hide. |
| isReady() | Checks whether the audio data is ready to play. |
| isError() | Checks whether a loading error has occurred. |
| isPlaying() | Checks whether the audio is playing. |
| _shouldUseDecoder() | Whether it should use decoder. |
| _isOggOpus() | An Ogg Opus stream opens with an "OpusHead" packet where Vorbis has "\x01vorbis", and keeps its comments and… |
Actions 22
| setMasterVolume() | Sets the master volume for all audio. |
| _resetVolume() | Resets the volume. |
| _fadeIn() | Fade in. |
| _fadeOut() | Fade out. |
| clear() | Clears the audio data. |
| play() | Plays the audio. |
| stop() | Stops the audio. |
| destroy() | Destroys the audio. |
| fadeIn() | Performs the audio fade-in. |
| fadeOut() | Performs the audio fade-out. |
| addLoadListener() | Adds a callback function that will be called when the audio data is loaded. |
| addStopListener() | Adds a callback function that will be called when the playback is stopped. |
| _startLoading() | Starts the loading. |
| _destroyDecoder() | Destroy decoder. |
| _startXhrLoading() | Starts the xhr loading. |
| _startFetching() | Starts the fetching. |
| _startPlaying() | Starts the playing. |
| _startAllSourceNodes() | Starts the all source nodes. |
| _startSourceNode() | Starts the source node. |
| _stopSourceNode() | Stops the source node. |
| _removeNodes() | Removes the nodes. |
| _removeEndTimer() | Removes the end timer. |
Events, Commands & Input 10
| _onUserGesture() | Called when user gesture happens. |
| _onVisibilityChange() | Called when visibility change happens. |
| _onHide() | Called when hide happens. |
| _onShow() | Called when show happens. |
| _onXhrLoad() | Called when xhr load happens. |
| _onFetch() | Called when fetch happens. |
| _onError() | Called when error happens. |
| _onFetchProcess() | Called when fetch process happens. |
| _onDecode() | Called when decode happens. |
| _onLoad() | Called when load happens. |
Updating & Drawing 5
| _updateBufferOnFetch() | Updates the buffer on fetch each frame. |
| _updateBuffer() | Updates the buffer each frame. |
| _updateLoopTimes() | Updates the loop times each frame. |
| _refreshSourceNode() | Redraws the source node. |
| _updatePanner() | Updates the panner each frame. |
Other 24
| _holdGain() | Clears pending automation, keeping the level the param is audibly at. |
| _restartGain() | Clears pending automation and forces the param to value. |
| _sweepStalledLoads() | Sweep stalled loads. |
| seek() | Gets the seek position of the audio. |
| retry() | Tries to load the audio again. |
| _checkStalledLoad() | A loading request can silently die without firing onload OR onerror (e.g., a dropped XHR in the burst of ~20… |
| _realUrl() | Real url. |
| _concatenateFetchedData() | Concatenate fetched data. |
| _readableBuffer() | Readable buffer. |
| _decodeAudioData() | Decode audio data. |
| _decodePcmWav() | Chromium's decoder rejects some plain PCM WAV files that older games ship (8-bit unsigned in particular). |
| _loopSampleRate() | The rate the loop's sample counts convert at. |
| _readLoopComments() | Read loop comments. |
| _readOpusLoopComments() | Opus keeps LOOPSTART/LOOPLENGTH in an "OpusTags" packet laid out like a FLAC comment block after its 8-byte m… |
| _readOggPackets() | The first count complete packets of an Ogg stream, each as a DataView. |
| _readFlacLoopComments() | FLAC carries the same vorbis comments as OGG, so LOOPSTART/LOOPLENGTH work identically; the sample rate lives… |
| _readFlacComments() | Comment blocks shared by FLAC, Ogg Vorbis and Ogg Opus. |
| _readOggLoopComments() | Vorbis keeps its sample rate in the identification header ("\x01vorbis") and its loop in the comment header (… |
| _readMetaData() | Read meta data. |
| _readWavLoopComments() | Loop points for a WAV file come from the sampler ("smpl") chunk, the format samplers and DAWs write them to. |
| _readMp3LoopComments() | Loop points for an MP3 come from ID3v2 TXXX frames named LOOPSTART and LOOPLENGTH (the convention loop-taggin… |
| _readMp3UserText() | Read mp3 user text. |
| _readMp3SampleRate() | Read mp3 sample rate. |
| _readFourCharacters() | Read four characters. |
Setup & Creation
initialize
method sourceWebAudio.prototype.initialize(url)
Sets up a new instance. (Same as the RPG Maker MZ method of this name.)
initialize
static sourceWebAudio.initialize()
Initializes the audio system.
Details
Initializes the audio system.
_createContext
static internal sourceWebAudio._createContext()
Creates the context. (Same as the RPG Maker MZ method of this name.)
_createMasterGainNode
static internal sourceWebAudio._createMasterGainNode()
Creates the master gain node. (Same as the RPG Maker MZ method of this name.)
_setupEventHandlers
static internal sourceWebAudio._setupEventHandlers()
Setup event handlers. (Same as the RPG Maker MZ method of this name.)
_createDecoder
method internal sourceWebAudio.prototype._createDecoder()
Creates the decoder. (Same as the RPG Maker MZ method of this name.)
_createPannerNode
method internal sourceWebAudio.prototype._createPannerNode()
Creates the panner node. (Same as the RPG Maker MZ method of this name.)
_createGainNode
method internal sourceWebAudio.prototype._createGainNode()
Creates the gain node. (Same as the RPG Maker MZ method of this name.)
_createAllSourceNodes
method internal sourceWebAudio.prototype._createAllSourceNodes()
Creates the all source nodes. (Same as the RPG Maker MZ method of this name.)
_createSourceNode
method internal sourceWebAudio.prototype._createSourceNode(index)
Creates the source node. (Same as the RPG Maker MZ method of this name.)
_createEndTimer
method internal sourceWebAudio.prototype._createEndTimer()
Creates the end timer. (Same as the RPG Maker MZ method of this name.)
Getting Information
_currentTime
static internal sourceWebAudio._currentTime()
The current time. (Same as the RPG Maker MZ method of this name.)
Checks (True / False)
_shouldMuteOnHide
static internal sourceWebAudio._shouldMuteOnHide()
Whether it should mute on hide. (Same as the RPG Maker MZ method of this name.)
isReady
method sourceWebAudio.prototype.isReady()
Checks whether the audio data is ready to play.
Details
Checks whether the audio data is ready to play.
isError
method sourceWebAudio.prototype.isError()
Checks whether a loading error has occurred.
Details
Checks whether a loading error has occurred.
isPlaying
method sourceWebAudio.prototype.isPlaying()
Checks whether the audio is playing.
Details
Checks whether the audio is playing.
_shouldUseDecoder
method internal sourceWebAudio.prototype._shouldUseDecoder()
Whether it should use decoder. (Same as the RPG Maker MZ method of this name.)
_isOggOpus
method internal sourceWebAudio.prototype._isOggOpus(view)
An Ogg Opus stream opens with an "OpusHead" packet where Vorbis has "\x01vorbis", and keeps its comments and sample rate where the Vorbis reader below does not look.
Actions
setMasterVolume
static sourceWebAudio.setMasterVolume(value)
Sets the master volume for all audio.
Details
Sets the master volume for all audio.
_resetVolume
static internal sourceWebAudio._resetVolume()
Resets the volume. (Same as the RPG Maker MZ method of this name.)
_fadeIn
static internal sourceWebAudio._fadeIn(duration)
Fade in. (Same as the RPG Maker MZ method of this name.)
_fadeOut
static internal sourceWebAudio._fadeOut(duration)
Fade out. (Same as the RPG Maker MZ method of this name.)
clear
method sourceWebAudio.prototype.clear()
Clears the audio data.
play
method sourceWebAudio.prototype.play(loop, offset)
Plays the audio.
Details
Plays the audio.
stop
method sourceWebAudio.prototype.stop()
Stops the audio.
destroy
method sourceWebAudio.prototype.destroy()
Destroys the audio.
fadeIn
method sourceWebAudio.prototype.fadeIn(duration)
Performs the audio fade-in.
Details
Performs the audio fade-in.
fadeOut
method sourceWebAudio.prototype.fadeOut(duration)
Performs the audio fade-out.
Details
Performs the audio fade-out.
addLoadListener
method sourceWebAudio.prototype.addLoadListener(listner)
Adds a callback function that will be called when the audio data is loaded.
Details
Adds a callback function that will be called when the audio data is loaded.
addStopListener
method sourceWebAudio.prototype.addStopListener(listner)
Adds a callback function that will be called when the playback is stopped.
Details
Adds a callback function that will be called when the playback is stopped.
_startLoading
method internal sourceWebAudio.prototype._startLoading()
Starts the loading. (Same as the RPG Maker MZ method of this name.)
_destroyDecoder
method internal sourceWebAudio.prototype._destroyDecoder()
Destroy decoder. (Same as the RPG Maker MZ method of this name.)
_startXhrLoading
method internal sourceWebAudio.prototype._startXhrLoading(url)
Starts the xhr loading. (Same as the RPG Maker MZ method of this name.)
_startFetching
method internal sourceWebAudio.prototype._startFetching(url)
Starts the fetching. (Same as the RPG Maker MZ method of this name.)
_startPlaying
method internal sourceWebAudio.prototype._startPlaying(offset)
Starts the playing. (Same as the RPG Maker MZ method of this name.)
_startAllSourceNodes
method internal sourceWebAudio.prototype._startAllSourceNodes()
Starts the all source nodes. (Same as the RPG Maker MZ method of this name.)
_startSourceNode
method internal sourceWebAudio.prototype._startSourceNode(index)
Starts the source node. (Same as the RPG Maker MZ method of this name.)
_stopSourceNode
method internal sourceWebAudio.prototype._stopSourceNode()
Stops the source node. (Same as the RPG Maker MZ method of this name.)
_removeNodes
method internal sourceWebAudio.prototype._removeNodes()
Removes the nodes. (Same as the RPG Maker MZ method of this name.)
_removeEndTimer
method internal sourceWebAudio.prototype._removeEndTimer()
Removes the end timer. (Same as the RPG Maker MZ method of this name.)
Events, Commands & Input
_onUserGesture
static internal sourceWebAudio._onUserGesture()
Called when user gesture happens. (Same as the RPG Maker MZ method of this name.)
_onVisibilityChange
static internal sourceWebAudio._onVisibilityChange()
Called when visibility change happens. (Same as the RPG Maker MZ method of this name.)
_onHide
static internal sourceWebAudio._onHide()
Called when hide happens. (Same as the RPG Maker MZ method of this name.)
_onShow
static internal sourceWebAudio._onShow()
Called when show happens. (Same as the RPG Maker MZ method of this name.)
_onXhrLoad
method internal sourceWebAudio.prototype._onXhrLoad(xhr)
Called when xhr load happens. (Same as the RPG Maker MZ method of this name.)
_onFetch
method internal sourceWebAudio.prototype._onFetch(response, generation)
Called when fetch happens. (Same as the RPG Maker MZ method of this name.)
_onError
method internal sourceWebAudio.prototype._onError()
Called when error happens. (Same as the RPG Maker MZ method of this name.)
_onFetchProcess
method internal sourceWebAudio.prototype._onFetchProcess(value)
Called when fetch process happens. (Same as the RPG Maker MZ method of this name.)
_onDecode
method internal sourceWebAudio.prototype._onDecode(buffer)
Called when decode happens. (Same as the RPG Maker MZ method of this name.)
_onLoad
method internal sourceWebAudio.prototype._onLoad()
Called when load happens. (Same as the RPG Maker MZ method of this name.)
Updating & Drawing
_updateBufferOnFetch
method internal sourceWebAudio.prototype._updateBufferOnFetch()
Updates the buffer on fetch each frame. (Same as the RPG Maker MZ method of this name.)
_updateBuffer
method internal sourceWebAudio.prototype._updateBuffer()
Updates the buffer each frame. (Same as the RPG Maker MZ method of this name.)
_updateLoopTimes
method internal sourceWebAudio.prototype._updateLoopTimes()
Updates the loop times each frame. (Same as the RPG Maker MZ method of this name.)
_refreshSourceNode
method internal sourceWebAudio.prototype._refreshSourceNode()
Redraws the source node. (Same as the RPG Maker MZ method of this name.)
_updatePanner
method internal sourceWebAudio.prototype._updatePanner()
Updates the panner each frame. (Same as the RPG Maker MZ method of this name.)
Other
_holdGain
static internal sourceWebAudio._holdGain(gain, currentTime)
Clears pending automation, keeping the level the param is audibly at.
_restartGain
static internal sourceWebAudio._restartGain(gain, value, currentTime)
Clears pending automation and forces the param to value.
_sweepStalledLoads
static internal sourceWebAudio._sweepStalledLoads()
Sweep stalled loads. (Same as the RPG Maker MZ method of this name.)
seek
method sourceWebAudio.prototype.seek()
Gets the seek position of the audio.
retry
method sourceWebAudio.prototype.retry()
Tries to load the audio again.
_checkStalledLoad
method internal sourceWebAudio.prototype._checkStalledLoad()
A loading request can silently die without firing onload OR onerror (e.g., a dropped XHR in the burst of ~20 parallel audio loads during a save-game load), leaving isReady() false forever.
Details
A loading request can silently die without firing onload OR onerror (e.g., a dropped XHR in the burst of ~20 parallel audio loads during a save-game load), leaving isReady() false forever. Anything gating on AudioManager.isReady() -- preload plugins wrap Scene_Base.isReady with it -- then deadlocks the scene: never started, stage never set, screen permanently black. The stall check runs from isReady() itself: it is polled every frame by exactly the code that is blocked waiting, and it measures AudioContext time, so it works even when the window is backgrounded (where setTimeout is throttled) and even if the load never armed because the context was missing at creation time. Zero progress for 10s -> retry (3 attempts) -> surface a real error.
_realUrl
method internal sourceWebAudio.prototype._realUrl()
Real url. (Same as the RPG Maker MZ method of this name.)
_concatenateFetchedData
method internal sourceWebAudio.prototype._concatenateFetchedData()
Concatenate fetched data. (Same as the RPG Maker MZ method of this name.)
_readableBuffer
method internal sourceWebAudio.prototype._readableBuffer()
Readable buffer. (Same as the RPG Maker MZ method of this name.)
_decodeAudioData
method internal sourceWebAudio.prototype._decodeAudioData(arrayBuffer)
Decode audio data. (Same as the RPG Maker MZ method of this name.)
_decodePcmWav
static internal sourceWebAudio._decodePcmWav(arrayBuffer)
Chromium's decoder rejects some plain PCM WAV files that older games ship (8-bit unsigned in particular).
Details
Chromium's decoder rejects some plain PCM WAV files that older games ship (8-bit unsigned in particular). A RIFF/WAVE file whose samples are integer PCM (8, 16, 24 or 32 bit) or 32-bit float is read here instead; anything else (ADPCM and other compressed WAV) returns null.
_loopSampleRate
method internal sourceWebAudio.prototype._loopSampleRate()
The rate the loop's sample counts convert at.
Details
The rate the loop's sample counts convert at. An Opus file's counts are read at the original rate OpusHead records, but some taggers count at Opus's own 48 kHz whatever the header says. Once the whole file is decoded, a loop that would end past the track at the recorded rate but fits at 48 kHz is read at 48 kHz; when both fit, the recorded rate holds.
_readLoopComments
method internal sourceWebAudio.prototype._readLoopComments(arrayBuffer)
Read loop comments. (Same as the RPG Maker MZ method of this name.)
_readOpusLoopComments
method internal sourceWebAudio.prototype._readOpusLoopComments(view)
Opus keeps LOOPSTART/LOOPLENGTH in an "OpusTags" packet laid out like a FLAC comment block after its 8-byte magic.
Details
Opus keeps LOOPSTART/LOOPLENGTH in an "OpusTags" packet laid out like a FLAC comment block after its 8-byte magic. The sample counts convert at the input sample rate OpusHead records, the rate of the source the tags were measured against; Opus itself always decodes at 48 kHz, which an unspecified (0) rate falls back to. Some taggers count at 48 kHz whatever the header records, which _loopSampleRate catches once the track's length is known.
_readOggPackets
method internal sourceWebAudio.prototype._readOggPackets(view, count)
The first count complete packets of an Ogg stream, each as a DataView.
Details
The first count complete packets of an Ogg stream, each as a DataView.
A packet may continue across pages (a comment packet carrying cover art
usually does), so segments are joined until one shorter than 255 ends it.
Streamed loads pass a growing prefix: a packet still cut off is left out.
_readFlacLoopComments
method internal sourceWebAudio.prototype._readFlacLoopComments(view)
FLAC carries the same vorbis comments as OGG, so LOOPSTART/LOOPLENGTH work identically; the sample rate lives in the STREAMINFO block.
_readFlacComments
method internal sourceWebAudio.prototype._readFlacComments(view, index, size)
Comment blocks shared by FLAC, Ogg Vorbis and Ogg Opus.
Details
Comment blocks shared by FLAC, Ogg Vorbis and Ogg Opus. LOOPSTART/LOOPLENGTH is the RPG Maker pair; LOOP_START/LOOP_END, an end sample in place of a length, is what other tools and rips write, and counts only without LOOPLENGTH.
_readOggLoopComments
method internal sourceWebAudio.prototype._readOggLoopComments(view)
Vorbis keeps its sample rate in the identification header ("\x01vorbis") and its loop in the comment header ("\x03vorbis"), the first two packets, the comments laid out like a FLAC comment block after the 7-byte magic.
Details
Vorbis keeps its sample rate in the identification header ("\x01vorbis") and its loop in the comment header ("\x03vorbis"), the first two packets, the comments laid out like a FLAC comment block after the 7-byte magic. Both are read whole through _readOggPackets, as Opus is: a comment header continued across pages (behind cover art) is still found, names match in any case, LOOP_START/LOOP_END counts as it does on Opus and FLAC, and a streamed prefix cut inside a header waits for the next pass instead of reading past its end. A comment header the block reader finds no loop in still gets the older scan, which also takes LOOPSTART and LOOPLENGTH written as a name with the number 16 bytes after it.
_readMetaData
method internal sourceWebAudio.prototype._readMetaData(view, index, size)
Read meta data. (Same as the RPG Maker MZ method of this name.)
_readWavLoopComments
method internal sourceWebAudio.prototype._readWavLoopComments(view)
Loop points for a WAV file come from the sampler ("smpl") chunk, the format samplers and DAWs write them to.
Details
Loop points for a WAV file come from the sampler ("smpl") chunk, the format samplers and DAWs write them to. The sample rate comes from the format chunk so loop samples convert to time exactly as for OGG. Streamed loads pass a growing prefix, so every read is bounds-checked and a truncated chunk simply leaves the defaults until the next pass.
_readMp3LoopComments
method internal sourceWebAudio.prototype._readMp3LoopComments(view)
Loop points for an MP3 come from ID3v2 TXXX frames named LOOPSTART and LOOPLENGTH (the convention loop-tagging tools share with the OGG comments).
Details
Loop points for an MP3 come from ID3v2 TXXX frames named LOOPSTART and LOOPLENGTH (the convention loop-tagging tools share with the OGG comments). The sample rate comes from the first MPEG frame header after the tag.
_readMp3UserText
method internal sourceWebAudio.prototype._readMp3UserText(view, index, size)
Read mp3 user text. (Same as the RPG Maker MZ method of this name.)
_readMp3SampleRate
method internal sourceWebAudio.prototype._readMp3SampleRate(view, start)
Read mp3 sample rate. (Same as the RPG Maker MZ method of this name.)
_readFourCharacters
method internal sourceWebAudio.prototype._readFourCharacters(view, index)
Read four characters. (Same as the RPG Maker MZ method of this name.)