Class in reactor_core_io.js

WebAudio

79 functions in 7 groups · hide internal

The Database: everything Core loads at boot.

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.

@class
@param
url string The url of the audio file.
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 source
WebAudio.prototype.initialize(url)

Sets up a new instance. (Same as the RPG Maker MZ method of this name.)

initialize

static source
WebAudio.initialize()

Initializes the audio system.

Details

Initializes the audio system.

@returns
boolean True if the audio system is available.

_createContext

static internal source
WebAudio._createContext()

Creates the context. (Same as the RPG Maker MZ method of this name.)

_createMasterGainNode

static internal source
WebAudio._createMasterGainNode()

Creates the master gain node. (Same as the RPG Maker MZ method of this name.)

_setupEventHandlers

static internal source
WebAudio._setupEventHandlers()

Setup event handlers. (Same as the RPG Maker MZ method of this name.)

_createDecoder

method internal source
WebAudio.prototype._createDecoder()

Creates the decoder. (Same as the RPG Maker MZ method of this name.)

_createPannerNode

method internal source
WebAudio.prototype._createPannerNode()

Creates the panner node. (Same as the RPG Maker MZ method of this name.)

_createGainNode

method internal source
WebAudio.prototype._createGainNode()

Creates the gain node. (Same as the RPG Maker MZ method of this name.)

_createAllSourceNodes

method internal source
WebAudio.prototype._createAllSourceNodes()

Creates the all source nodes. (Same as the RPG Maker MZ method of this name.)

_createSourceNode

method internal source
WebAudio.prototype._createSourceNode(index)

Creates the source node. (Same as the RPG Maker MZ method of this name.)

_createEndTimer

method internal source
WebAudio.prototype._createEndTimer()

Creates the end timer. (Same as the RPG Maker MZ method of this name.)

Getting Information

_currentTime

static internal source
WebAudio._currentTime()

The current time. (Same as the RPG Maker MZ method of this name.)

Checks (True / False)

_shouldMuteOnHide

static internal source
WebAudio._shouldMuteOnHide()

Whether it should mute on hide. (Same as the RPG Maker MZ method of this name.)

isReady

method source
WebAudio.prototype.isReady()

Checks whether the audio data is ready to play.

Details

Checks whether the audio data is ready to play.

@returns
boolean True if the audio data is ready to play.

isError

method source
WebAudio.prototype.isError()

Checks whether a loading error has occurred.

Details

Checks whether a loading error has occurred.

@returns
boolean True if a loading error has occurred.

isPlaying

method source
WebAudio.prototype.isPlaying()

Checks whether the audio is playing.

Details

Checks whether the audio is playing.

@returns
boolean True if the audio is playing.

_shouldUseDecoder

method internal source
WebAudio.prototype._shouldUseDecoder()

Whether it should use decoder. (Same as the RPG Maker MZ method of this name.)

_isOggOpus

method internal source
WebAudio.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 source
WebAudio.setMasterVolume(value)

Sets the master volume for all audio.

Details

Sets the master volume for all audio.

@param
value number The master volume (0 to 1).

_resetVolume

static internal source
WebAudio._resetVolume()

Resets the volume. (Same as the RPG Maker MZ method of this name.)

_fadeIn

static internal source
WebAudio._fadeIn(duration)

Fade in. (Same as the RPG Maker MZ method of this name.)

_fadeOut

static internal source
WebAudio._fadeOut(duration)

Fade out. (Same as the RPG Maker MZ method of this name.)

clear

method source
WebAudio.prototype.clear()

Clears the audio data.

play

method source
WebAudio.prototype.play(loop, offset)

Plays the audio.

Details

Plays the audio.

@param
loop boolean Whether the audio data play in a loop.
@param
offset number The start position to play in seconds.

stop

method source
WebAudio.prototype.stop()

Stops the audio.

destroy

method source
WebAudio.prototype.destroy()

Destroys the audio.

fadeIn

method source
WebAudio.prototype.fadeIn(duration)

Performs the audio fade-in.

Details

Performs the audio fade-in.

@param
duration number Fade-in time in seconds.

fadeOut

method source
WebAudio.prototype.fadeOut(duration)

Performs the audio fade-out.

Details

Performs the audio fade-out.

@param
duration number Fade-out time in seconds.

addLoadListener

method source
WebAudio.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.

@param
listner function The callback function.

addStopListener

method source
WebAudio.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.

@param
listner function The callback function.

_startLoading

method internal source
WebAudio.prototype._startLoading()

Starts the loading. (Same as the RPG Maker MZ method of this name.)

_destroyDecoder

method internal source
WebAudio.prototype._destroyDecoder()

Destroy decoder. (Same as the RPG Maker MZ method of this name.)

_startXhrLoading

method internal source
WebAudio.prototype._startXhrLoading(url)

Starts the xhr loading. (Same as the RPG Maker MZ method of this name.)

_startFetching

method internal source
WebAudio.prototype._startFetching(url)

Starts the fetching. (Same as the RPG Maker MZ method of this name.)

_startPlaying

method internal source
WebAudio.prototype._startPlaying(offset)

Starts the playing. (Same as the RPG Maker MZ method of this name.)

_startAllSourceNodes

method internal source
WebAudio.prototype._startAllSourceNodes()

