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

@ -292,6 +292,11 @@ bool AudioDriverWeb::is_sample_playback_active(const Ref<AudioSamplePlayback> &p
return godot_audio_sample_is_active(itos(p_playback->get_instance_id()).utf8().get_data()) != 0; return godot_audio_sample_is_active(itos(p_playback->get_instance_id()).utf8().get_data()) != 0;
} }
double AudioDriverWeb::get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null.");
return godot_audio_get_sample_playback_position(itos(p_playback->get_instance_id()).utf8().get_data());
}
void AudioDriverWeb::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) { void AudioDriverWeb::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
godot_audio_sample_update_pitch_scale( godot_audio_sample_update_pitch_scale(

View File

@ -95,6 +95,7 @@ public:
virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override; virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) override; virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) override;
virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) override; virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) override;
virtual double get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) override;
virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) override; virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) override;
virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) override; virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) override;

View File

@ -51,11 +51,13 @@ def create_template_zip(env, js, wasm, worker, side):
js, js,
wasm, wasm,
"#platform/web/js/libs/audio.worklet.js", "#platform/web/js/libs/audio.worklet.js",
"#platform/web/js/libs/audio.position.worklet.js",
] ]
out_files = [ out_files = [
zip_dir.File(binary_name + ".js"), zip_dir.File(binary_name + ".js"),
zip_dir.File(binary_name + ".wasm"), zip_dir.File(binary_name + ".wasm"),
zip_dir.File(binary_name + ".audio.worklet.js"), zip_dir.File(binary_name + ".audio.worklet.js"),
zip_dir.File(binary_name + ".audio.position.worklet.js"),
] ]
if env["threads"]: if env["threads"]:
in_files.append(worker) in_files.append(worker)
@ -74,6 +76,7 @@ def create_template_zip(env, js, wasm, worker, side):
"offline.html", "offline.html",
"godot.editor.js", "godot.editor.js",
"godot.editor.audio.worklet.js", "godot.editor.audio.worklet.js",
"godot.editor.audio.position.worklet.js",
"logo.svg", "logo.svg",
"favicon.png", "favicon.png",
] ]

View File

