1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Fix AudioStreamPlayer get_playback_position() for web build

This commit is contained in:
Yahkub-R
2024-08-06 09:46:37 -04:00
parent 446e7a7c22
commit bcd776e441
10 changed files with 183 additions and 3 deletions

View File

@@ -299,6 +299,8 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
return `${loadPath}.worker.js`;
} else if (path.endsWith('.audio.worklet.js')) {
return `${loadPath}.audio.worklet.js`;
} else if (path.endsWith('.audio.position.worklet.js')) {
return `${loadPath}.audio.position.worklet.js`;
} else if (path.endsWith('.js')) {
return `${loadPath}.js`;
} else if (path in gdext) {

View File

@@ -0,0 +1,50 @@
/**************************************************************************/
/* godot.audio.position.worklet.js */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
class GodotPositionReportingProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.position = 0;
}
process(inputs, _outputs, _parameters) {
if (inputs.length > 0) {
const input = inputs[0];
if (input.length > 0) {
this.position += input[0].length;
this.port.postMessage({ 'type': 'position', 'data': this.position });
return true;
}
}
return true;
}
}
registerProcessor('godot-position-reporting-processor', GodotPositionReportingProcessor);

View File

@@ -332,6 +332,7 @@ class SampleNodeBus {
* startTime?: number
* loopMode?: LoopMode
* volume?: Float32Array
* start?: boolean
* }} SampleNodeOptions
*/
@@ -424,9 +425,15 @@ class SampleNode {
/** @type {number} */
this.offset = options.offset ?? 0;
/** @type {number} */
this._playbackPosition = options.offset;
/** @type {number} */
this.startTime = options.startTime ?? 0;
/** @type {boolean} */
this.isPaused = false;
/** @type {boolean} */
this.isStarted = false;
/** @type {boolean} */
this.isCanceled = false;
/** @type {number} */
this.pauseTime = 0;
/** @type {number} */
@@ -443,6 +450,8 @@ class SampleNode {
this._source = GodotAudio.ctx.createBufferSource();
/** @type {AudioBufferSourceNode["onended"]} */
this._onended = null;
/** @type {AudioWorkletNode | null} */
this._positionWorklet = null;
this.setPlaybackRate(options.playbackRate ?? 44100);
this.loopMode = options.loopMode ?? this.getSample().loopMode ?? 'disabled';
@@ -453,6 +462,8 @@ class SampleNode {
const bus = GodotAudio.Bus.getBus(params.busIndex);
const sampleNodeBus = this.getSampleNodeBus(bus);
sampleNodeBus.setVolume(options.volume);
this.connectPositionWorklet(options.start);
}
/**
@@ -463,6 +474,14 @@ class SampleNode {
return this._playbackRate;
}
/**
* Gets the playback position.
* @returns {number}
*/
getPlaybackPosition() {
return this._playbackPosition;
}
/**
* Sets the playback rate.
* @param {number} val Value to set.
@@ -512,8 +531,12 @@ class SampleNode {
* @returns {void}
*/
start() {
if (this.isStarted) {
return;
}
this._resetSourceStartTime();
this._source.start(this.startTime, this.offset);
this.isStarted = true;
}
/**
@@ -588,18 +611,74 @@ class SampleNode {
return this._sampleNodeBuses.get(bus);
}
/**
* Sets up and connects the source to the GodotPositionReportingProcessor
* If the worklet module is not loaded in, it will be added
*/
connectPositionWorklet(start) {
try {
this._positionWorklet = this.createPositionWorklet();
this._source.connect(this._positionWorklet);
if (start) {
this.start();
}
} catch (error) {
if (error?.name !== 'InvalidStateError') {
throw error;
}
const path = GodotConfig.locate_file('godot.audio.position.worklet.js');
GodotAudio.ctx.audioWorklet
.addModule(path)
.then(() => {
if (!this.isCanceled) {
this._positionWorklet = this.createPositionWorklet();
this._source.connect(this._positionWorklet);
if (start) {
this.start();
}
}
}).catch((addModuleError) => {
GodotRuntime.error('Failed to create PositionWorklet.', addModuleError);
});
}
}
/**
* Creates the AudioWorkletProcessor used to track playback position.
* @returns {AudioWorkletNode}
*/
createPositionWorklet() {
const worklet = new AudioWorkletNode(
GodotAudio.ctx,
'godot-position-reporting-processor'
);
worklet.port.onmessage = (event) => {
switch (event.data['type']) {
case 'position':
this._playbackPosition = (parseInt(event.data.data, 10) / this.getSample().sampleRate) + this.offset;
break;
default:
// Do nothing.
}
};
return worklet;
}
/**
* Clears the `SampleNode`.
* @returns {void}
*/
clear() {
this.isCanceled = true;
this.isPaused = false;
this.pauseTime = 0;
if (this._source != null) {
this._source.removeEventListener('ended', this._onended);
this._onended = null;
this._source.stop();
if (this.isStarted) {
this._source.stop();
}
this._source.disconnect();
this._source = null;
}
@@ -609,6 +688,12 @@ class SampleNode {
}
this._sampleNodeBuses.clear();
if (this._positionWorklet) {
this._positionWorklet.disconnect();
this._positionWorklet.port.onmessage = null;
this._positionWorklet = null;
}
GodotAudio.SampleNode.delete(this.id);
}
@@ -647,7 +732,9 @@ class SampleNode {
const pauseTime = this.isPaused
? this.pauseTime
: 0;
this.connectPositionWorklet();
this._source.start(this.startTime, this.offset + pauseTime);
this.isStarted = true;
}
/**
@@ -1252,7 +1339,7 @@ const _GodotAudio = {
startOptions
) {
GodotAudio.SampleNode.stopSampleNode(playbackObjectId);
const sampleNode = GodotAudio.SampleNode.create(
GodotAudio.SampleNode.create(
{
busIndex,
id: playbackObjectId,
@@ -1260,7 +1347,6 @@ const _GodotAudio = {
},
startOptions
);
sampleNode.start();
},
/**
@@ -1580,6 +1666,7 @@ const _GodotAudio = {
offset,
volume,
playbackRate: 1,
start: true,
};
GodotAudio.start_sample(
playbackObjectId,
@@ -1625,6 +1712,22 @@ const _GodotAudio = {
return Number(GodotAudio.sampleNodes.has(playbackObjectId));
},
godot_audio_get_sample_playback_position__proxy: 'sync',
godot_audio_get_sample_playback_position__sig: 'di',
/**
* Returns the position of the playback position.
* @param {number} playbackObjectIdStrPtr Playback object id pointer
* @returns {number}
*/
godot_audio_get_sample_playback_position: function (playbackObjectIdStrPtr) {
const playbackObjectId = GodotRuntime.parseString(playbackObjectIdStrPtr);
const sampleNode = GodotAudio.SampleNode.getSampleNodeOrNull(playbackObjectId);
if (sampleNode == null) {
return 0;
}
return sampleNode.getPlaybackPosition();
},
godot_audio_sample_update_pitch_scale__proxy: 'sync',
godot_audio_sample_update_pitch_scale__sig: 'vii',
/**