Skip to content

Commit 21dd72b

Browse files
committed
[Web] Fix AudioStreamPlayer.get_playback_position() returning incorrect values for samples
1 parent 28bd72a commit 21dd72b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

platform/web/js/libs/audio.position.worklet.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@
2929
/**************************************************************************/
3030

3131
class GodotPositionReportingProcessor extends AudioWorkletProcessor {
32+
static get parameterDescriptors() {
33+
return [
34+
{
35+
name: 'reset',
36+
defaultValue: 0,
37+
minValue: 0,
38+
maxValue: 1,
39+
automationRate: 'k-rate',
40+
},
41+
];
42+
}
43+
3244
constructor(...args) {
3345
super(...args);
3446
this.position = 0;
35-
36-
this.port.onmessage = (event) => {
37-
switch (event?.data?.type) {
38-
case 'reset':
39-
this.position = 0;
40-
break;
41-
default:
42-
// Do nothing.
43-
}
44-
};
4547
}
4648

47-
process(inputs, _outputs, _parameters) {
49+
process(inputs, _outputs, parameters) {
50+
if (parameters['reset'][0] > 0) {
51+
this.position = 0;
52+
}
53+
4854
if (inputs.length > 0) {
4955
const input = inputs[0];
5056
if (input.length > 0) {

platform/web/js/libs/library_godot_audio.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ class SampleNode {
643643
'godot-position-reporting-processor'
644644
);
645645
}
646+
this._playbackPosition = this.offset;
646647
this._positionWorklet.port.onmessage = (event) => {
647648
switch (event.data['type']) {
648649
case 'position':
@@ -652,7 +653,11 @@ class SampleNode {
652653
// Do nothing.
653654
}
654655
};
655-
this._positionWorklet.port.postMessage('reset');
656+
657+
const resetParameter = this._positionWorklet.parameters.get('reset');
658+
resetParameter.setValueAtTime(1, GodotAudio.ctx.currentTime);
659+
resetParameter.setValueAtTime(0, GodotAudio.ctx.currentTime + 1);
660+
656661
return this._positionWorklet;
657662
}
658663

0 commit comments

Comments
 (0)