[GH-ISSUE #2750] [Issue]: Game-mode music overlap — stopMusic fade-out interval untracked, old + new music stack on scene change (+ crossfade glitches/leak) #1391

Closed
opened 2026-10-21 05:32:52 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2750

Originally assigned to: @SpicyMarinara on GitHub.

Summary

game-audio.ts has several audio-lifecycle defects centered on the music/ambient crossfade machinery. The flagship is that stopMusic()'s fade-out interval is never stored in this.fadeInterval, so it cannot be cancelled — and dispose() (called on scene-scope change and unmount) starts that uncancellable 2s fade while the next scene's playMusic() crossfades a new track in on top, producing audible overlap (the same class as prior bug #2647).

Sub-bug 1: Untracked fade-out -> overlap (#10/#46) [primary]

  • How it breaks: stopMusic (742-772) clears this.fadeInterval (the crossfade-in), nulls this.musicElement, then starts a local const interval = setInterval(...) (764-771) that is NEVER assigned to this.fadeInterval. playMusic's guard if (this.fadeInterval) clearInterval (662) finds nothing to cancel, so the old fade-out and the new crossfade-in run concurrently for ~2s. dispose() therefore never hard-stops music.
  • Repro: In game mode with music + a user gesture, trigger a scene-scope change (chat/game switch) that disposes then plays new music with a cached/local track (decode <2s). Two tracks overlap for ~2s. On unmount the interval keeps ticking on a detached element until self-clear.
  • Trace: GameSurface.tsx:2638 dispose() -> game-audio.ts:897 stopMusic() -> 764 untracked interval -> next scene playMusic() 662 this.fadeInterval is null -> 690 new crossfade-in runs alongside the 764 fade-out.

Sub-bug 2: Fallback silenced by suspended context (#11)

  • How it breaks: When Web Audio decode fails or the context won't reach 'running', startFallbackAudio (327-333) creates a plain <audio>, then applyVolume -> setElementLayerVolume -> getMediaGain calls ctx.createMediaElementSource(fallbackAudio) (283), re-attaching the element to the same suspended context. Once captured, the element's only output is the (silent) Web Audio graph.
  • Repro: On mobile/iOS/autoplay-blocked auto-start of music or ambient where the context can't resume, the 'fallback' plays inaudibly with no error.
  • Trace: createLoopingAudioLayer ready -> 347 resumeAudioContext fails -> throw -> catch 361 -> startFallbackAudio -> applyVolume -> getMediaGain -> createMediaElementSource on suspended ctx -> audio.play() resolves silent. (Contrast: playOneShot fallback uses setMediaElementVolume which sets audio.volume directly and never captures the element.)

Sub-bug 3: Volume slider during crossfade pops (#47)

  • How it breaks: setVolumes (868-881) unconditionally calls this.nextMusicElement?.setVolume(this.musicVolume) and this.musicElement?.setVolume(...). During a crossfade nextMusicElement is the fading-in track and musicElement the fading-out track; both are slammed to full via gain.setValueAtTime (immediate), then the next ≤50ms fade tick (690-706) yanks them back to the partial value. Cosmetic volume pop/stutter, self-heals in one tick.
  • Repro: Drag the music/master slider during the ~2s scene-music crossfade.

Sub-bug 4: Ambient overlap on rapid changes (#49)

  • How it breaks: Unlike music, ambient has only this.ambientElement (no nextAmbientElement); the pending next-ambient is a local var. Two playAmbient calls within one fetch/decode window both start their BufferSource on decode; the stale one is stopped only a tick later by its ready guard, producing a brief double-ambient blip with no synchronous cancel handle.

Suggested Fix

  • stopMusic: store its fade interval in this.fadeInterval (and stop the captured audio on a second stop/dispose). Give dispose() an immediate hard-cut path that calls audio.stop() right away instead of fading into the next scene.
  • Fallback path: when the context is not 'running', set fallbackAudio.volume directly and skip getMediaGain/createMediaElementSource (or gate getMediaGain on ctx.state === 'running').
  • setVolumes: during an active crossfade, only update this.musicVolume and let the fade interpolate toward it; don't directly set the in-fade elements.
  • Ambient: add this.nextAmbientElement mirroring nextMusicElement; stop any pending next-ambient on entry and in stopAmbient().

Verification checklist (all manual)

  • Manually verify no audible music overlap on a chat/game switch that disposes then plays new music (cached track)
  • Verify music hard-cuts (no lingering 2s tail) on leaving the game view
  • Manually verify music/ambient is audible on a mobile/autoplay-blocked auto-start (fallback path)
  • Verify dragging the volume slider mid-crossfade produces no pop
  • Verify rapid ambient changes do not double-play
  • Run pnpm check

Affected files

  • packages/client/src/lib/game-audio.ts
  • packages/client/src/components/game/GameSurface.tsx

Audit provenance

Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged staging tree. Severity: HIGH.

AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.

Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2750 Originally assigned to: @SpicyMarinara on GitHub. ## Summary `game-audio.ts` has several audio-lifecycle defects centered on the music/ambient crossfade machinery. The flagship is that `stopMusic()`'s fade-out interval is never stored in `this.fadeInterval`, so it cannot be cancelled — and `dispose()` (called on scene-scope change and unmount) starts that uncancellable 2s fade while the next scene's `playMusic()` crossfades a new track in on top, producing audible overlap (the same class as prior bug #2647). ## Sub-bug 1: Untracked fade-out -> overlap (#10/#46) [primary] - **How it breaks:** stopMusic (742-772) clears `this.fadeInterval` (the crossfade-in), nulls `this.musicElement`, then starts a local `const interval = setInterval(...)` (764-771) that is NEVER assigned to `this.fadeInterval`. playMusic's guard `if (this.fadeInterval) clearInterval` (662) finds nothing to cancel, so the old fade-out and the new crossfade-in run concurrently for ~2s. dispose() therefore never hard-stops music. - **Repro:** In game mode with music + a user gesture, trigger a scene-scope change (chat/game switch) that disposes then plays new music with a cached/local track (decode <2s). Two tracks overlap for ~2s. On unmount the interval keeps ticking on a detached element until self-clear. - **Trace:** GameSurface.tsx:2638 dispose() -> game-audio.ts:897 stopMusic() -> 764 untracked `interval` -> next scene playMusic() 662 `this.fadeInterval` is null -> 690 new crossfade-in runs alongside the 764 fade-out. ## Sub-bug 2: Fallback <audio> silenced by suspended context (#11) - **How it breaks:** When Web Audio decode fails or the context won't reach 'running', startFallbackAudio (327-333) creates a plain `<audio>`, then applyVolume -> setElementLayerVolume -> getMediaGain calls `ctx.createMediaElementSource(fallbackAudio)` (283), re-attaching the element to the same suspended context. Once captured, the element's only output is the (silent) Web Audio graph. - **Repro:** On mobile/iOS/autoplay-blocked auto-start of music or ambient where the context can't resume, the 'fallback' plays inaudibly with no error. - **Trace:** createLoopingAudioLayer ready -> 347 resumeAudioContext fails -> throw -> catch 361 -> startFallbackAudio -> applyVolume -> getMediaGain -> createMediaElementSource on suspended ctx -> audio.play() resolves silent. (Contrast: playOneShot fallback uses setMediaElementVolume which sets `audio.volume` directly and never captures the element.) ## Sub-bug 3: Volume slider during crossfade pops (#47) - **How it breaks:** setVolumes (868-881) unconditionally calls `this.nextMusicElement?.setVolume(this.musicVolume)` and `this.musicElement?.setVolume(...)`. During a crossfade `nextMusicElement` is the fading-in track and `musicElement` the fading-out track; both are slammed to full via `gain.setValueAtTime` (immediate), then the next ≤50ms fade tick (690-706) yanks them back to the partial value. Cosmetic volume pop/stutter, self-heals in one tick. - **Repro:** Drag the music/master slider during the ~2s scene-music crossfade. ## Sub-bug 4: Ambient overlap on rapid changes (#49) - **How it breaks:** Unlike music, ambient has only `this.ambientElement` (no `nextAmbientElement`); the pending next-ambient is a local var. Two playAmbient calls within one fetch/decode window both start their BufferSource on decode; the stale one is stopped only a tick later by its ready guard, producing a brief double-ambient blip with no synchronous cancel handle. ## Suggested Fix - stopMusic: store its fade interval in `this.fadeInterval` (and stop the captured `audio` on a second stop/dispose). Give dispose() an `immediate` hard-cut path that calls `audio.stop()` right away instead of fading into the next scene. - Fallback path: when the context is not 'running', set `fallbackAudio.volume` directly and skip `getMediaGain`/`createMediaElementSource` (or gate getMediaGain on `ctx.state === 'running'`). - setVolumes: during an active crossfade, only update `this.musicVolume` and let the fade interpolate toward it; don't directly set the in-fade elements. - Ambient: add `this.nextAmbientElement` mirroring `nextMusicElement`; stop any pending next-ambient on entry and in stopAmbient(). ## Verification checklist (all manual) - [ ] Manually verify no audible music overlap on a chat/game switch that disposes then plays new music (cached track) - [ ] Verify music hard-cuts (no lingering 2s tail) on leaving the game view - [ ] Manually verify music/ambient is audible on a mobile/autoplay-blocked auto-start (fallback path) - [ ] Verify dragging the volume slider mid-crossfade produces no pop - [ ] Verify rapid ambient changes do not double-play - [ ] Run `pnpm check` --- ### Affected files - `packages/client/src/lib/game-audio.ts` - `packages/client/src/components/game/GameSurface.tsx` ### Audit provenance Found by a multi-agent static audit of the TTS/voice + image-generation subsystems (Round 1 + round-2 gap sweep + completeness critic, each finding adversarially verified) against the merged `staging` tree. Severity: **HIGH**. > AI-drafted from a static read; validation/test-plan boxes intentionally left UNCHECKED for a human to verify before close.
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Addressed on PR #2747. game-audio now tracks and cancels fade-out music via fadingMusicElement/fadeInterval, dispose hard-cuts music, fallback media elements no longer get routed through a suspended Web Audio context, volume changes no longer slam both music layers during active crossfade, and ambient playback has a tracked pending layer that stop/mute/volume/dispose can cancel. Deferred full pnpm validation until the end of the issue sweep per maintainer request.

<!-- gh-comment-id:4763266234 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on PR #2747. game-audio now tracks and cancels fade-out music via fadingMusicElement/fadeInterval, dispose hard-cuts music, fallback media elements no longer get routed through a suspended Web Audio context, volume changes no longer slam both music layers during active crossfade, and ambient playback has a tracked pending layer that stop/mute/volume/dispose can cancel. Deferred full pnpm validation until the end of the issue sweep per maintainer request.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1391
No description provided.