Skip to content

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.

fix(audio): preserve playback position across rate changes

yhhf5432b18b1bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b98e6a5f..17697ce41 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **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.
+
- **Playing audio sources follow live parameter edits.** Volume, pitch and loop changes update the existing voice without restarting it. Switching spatial sound off restores base gain and centered pan; stable nonspatial sources do not resend unchanged parameters, and voice cleanup avoids copying its key list every frame.
- **UI ancestor walks terminate on malformed hierarchy adapters.** Depth counting and parent callbacks stop before repeating an entity or following an ancestor invalidated by a callback. Shallow chains use bounded array scans, deeper chains switch to a set, and long valid chains remain iterative with early callback termination preserved.
diff --git a/sdk/src/audio/WebAudioBackend.ts b/sdk/src/audio/WebAudioBackend.ts
index 78b841cfc..6cdc39b34 100644
--- a/sdk/src/audio/WebAudioBackend.ts
+++ b/sdk/src/audio/WebAudioBackend.ts
@@ -110,10 +110,14 @@ class WebAudioHandle implements AudioHandle {
}
setPlaybackRate(rate: number): void {
- this.playbackRate_ = rate;
if (this.playing_) {
+ const now = this.context_.currentTime;
+ const position = this.startOffset_ + (now - this.startedAt_) * this.playbackRate_;
this.source_.playbackRate.value = rate;
+ this.startOffset_ = position;
+ this.startedAt_ = now;
}
+ this.playbackRate_ = rate;
}
get isPlaying(): boolean {
diff --git a/sdk/tests/web-audio-backend.test.ts b/sdk/tests/web-audio-backend.test.ts
index 1d19d86b6..7a6233304 100644
--- a/sdk/tests/web-audio-backend.test.ts
+++ b/sdk/tests/web-audio-backend.test.ts
@@ -311,6 +311,59 @@ describe('WebAudioBackend', () => {
});
describe('pause/resume', () => {
+ it('rate changes keep the accumulated position continuous', async () => {
+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);
+ const handle = backend.play(bufferHandle, {});
+ mockCtx.currentTime = .25;
+ expect(handle.currentTime).toBeCloseTo(.25);
+ handle.setPlaybackRate(2);
+ expect(handle.currentTime).toBeCloseTo(.25);
+ mockCtx.currentTime = .5;
+ expect(handle.currentTime).toBeCloseTo(.75);
+ handle.setPlaybackRate(.5);
+ expect(handle.currentTime).toBeCloseTo(.75);
+ mockCtx.currentTime = .75;
+ expect(handle.currentTime).toBeCloseTo(.875);
+ });
+
+ it('pause and resume use the position accumulated across rate changes', async () => {
+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);
+ const handle = backend.play(bufferHandle, {});
+ mockCtx.currentTime = .25; handle.setPlaybackRate(2);
+ mockCtx.currentTime = .5; handle.pause();
+ expect(handle.currentTime).toBeCloseTo(.75);
+ handle.setPlaybackRate(.5);
+ mockCtx.currentTime = 10; handle.resume();
+ const source = mockCtx.createBufferSource.mock.results.at(-1).value;
+ expect(source.start).toHaveBeenCalledWith(0, expect.closeTo(.75, 5));
+ expect(source.playbackRate.value).toBe(.5);
+ mockCtx.currentTime = 10.25;
+ expect(handle.currentTime).toBeCloseTo(.875);
+ });
+
+ it('looping pause wraps the accumulated position after a rate change', async () => {
+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);
+ const handle = backend.play(bufferHandle, { loop: true });
+ mockCtx.currentTime = .25; handle.setPlaybackRate(2);
+ mockCtx.currentTime = 1.25; handle.pause();
+ expect(handle.currentTime).toBeCloseTo(.25);
+ handle.resume();
+ const source = mockCtx.createBufferSource.mock.results.at(-1).value;
+ expect(source.start).toHaveBeenCalledWith(0, expect.closeTo(.25, 5));
+ });
+
+ it('a rejected backend rate change leaves position tracking intact', async () => {
+ const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);
+ const handle = backend.play(bufferHandle, {});
+ const source = mockCtx.createBufferSource.mock.results.at(-1).value;
+ Object.defineProperty(source.playbackRate, 'value', { get: () => 1,
+ set: () => { throw new Error('rate rejected'); } });
+ mockCtx.currentTime = .25;
+ expect(() => handle.setPlaybackRate(2)).toThrow('rate rejected');
+ mockCtx.currentTime = .5;
+ expect(handle.currentTime).toBeCloseTo(.5);
+ });
+
it('should pause by stopping source and track position', async () => {
const { bufferHandle, mockCtx } = await initAndLoadBuffer(backend);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index e114f47a1..4e8819cc4 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): preserve playback position across rate changes':
+ { note: 'WebAudio position stays continuous across playback-rate changes.' },
'fix(audio): apply live source parameters without restarting voices':
{ note: 'Playing audio sources follow live parameter edits.' },
'fix(ui): bound ancestor traversal across cyclic hierarchy adapters':