From 21dd72bc2187069b2a1fa51286a63170480a219e Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Tue, 19 Aug 2025 19:26:26 -0400 Subject: [PATCH] [Web] Fix `AudioStreamPlayer.get_playback_position()` returning incorrect values for samples --- .../web/js/libs/audio.position.worklet.js | 28 +++++++++++-------- platform/web/js/libs/library_godot_audio.js | 7 ++++- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/platform/web/js/libs/audio.position.worklet.js b/platform/web/js/libs/audio.position.worklet.js index 1237e843f9a..4e512c1e3c9 100644 --- a/platform/web/js/libs/audio.position.worklet.js +++ b/platform/web/js/libs/audio.position.worklet.js @@ -29,22 +29,28 @@ /**************************************************************************/ class GodotPositionReportingProcessor extends AudioWorkletProcessor { + static get parameterDescriptors() { + return [ + { + name: 'reset', + defaultValue: 0, + minValue: 0, + maxValue: 1, + automationRate: 'k-rate', + }, + ]; + } + constructor(...args) { super(...args); this.position = 0; - - this.port.onmessage = (event) => { - switch (event?.data?.type) { - case 'reset': - this.position = 0; - break; - default: - // Do nothing. - } - }; } - process(inputs, _outputs, _parameters) { + process(inputs, _outputs, parameters) { + if (parameters['reset'][0] > 0) { + this.position = 0; + } + if (inputs.length > 0) { const input = inputs[0]; if (input.length > 0) { diff --git a/platform/web/js/libs/library_godot_audio.js b/platform/web/js/libs/library_godot_audio.js index 4eb69483edf..d1c996efe46 100644 --- a/platform/web/js/libs/library_godot_audio.js +++ b/platform/web/js/libs/library_godot_audio.js @@ -643,6 +643,7 @@ class SampleNode { 'godot-position-reporting-processor' ); } + this._playbackPosition = this.offset; this._positionWorklet.port.onmessage = (event) => { switch (event.data['type']) { case 'position': @@ -652,7 +653,11 @@ class SampleNode { // Do nothing. } }; - this._positionWorklet.port.postMessage('reset'); + + const resetParameter = this._positionWorklet.parameters.get('reset'); + resetParameter.setValueAtTime(1, GodotAudio.ctx.currentTime); + resetParameter.setValueAtTime(0, GodotAudio.ctx.currentTime + 1); + return this._positionWorklet; }