A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(audio): guard reused pool nodes from retired handles
yhhaf3141b4d9e3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6c32cbac5..9d0fa3b54 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Retired WebAudio handles cannot modify reused pool nodes.** Gain and pan writes from stopped or completed handles are ignored, preserving the next voice's mix. Completion callbacks are checked against the currently playing source, so an old source cannot release the pool slot of a resumed voice.+- **Paused voices remain owned by the audio API.** Paused handles retain their voice-budget slot, bus volume and mute updates, and disposal cleanup; stolen paused voices cannot resume outside the budget. Handle volume edits update the tracked base gain, fades stay associated with managed handles, and ended ramps stop writing to released backend voices.- **WebAudio position stays continuous across playback-rate changes.** Rate edits anchor the accumulated position before applying the new rate, so pause/resume and loop offsets use the time actually played at each rate. A rate rejected by the backend leaves the existing clock intact; paused edits apply when playback resumes.diff --git a/sdk/src/audio/WebAudioBackend.ts b/sdk/src/audio/WebAudioBackend.tsindex 6cdc39b34..3683ec6f7 100644--- a/sdk/src/audio/WebAudioBackend.ts+++ b/sdk/src/audio/WebAudioBackend.ts@@ -47,7 +47,7 @@ class WebAudioHandle implements AudioHandle {private bindOnEnded_(source: AudioBufferSourceNode): void {source.onended = () => {- if (!this.stopped_ && !this.loop_) {+ if (!this.stopped_ && this.playing_ && source === this.source_ && !this.loop_) {this.playing_ = false;this.stopped_ = true;this.pool_.release(this.poolNode_);@@ -97,10 +97,12 @@ class WebAudioHandle implements AudioHandle {}setVolume(volume: number): void {+ if (this.stopped_) return;this.poolNode_.gain.gain.value = volume;}setPan(pan: number): void {+ if (this.stopped_) return;this.poolNode_.panner.pan.value = pan;}diff --git a/sdk/tests/web-audio-backend.test.ts b/sdk/tests/web-audio-backend.test.tsindex 7a6233304..2973a501b 100644--- a/sdk/tests/web-audio-backend.test.ts+++ b/sdk/tests/web-audio-backend.test.ts@@ -264,6 +264,19 @@ describe('WebAudioBackend', () => {});describe('play', () => {+ it.each(['stopped', 'ended'])('a %s handle cannot change gain or pan of a reused pool node', async kind => {+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);+ const old = backend.play(bufferHandle, {});+ const first = mockCtx.createBufferSource.mock.results.at(-1).value;+ const gain = first.connect.mock.calls[0][0];+ const panner = gain.connect.mock.calls[0][0];+ if (kind === 'stopped') old.stop(); else first.onended();+ backend.play(bufferHandle, { volume: .7, pan: .3 });+ const next = mockCtx.createBufferSource.mock.results.at(-1).value;+ expect(next.connect.mock.calls[0][0]).toBe(gain);+ old.setVolume(.1); old.setPan(-.9);+ expect(gain.gain.value).toBe(.7); expect(panner.pan.value).toBe(.3);+ });it('should create source, connect to bus, and start', async () => {const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);const audioHandle = backend.play(bufferHandle, { volume: 0.8, bus: 'sfx' });@@ -311,6 +324,14 @@ describe('WebAudioBackend', () => {});describe('pause/resume', () => {+ it('a retired source completion cannot end a resumed voice', async () => {+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);+ const h = backend.play(bufferHandle, {});+ const oldSource = mockCtx.createBufferSource.mock.results.at(-1).value;+ const completed = oldSource.onended;+ mockCtx.currentTime = .25; h.pause(); h.resume(); completed();+ expect(h.isPlaying).toBe(true);+ });it('rate changes keep the accumulated position continuous', async () => {const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);const handle = backend.play(bufferHandle, {});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex f7ab81d5e..230b867d2 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(audio): guard reused pool nodes from retired handles':+ { note: 'Retired WebAudio handles cannot modify reused pool nodes.' },'fix(audio): retain paused voice ownership and tracked base gains':{ note: 'Paused voices remain owned by the audio API.' },'fix(audio): preserve playback position across rate changes':