1
0
Fork 0

Merge pull request #102715 from adamscott/remove-pool-return-false

[Web] Remove position pool system and return false when done instead
This commit is contained in:
Rémi Verschelde 2025-02-11 23:59:29 +01:00
commit 66d66807ab
2 changed files with 18 additions and 19 deletions

View File

@ -35,16 +35,20 @@ class GodotPositionReportingProcessor extends AudioWorkletProcessor {
super(...args); super(...args);
this.lastPostTime = currentTime; this.lastPostTime = currentTime;
this.position = 0; this.position = 0;
this.ended = false;
this.port.onmessage = (event) => { this.port.onmessage = (event) => {
if (event?.data?.type === 'clear') { if (event?.data?.type === 'ended') {
this.lastPostTime = currentTime; this.ended = true;
this.position = 0;
} }
}; };
} }
process(inputs, _outputs, _parameters) { process(inputs, _outputs, _parameters) {
if (this.ended) {
return false;
}
if (inputs.length > 0) { if (inputs.length > 0) {
const input = inputs[0]; const input = inputs[0];
if (input.length > 0) { if (input.length > 0) {
@ -55,7 +59,7 @@ class GodotPositionReportingProcessor extends AudioWorkletProcessor {
// Posting messages is expensive. Let's limit the number of posts. // Posting messages is expensive. Let's limit the number of posts.
if (currentTime - this.lastPostTime > POST_THRESHOLD_S) { if (currentTime - this.lastPostTime > POST_THRESHOLD_S) {
this.lastPostTime = currentTime; this.lastPostTime = currentTime;
this.port.postMessage({ 'type': 'position', 'data': this.position }); this.port.postMessage({ type: 'position', data: this.position });
} }
return true; return true;

View File

@ -621,28 +621,24 @@ class SampleNode {
if (this.isCanceled) { if (this.isCanceled) {
return; return;
} }
this.getPositionWorklet(); this._source.connect(this.getPositionWorklet());
this._source.connect(this._positionWorklet);
if (start) { if (start) {
this.start(); this.start();
} }
} }
/** /**
* Get a AudioWorkletProcessor from the pool, or create one if no processor is available. * Get a AudioWorkletProcessor
* @returns {AudioWorkletNode}
*/ */
getPositionWorklet() { getPositionWorklet() {
if (this._positionWorklet != null) { if (this._positionWorklet != null) {
return; return this._positionWorklet;
} }
if (GodotAudio.audioPositionWorkletPool.length == 0) {
this._positionWorklet = new AudioWorkletNode( this._positionWorklet = new AudioWorkletNode(
GodotAudio.ctx, GodotAudio.ctx,
'godot-position-reporting-processor' 'godot-position-reporting-processor'
); );
} else {
this._positionWorklet = GodotAudio.audioPositionWorkletPool.pop();
}
this._positionWorklet.port.onmessage = (event) => { this._positionWorklet.port.onmessage = (event) => {
switch (event.data['type']) { switch (event.data['type']) {
case 'position': case 'position':
@ -652,6 +648,7 @@ class SampleNode {
// Do nothing. // Do nothing.
} }
}; };
return this._positionWorklet;
} }
/** /**
@ -681,8 +678,7 @@ class SampleNode {
if (this._positionWorklet) { if (this._positionWorklet) {
this._positionWorklet.disconnect(); this._positionWorklet.disconnect();
this._positionWorklet.port.onmessage = null; this._positionWorklet.port.onmessage = null;
this._positionWorklet.port.postMessage({ type: 'clear' }); this._positionWorklet.port.postMessage({ type: 'ended' });
GodotAudio.audioPositionWorkletPool.push(this._positionWorklet);
this._positionWorklet = null; this._positionWorklet = null;
} }
@ -1199,7 +1195,6 @@ const _GodotAudio = {
/** @type {Promise} */ /** @type {Promise} */
audioPositionWorkletPromise: null, audioPositionWorkletPromise: null,
audioPositionWorkletPool: [],
/** /**
* Converts linear volume to Db. * Converts linear volume to Db.