Audio
Provide the tools to create and manage audio playback. Use InnerAudioContext for straightforward audio playback controls, and WebAudioContext for advanced audio processing, synthesis, and analysis.
tt.createInnerAudioContext()
Creates an InnerAudioContext object instance.
Returns
Returns an InnerAudioContext object instance.
Example
const innerAudioContext = tt.createInnerAudioContext();
console.log('Created audio context instance:', innerAudioContext);tt.createWebAudioContext()
Creates a WebAudioContext object instance.
Returns
Returns a WebAudioContext object instance.
Example
const webAudioContext = tt.createWebAudioContext();
console.log('Created web audio context:', webAudioContext);InnerAudioContext instance methods
InnerAudioContext.destroy()
Destroys the current instance.
InnerAudioContext.pause()
Pauses playback. Audio resumed after pausing will continue from the paused position.
InnerAudioContext.play()
Starts playback.
InnerAudioContext.seek(position)
Seeks to the specified position.
Parameters
Field | Type | Description | Required |
|
| The seek time in seconds | Yes |
InnerAudioContext.stop()
Stops playback. Audio played after stopping will start from the beginning.
InnerAudioContext event listener methods
The following table lists the .on* event listener methods for InnerAudioContext. Each method accepts a callback parameter of type function.
Method | Description |
| Listens for the event when audio enters a playable state. Does not guarantee smooth playback afterwards. |
| Listens for the event when audio naturally plays to the end |
| Listens for audio playback error events. The callback receives an |
| Listens for audio pause events |
| Listens for audio play events |
| Listens for the event when a seek operation completes |
| Listens for the event when a seek operation is in progress |
| Listens for audio stop events |
| Listens for audio playback progress update events |
| Listens for audio loading events. Triggered when audio needs to stop and load due to insufficient data. |
The following table lists the .off* event listener removal methods for InnerAudioContext. Each method accepts an optional callback parameter of type function. If no callback is provided, all listeners for that event are removed.
Method | Description |
| Removes the listener for the canplay event |
| Removes the listener for the ended event |
| Removes the listener for the error event |
| Removes the listener for the pause event |
| Removes the listener for the play event |
| Removes the listener for the seeked event |
| Removes the listener for the seeking event |
| Removes the listener for the stop event |
| Removes the listener for the time update event |
| Removes the listener for the waiting event |
Example
const listener = function(res) { console.log(res); };
innerAudioContext.onCanplay(listener);
innerAudioContext.offCanplay(listener); // Pass the same function object used for listening
innerAudioContext.onEnded(listener);
innerAudioContext.offEnded(listener);
innerAudioContext.onError(listener);
innerAudioContext.offError(listener);
innerAudioContext.onPause(listener);
innerAudioContext.offPause(listener);
innerAudioContext.onPlay(listener);
innerAudioContext.offPlay(listener);
innerAudioContext.onSeeked(listener);
innerAudioContext.offSeeked(listener);
innerAudioContext.onSeeking(listener);
innerAudioContext.offSeeking(listener);
innerAudioContext.onStop(listener);
innerAudioContext.offStop(listener);
innerAudioContext.onTimeUpdate(listener);
innerAudioContext.offTimeUpdate(listener);
innerAudioContext.onWaiting(listener);
innerAudioContext.offWaiting(listener);WebAudioContext instance methods
WebAudioContext.close()
Closes the WebAudioContext. Immediately releases the resources of the current context.
Returns
Returns a Promise.
Warning: Do not access the state property after calling close().
Example
const audioCtx = TTMinis.game.createWebAudioContext();
audioCtx.close().then(() => {
// Do not access audioCtx.state after close
});WebAudioContext.createAnalyser()
Creates an AnalyserNode. Can be used to retrieve audio time and frequency data and implement data visualization.
Returns
Returns an AnalyserNode.
Example
const audioCtx = TTMinis.game.createWebAudioContext();
const analyser = audioCtx.createAnalyser();
analyser.fftSize = 2048;
const bufferLength = analyser.fftSize;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);WebAudioContext.createBiquadFilter()
Creates a BiquadFilterNode.
Returns
Returns a BiquadFilterNode.
WebAudioContext.createBuffer(numOfChannels, length, sampleRate)
Creates an AudioBuffer representing a short audio segment residing in memory.
Parameters
Field | Type | Description | Required |
|
| The number of audio channels in the buffer | Yes |
|
| The number of sample frames in the buffer | Yes |
|
| The sample rate of the audio data, in samples per second | Yes |
Returns
Returns an AudioBuffer instance.
Example
const audioCtx = TTMinis.game.createWebAudioContext();
const channels = 2;
const frameCount = audioCtx.sampleRate * 2.0;
const myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);WebAudioContext.createBufferSource()
Creates a BufferSourceNode instance to play audio data through an AudioBuffer object.
Returns
Returns a BufferSourceNode.
Example
const audioCtx = TTMinis.game.createWebAudioContext();
const loadAudio = (url) => {
return new Promise((resolve, reject) => {
TTMinis.game.request({
url,
responseType: 'arraybuffer',
success: res => {
console.log('res.data', res.data);
audioCtx.decodeAudioData(res.data, buffer => {
resolve(buffer);
}, err => {
console.error('decodeAudioData fail', err);
reject(err);
});
},
fail: res => {
console.error('request fail', res);
reject(res);
}
});
});
};
const play = () => {
loadAudio('YOUR_AUDIO_URL.mp3').then(buffer => {
let source = audioCtx.createBufferSource();
source.buffer = buffer;
source.connect(audioCtx.destination);
source.start();
}).catch(() => {
console.log('fail');
});
};
play();WebAudioContext.createChannelMerger(numberOfInputs)
Creates a ChannelMergerNode.
Parameters
Field | Type | Description | Required |
|
| The number of input streams to maintain in the output stream | Yes |
Returns
Returns a ChannelMergerNode.
WebAudioContext.createChannelSplitter(numberOfOutputs)
Creates a ChannelSplitterNode.
Parameters
Field | Type | Description | Required |
|
| The number of channels in the input audio stream to output separately | Yes |
Returns
Returns a ChannelSplitterNode.
WebAudioContext.createDelay(maxDelayTime)
Creates a DelayNode.
Parameters
Field | Type | Description | Required |
|
| The maximum delay time in seconds | Yes |
Returns
Returns a DelayNode.
Example
let audioCtx = TTMinis.game.createWebAudioContext();
const delayNode = audioCtx.createDelay(5);WebAudioContext.createDynamicsCompressor()
Creates a DynamicsCompressorNode.
Returns
Returns a DynamicsCompressorNode.
Example
let audioCtx = TTMinis.game.createWebAudioContext();
let compressor = audioCtx.createDynamicsCompressor();
compressor.threshold.value = -50;
compressor.knee.value = 40;
compressor.ratio.value = 12;
compressor.attack.value = 0;
compressor.release.value = 0.25;WebAudioContext.createGain()
Creates a GainNode.
Returns
Returns a GainNode.
WebAudioContext.createOscillator()
Creates an OscillatorNode.
Returns
Returns an OscillatorNode.
WebAudioContext.createPanner()
Creates a PannerNode.
Returns
Returns a PannerNode.
WebAudioContext.createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)
Creates a ScriptProcessorNode.
Parameters
Field | Type | Description | Required |
|
| The buffer size in sample frames | Yes |
|
| The number of channels for the input node | Yes |
|
| The number of channels for the output node | Yes |
Returns
Returns a ScriptProcessorNode.
Example
let audioCtx = TTMinis.game.createWebAudioContext();
const sampleSize = 4096;
audioCtx.createScriptProcessor(sampleSize, 1, 1);WebAudioContext.createWaveShaper()
Creates a WaveShaperNode.
Returns
Returns a WaveShaperNode.
WebAudioContext.decodeAudioData(audioData, successCallback, errorCallback)
Asynchronously decodes audio data into an AudioBuffer.
Parameters
Field | Type | Description | Required |
|
| An | Yes |
|
| Called when the audio data is successfully decoded. Receives the decoded | No |
|
| Called when the audio data fails to decode | No |
Returns
Returns an AudioBuffer through the success callback.
Example
TTMinis.game.request({
url: YOUR_AUDIO_URL,
responseType: 'arraybuffer',
success: res => {
audioCtx.decodeAudioData(res.data, buffer => {
console.log(buffer);
}, err => {
console.error('decodeAudioData fail', err);
});
}
});WebAudioContext.resume()
Resumes a paused WebAudioContext.
Returns
Returns a Promise.
WebAudioContext.suspend()
Suspends the WebAudioContext.
Returns
Returns a Promise.