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(sdk): audio ducking and BGM crossfade tear down cleanly

esengine5c91a9485454
diff --git a/sdk/src/audio/Audio.ts b/sdk/src/audio/Audio.ts
index 214af1918..cfafae6fc 100644
--- a/sdk/src/audio/Audio.ts
+++ b/sdk/src/audio/Audio.ts
@@ -50,6 +50,10 @@ export class AudioAPI {
private bgmHandle_: AudioHandle | null = null;
private bgmVolume_ = 1.0;
private readonly fadeAnimIds_ = new Set<number>();
+ // Handles mid fade-out. A cancelled fade RAF never reaches its handle.stop(),
+ // so a rapid crossfade would orphan the outgoing track (playing forever) —
+ // {@link cancelFades_} stops these when it tears the animations down.
+ private readonly fadingOut_ = new Set<AudioHandle>();
private disposed_ = false;
private assetResolver_: ((url: string) => ArrayBuffer | null) | null = null;
private refResolver_: ((ref: string) => string) | null = null;
@@ -301,10 +305,7 @@ export class AudioAPI {
crossFade?: number;
}): void {
const play = (buffer: AudioBufferHandle) => {
- for (const id of this.fadeAnimIds_) {
- cancelAnimationFrame(id);
- }
- this.fadeAnimIds_.clear();
+ this.cancelFades_();
const targetVolume = config?.volume ?? 1.0;
const oldVolume = this.bgmVolume_;
@@ -345,10 +346,7 @@ export class AudioAPI {
}
stopAll(): void {
- for (const id of this.fadeAnimIds_) {
- cancelAnimationFrame(id);
- }
- this.fadeAnimIds_.clear();
+ this.cancelFades_();
if (this.bgmHandle_) {
this.bgmHandle_.stop();
this.bgmHandle_ = null;
@@ -357,10 +355,7 @@ export class AudioAPI {
stopBGM(fadeOut?: number): void {
if (!this.bgmHandle_) return;
- for (const id of this.fadeAnimIds_) {
- cancelAnimationFrame(id);
- }
- this.fadeAnimIds_.clear();
+ this.cancelFades_();
if (fadeOut && fadeOut > 0) {
const handle = this.bgmHandle_;
this.bgmHandle_ = null;
@@ -487,10 +482,7 @@ export class AudioAPI {
dispose(): void {
this.disposed_ = true;
- for (const id of this.fadeAnimIds_) {
- cancelAnimationFrame(id);
- }
- this.fadeAnimIds_.clear();
+ this.cancelFades_();
if (this.bgmHandle_) {
this.bgmHandle_.stop();
this.bgmHandle_ = null;
@@ -523,7 +515,17 @@ export class AudioAPI {
this.fadeAnimIds_.add(animId);
}
+ /** Cancel every in-flight fade AND stop the tracks that were fading out — a
+ * cancelled fade RAF would otherwise never reach their `stop()`. */
+ private cancelFades_(): void {
+ for (const id of this.fadeAnimIds_) cancelAnimationFrame(id);
+ this.fadeAnimIds_.clear();
+ for (const h of this.fadingOut_) h.stop();
+ this.fadingOut_.clear();
+ }
+
private fadeOut_(handle: AudioHandle, duration: number, startVolume: number): void {
+ this.fadingOut_.add(handle);
const startTime = performance.now();
let animId = 0;
const tick = () => {
@@ -536,6 +538,7 @@ export class AudioAPI {
} else {
handle.stop();
this.fadeAnimIds_.delete(animId);
+ this.fadingOut_.delete(handle);
}
};
animId = requestAnimationFrame(tick);
diff --git a/sdk/src/audio/AudioMixer.ts b/sdk/src/audio/AudioMixer.ts
index 6b300d89b..dd4efbd50 100644
--- a/sdk/src/audio/AudioMixer.ts
+++ b/sdk/src/audio/AudioMixer.ts
@@ -98,7 +98,10 @@ export class AudioMixer {
if (!targetBus) return false;
const existing = this.ducks_.get(target);
if (existing) {
- existing.analyser.disconnect();
+ // The tap is trigger.node → analyser (INCOMING to the analyser), so
+ // disconnect it from the trigger; analyser.disconnect() drops the
+ // analyser's outputs (none) and leaks the tap on every re-apply.
+ this.buses_.get(existing.rule.trigger)?.node.disconnect(existing.analyser);
this.ducks_.delete(target);
targetBus.duckTo(1, existing.rule.release ?? 0.4);
}
diff --git a/sdk/tests/audio-bus-effects.test.ts b/sdk/tests/audio-bus-effects.test.ts
index 8c92b8bca..5b3028d57 100644
--- a/sdk/tests/audio-bus-effects.test.ts
+++ b/sdk/tests/audio-bus-effects.test.ts
@@ -138,11 +138,13 @@ describe('AudioMixer ducking', () => {
expect(musicDuck.gain.setTargetAtTime).toHaveBeenLastCalledWith(1, 0, 0.4);
});
- it('clearing the rule disconnects the tap and restores the duck stage', () => {
+ it('clearing the rule disconnects the tap from the trigger and restores the duck stage', () => {
mixer.setDucking('music', { trigger: 'voice', amount: 0.3 });
const analyser = ctx.createAnalyser.mock.results[0].value;
mixer.setDucking('music', null);
- expect(analyser.disconnect).toHaveBeenCalled();
+ // The tap is trigger.node → analyser, so it must be torn down from the
+ // trigger node (analyser.disconnect() would drop nothing and leak the tap).
+ expect(mixer.voice.node.disconnect).toHaveBeenCalledWith(analyser);
expect(mixer.getDucking('music')).toBeNull();
});
});