瀏覽代碼

Merge pull request #109790 from adamscott/tentative-fix-for-109144

[Web] Fix `AudioStreamPlayer.get_playback_position()` returning incorrect values for samples
Thaddeus Crews 2 周之前
父節點
當前提交
17fb6e3bd0
共有 2 個文件被更改,包括 23 次插入12 次删除
  1. 17 11
      platform/web/js/libs/audio.position.worklet.js
  2. 6 1
      platform/web/js/libs/library_godot_audio.js

+ 17 - 11
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) {

+ 6 - 1
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;
 	}