@ -242,6 +242,7 @@ Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_prese
} }
cache_files.push_back(name + ".worker.js"); cache_files.push_back(name + ".worker.js");
cache_files.push_back(name + ".audio.worklet.js"); cache_files.push_back(name + ".audio.worklet.js");
cache_files.push_back(name + ".audio.position.worklet.js");
replaces["___GODOT_CACHE___"] = Variant(cache_files).to_json_string(); replaces["___GODOT_CACHE___"] = Variant(cache_files).to_json_string();
// Heavy files that are cached on demand. // Heavy files that are cached on demand.
@ -835,6 +836,7 @@ Error EditorExportPlatformWeb::_export_project(const Ref<EditorExportPreset> &p_
DirAccess::remove_file_or_error(basepath + ".js"); DirAccess::remove_file_or_error(basepath + ".js");
DirAccess::remove_file_or_error(basepath + ".worker.js"); DirAccess::remove_file_or_error(basepath + ".worker.js");
DirAccess::remove_file_or_error(basepath + ".audio.worklet.js"); DirAccess::remove_file_or_error(basepath + ".audio.worklet.js");
DirAccess::remove_file_or_error(basepath + ".audio.position.worklet.js");
DirAccess::remove_file_or_error(basepath + ".service.worker.js"); DirAccess::remove_file_or_error(basepath + ".service.worker.js");
DirAccess::remove_file_or_error(basepath + ".pck"); DirAccess::remove_file_or_error(basepath + ".pck");
DirAccess::remove_file_or_error(basepath + ".png"); DirAccess::remove_file_or_error(basepath + ".png");

View File

@ -55,6 +55,7 @@ extern void godot_audio_sample_start(const char *p_playback_object_id, const cha
extern void godot_audio_sample_stop(const char *p_playback_object_id); extern void godot_audio_sample_stop(const char *p_playback_object_id);
extern void godot_audio_sample_set_pause(const char *p_playback_object_id, bool p_pause); extern void godot_audio_sample_set_pause(const char *p_playback_object_id, bool p_pause);
extern int godot_audio_sample_is_active(const char *p_playback_object_id); extern int godot_audio_sample_is_active(const char *p_playback_object_id);
extern double godot_audio_get_sample_playback_position(const char *p_playback_object_id);
extern void godot_audio_sample_update_pitch_scale(const char *p_playback_object_id, float p_pitch_scale); extern void godot_audio_sample_update_pitch_scale(const char *p_playback_object_id, float p_pitch_scale);
extern void godot_audio_sample_set_volumes_linear(const char *p_playback_object_id, int *p_buses_buf, int p_buses_size, float *p_volumes_buf, int p_volumes_size); extern void godot_audio_sample_set_volumes_linear(const char *p_playback_object_id, int *p_buses_buf, int p_buses_size, float *p_volumes_buf, int p_volumes_size);

View File

@ -299,6 +299,8 @@ const InternalConfig = function (initConfig) { // eslint-disable-line no-unused-
return `${loadPath}.worker.js`; return `${loadPath}.worker.js`;
} else if (path.endsWith('.audio.worklet.js')) { } else if (path.endsWith('.audio.worklet.js')) {
return `${loadPath}.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')) { } else if (path.endsWith('.js')) {
return `${loadPath}.js`; return `${loadPath}.js`;
} else if (path in gdext) { } 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 * startTime?: number
* loopMode?: LoopMode * loopMode?: LoopMode
* volume?: Float32Array * volume?: Float32Array
* start?: boolean
* }} SampleNodeOptions * }} SampleNodeOptions
*/ */
@ -424,9 +425,15 @@ class SampleNode {
/** @type {number} */ /** @type {number} */
this.offset = options.offset ?? 0; this.offset = options.offset ?? 0;
/** @type {number} */ /** @type {number} */
this._playbackPosition = options.offset;
/** @type {number} */
this.startTime = options.startTime ?? 0; this.startTime = options.startTime ?? 0;
/** @type {boolean} */ /** @type {boolean} */
this.isPaused = false; this.isPaused = false;
/** @type {boolean} */
this.isStarted = false;
/** @type {boolean} */
this.isCanceled = false;
/** @type {number} */ /** @type {number} */
this.pauseTime = 0; this.pauseTime = 0;
/** @type {number} */ /** @type {number} */
@ -443,6 +450,8 @@ class SampleNode {
this._source = GodotAudio.ctx.createBufferSource(); this._source = GodotAudio.ctx.createBufferSource();
/** @type {AudioBufferSourceNode["onended"]} */ /** @type {AudioBufferSourceNode["onended"]} */
this._onended = null; this._onended = null;
/** @type {AudioWorkletNode | null} */
this._positionWorklet = null;
this.setPlaybackRate(options.playbackRate ?? 44100); this.setPlaybackRate(options.playbackRate ?? 44100);
this.loopMode = options.loopMode ?? this.getSample().loopMode ?? 'disabled'; this.loopMode = options.loopMode ?? this.getSample().loopMode ?? 'disabled';
@ -453,6 +462,8 @@ class SampleNode {
const bus = GodotAudio.Bus.getBus(params.busIndex); const bus = GodotAudio.Bus.getBus(params.busIndex);
const sampleNodeBus = this.getSampleNodeBus(bus); const sampleNodeBus = this.getSampleNodeBus(bus);
sampleNodeBus.setVolume(options.volume); sampleNodeBus.setVolume(options.volume);
this.connectPositionWorklet(options.start);
} }
/** /**
@ -463,6 +474,14 @@ class SampleNode {
return this._playbackRate; return this._playbackRate;
} }
/**
* Gets the playback position.
* @returns {number}
*/
getPlaybackPosition() {
return this._playbackPosition;
}
/** /**
* Sets the playback rate. * Sets the playback rate.
* @param {number} val Value to set. * @param {number} val Value to set.
@ -512,8 +531,12 @@ class SampleNode {
* @returns {void} * @returns {void}
*/ */
start() { start() {
if (this.isStarted) {
return;
}
this._resetSourceStartTime(); this._resetSourceStartTime();
this._source.start(this.startTime, this.offset); this._source.start(this.startTime, this.offset);
this.isStarted = true;
} }
/** /**
@ -588,18 +611,74 @@ class SampleNode {
return this._sampleNodeBuses.get(bus); 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`. * Clears the `SampleNode`.
* @returns {void} * @returns {void}
*/ */
clear() { clear() {
this.isCanceled = true;
this.isPaused = false; this.isPaused = false;
this.pauseTime = 0; this.pauseTime = 0;
if (this._source != null) { if (this._source != null) {
this._source.removeEventListener('ended', this._onended); this._source.removeEventListener('ended', this._onended);
this._onended = null; this._onended = null;
this._source.stop(); if (this.isStarted) {
this._source.stop();
}
this._source.disconnect(); this._source.disconnect();
this._source = null; this._source = null;
} }
@ -609,6 +688,12 @@ class SampleNode {
} }
this._sampleNodeBuses.clear(); this._sampleNodeBuses.clear();
if (this._positionWorklet) {
this._positionWorklet.disconnect();
this._positionWorklet.port.onmessage = null;
this._positionWorklet = null;
}
GodotAudio.SampleNode.delete(this.id); GodotAudio.SampleNode.delete(this.id);
} }
@ -647,7 +732,9 @@ class SampleNode {
const pauseTime = this.isPaused const pauseTime = this.isPaused
? this.pauseTime ? this.pauseTime
: 0; : 0;
this.connectPositionWorklet();
this._source.start(this.startTime, this.offset + pauseTime); this._source.start(this.startTime, this.offset + pauseTime);
this.isStarted = true;
} }
/** /**
@ -1252,7 +1339,7 @@ const _GodotAudio = {
startOptions startOptions
) { ) {
GodotAudio.SampleNode.stopSampleNode(playbackObjectId); GodotAudio.SampleNode.stopSampleNode(playbackObjectId);
const sampleNode = GodotAudio.SampleNode.create( GodotAudio.SampleNode.create(
{ {
busIndex, busIndex,
id: playbackObjectId, id: playbackObjectId,
@ -1260,7 +1347,6 @@ const _GodotAudio = {
}, },
startOptions startOptions
); );
sampleNode.start();
}, },
/** /**
@ -1580,6 +1666,7 @@ const _GodotAudio = {
offset, offset,
volume, volume,
playbackRate: 1, playbackRate: 1,
start: true,
}; };
GodotAudio.start_sample( GodotAudio.start_sample(
playbackObjectId, playbackObjectId,
@ -1625,6 +1712,22 @@ const _GodotAudio = {
return Number(GodotAudio.sampleNodes.has(playbackObjectId)); 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__proxy: 'sync',
godot_audio_sample_update_pitch_scale__sig: 'vii', godot_audio_sample_update_pitch_scale__sig: 'vii',
/** /**

View File

@ -1369,6 +1369,12 @@ bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) {
float AudioServer::get_playback_position(Ref<AudioStreamPlayback> p_playback) { float AudioServer::get_playback_position(Ref<AudioStreamPlayback> p_playback) {
ERR_FAIL_COND_V(p_playback.is_null(), 0); ERR_FAIL_COND_V(p_playback.is_null(), 0);
// Samples.
if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) {
Ref<AudioSamplePlayback> sample_playback = p_playback->get_sample_playback();
return AudioServer::get_singleton()->get_sample_playback_position(sample_playback);
}
AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback); AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
if (!playback_node) { if (!playback_node) {
return 0; return 0;
@ -1835,6 +1841,11 @@ bool AudioServer::is_sample_playback_active(const Ref<AudioSamplePlayback> &p_pl
return AudioDriver::get_singleton()->is_sample_playback_active(p_playback); return AudioDriver::get_singleton()->is_sample_playback_active(p_playback);
} }
double AudioServer::get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_V_MSG(p_playback.is_null(), false, "Parameter p_playback is null.");
return AudioDriver::get_singleton()->get_sample_playback_position(p_playback);
}
void AudioServer::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) { void AudioServer::update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
return AudioDriver::get_singleton()->update_sample_playback_pitch_scale(p_playback, p_pitch_scale); return AudioDriver::get_singleton()->update_sample_playback_pitch_scale(p_playback, p_pitch_scale);

View File

@ -141,6 +141,7 @@ public:
virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {} virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {}
virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) {} virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) {}
virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) { return false; } virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) { return false; }
virtual double get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) { return false; }
virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) {} virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) {}
virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) {} virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) {}
@ -482,6 +483,7 @@ public:
void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback); void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback);
void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused); void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused);
bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback); bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback);
double get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback);
void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f); void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f);
AudioServer(); AudioServer();