Starts the all source nodes. (Same as the RPG Maker MZ method of this name.)

_startSourceNode

method internal source
WebAudio.prototype._startSourceNode(index)

Starts the source node. (Same as the RPG Maker MZ method of this name.)

_stopSourceNode

method internal source
WebAudio.prototype._stopSourceNode()

Stops the source node. (Same as the RPG Maker MZ method of this name.)

_removeNodes

method internal source
WebAudio.prototype._removeNodes()

Removes the nodes. (Same as the RPG Maker MZ method of this name.)

_removeEndTimer

method internal source
WebAudio.prototype._removeEndTimer()

Removes the end timer. (Same as the RPG Maker MZ method of this name.)

Events, Commands & Input

_onUserGesture

static internal source
WebAudio._onUserGesture()

Called when user gesture happens. (Same as the RPG Maker MZ method of this name.)

_onVisibilityChange

static internal source
WebAudio._onVisibilityChange()

Called when visibility change happens. (Same as the RPG Maker MZ method of this name.)

_onHide

static internal source
WebAudio._onHide()

Called when hide happens. (Same as the RPG Maker MZ method of this name.)

_onShow

static internal source
WebAudio._onShow()

Called when show happens. (Same as the RPG Maker MZ method of this name.)

_onXhrLoad

method internal source
WebAudio.prototype._onXhrLoad(xhr)

Called when xhr load happens. (Same as the RPG Maker MZ method of this name.)

_onFetch

method internal source
WebAudio.prototype._onFetch(response, generation)

Called when fetch happens. (Same as the RPG Maker MZ method of this name.)

_onError

method internal source
WebAudio.prototype._onError()

Called when error happens. (Same as the RPG Maker MZ method of this name.)

_onFetchProcess

method internal source
WebAudio.prototype._onFetchProcess(value)

Called when fetch process happens. (Same as the RPG Maker MZ method of this name.)

_onDecode

method internal source
WebAudio.prototype._onDecode(buffer)

Called when decode happens. (Same as the RPG Maker MZ method of this name.)

_onLoad

method internal source
WebAudio.prototype._onLoad()

Called when load happens. (Same as the RPG Maker MZ method of this name.)

Updating & Drawing

_updateBufferOnFetch

method internal source
WebAudio.prototype._updateBufferOnFetch()

Updates the buffer on fetch each frame. (Same as the RPG Maker MZ method of this name.)

_updateBuffer

method internal source
WebAudio.prototype._updateBuffer()

Updates the buffer each frame. (Same as the RPG Maker MZ method of this name.)

_updateLoopTimes

method internal source
WebAudio.prototype._updateLoopTimes()

Updates the loop times each frame. (Same as the RPG Maker MZ method of this name.)

_refreshSourceNode

method internal source
WebAudio.prototype._refreshSourceNode()

Redraws the source node. (Same as the RPG Maker MZ method of this name.)

_updatePanner

method internal source
WebAudio.prototype._updatePanner()

Updates the panner each frame. (Same as the RPG Maker MZ method of this name.)

Other

_holdGain

static internal source
WebAudio._holdGain(gain, currentTime)

Clears pending automation, keeping the level the param is audibly at.

_restartGain

static internal source
WebAudio._restartGain(gain, value, currentTime)

Clears pending automation and forces the param to value.

_sweepStalledLoads

static internal source
WebAudio._sweepStalledLoads()

Sweep stalled loads. (Same as the RPG Maker MZ method of this name.)

seek

method source
WebAudio.prototype.seek()

Gets the seek position of the audio.

retry

method source
WebAudio.prototype.retry()

Tries to load the audio again.

_checkStalledLoad

method internal source
WebAudio.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 source
WebAudio.prototype._realUrl()

Real url. (Same as the RPG Maker MZ method of this name.)

_concatenateFetchedData

method internal source
WebAudio.prototype._concatenateFetchedData()

Concatenate fetched data. (Same as the RPG Maker MZ method of this name.)

_readableBuffer

method internal source
WebAudio.prototype._readableBuffer()

Readable buffer. (Same as the RPG Maker MZ method of this name.)

_decodeAudioData

method internal source
WebAudio.prototype._decodeAudioData(arrayBuffer)

Decode audio data. (Same as the RPG Maker MZ method of this name.)

_decodePcmWav

static internal source
WebAudio._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 source
WebAudio.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 source
WebAudio.prototype._readLoopComments(arrayBuffer)

Read loop comments. (Same as the RPG Maker MZ method of this name.)

_readOpusLoopComments

method internal source
WebAudio.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 source
WebAudio.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 source
WebAudio.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 source
WebAudio.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 source
WebAudio.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 source
WebAudio.prototype._readMetaData(view, index, size)

Read meta data. (Same as the RPG Maker MZ method of this name.)

_readWavLoopComments

method internal source
WebAudio.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 source
WebAudio.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 source
WebAudio.prototype._readMp3UserText(view, index, size)

Read mp3 user text. (Same as the RPG Maker MZ method of this name.)

_readMp3SampleRate

method internal source
WebAudio.prototype._readMp3SampleRate(view, start)

Read mp3 sample rate. (Same as the RPG Maker MZ method of this name.)

_readFourCharacters

method internal source
WebAudio.prototype._readFourCharacters(view, index)

Read four characters. (Same as the RPG Maker MZ method of this name.)