Skip to content

Commit cdb6fc7

Browse files
committed
[Web] Remove position pool system and return false when done instead
1 parent 172fc62 commit cdb6fc7

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ class GodotPositionReportingProcessor extends AudioWorkletProcessor {
3535
super(...args);
3636
this.lastPostTime = currentTime;
3737
this.position = 0;
38+
this.ended = false;
3839

3940
this.port.onmessage = (event) => {
40-
if (event?.data?.type === 'clear') {
41-
this.lastPostTime = currentTime;
42-
this.position = 0;
41+
if (event?.data?.type === 'ended') {
42+
this.ended = true;
4343
}
4444
};
4545
}
4646

4747
process(inputs, _outputs, _parameters) {
48+
if (this.ended) {
49+
return false;
50+
}
51+
4852
if (inputs.length > 0) {
4953
const input = inputs[0];
5054
if (input.length > 0) {
@@ -55,7 +59,7 @@ class GodotPositionReportingProcessor extends AudioWorkletProcessor {
5559
// Posting messages is expensive. Let's limit the number of posts.
5660
if (currentTime - this.lastPostTime > POST_THRESHOLD_S) {
5761
this.lastPostTime = currentTime;
58-
this.port.postMessage({ 'type': 'position', 'data': this.position });
62+
this.port.postMessage({ type: 'position', data: this.position });
5963
}
6064

6165
return true;

platform/web/js/libs/library_godot_audio.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -621,28 +621,24 @@ class SampleNode {
621621
if (this.isCanceled) {
622622
return;
623623
}
624-
this.getPositionWorklet();
625-
this._source.connect(this._positionWorklet);
624+
this._source.connect(this.getPositionWorklet());
626625
if (start) {
627626
this.start();
628627
}
629628
}
630629

631630
/**
632-
* Get a AudioWorkletProcessor from the pool, or create one if no processor is available.
631+
* Get a AudioWorkletProcessor
632+
* @returns {AudioWorkletNode}
633633
*/
634634
getPositionWorklet() {
635635
if (this._positionWorklet != null) {
636-
return;
637-
}
638-
if (GodotAudio.audioPositionWorkletPool.length == 0) {
639-
this._positionWorklet = new AudioWorkletNode(
640-
GodotAudio.ctx,
641-
'godot-position-reporting-processor'
642-
);
643-
} else {
644-
this._positionWorklet = GodotAudio.audioPositionWorkletPool.pop();
636+
return this._positionWorklet;
645637
}
638+
this._positionWorklet = new AudioWorkletNode(
639+
GodotAudio.ctx,
640+
'godot-position-reporting-processor'
641+
);
646642
this._positionWorklet.port.onmessage = (event) => {
647643
switch (event.data['type']) {
648644
case 'position':
@@ -652,6 +648,7 @@ class SampleNode {
652648
// Do nothing.
653649
}
654650
};
651+
return this._positionWorklet;
655652
}
656653

657654
/**
@@ -681,8 +678,7 @@ class SampleNode {
681678
if (this._positionWorklet) {
682679
this._positionWorklet.disconnect();
683680
this._positionWorklet.port.onmessage = null;
684-
this._positionWorklet.port.postMessage({ type: 'clear' });
685-
GodotAudio.audioPositionWorkletPool.push(this._positionWorklet);
681+
this._positionWorklet.port.postMessage({ type: 'ended' });
686682
this._positionWorklet = null;
687683
}
688684

@@ -1199,7 +1195,6 @@ const _GodotAudio = {
11991195

12001196
/** @type {Promise} */
12011197
audioPositionWorkletPromise: null,
1202-
audioPositionWorkletPool: [],
12031198

12041199
/**
12051200
* Converts linear volume to Db.

0 commit comments

Comments
 (0)