From cca970c34830c78088c4af72672bffb6d59731db Mon Sep 17 00:00:00 2001 From: 3dtours Date: Sun, 26 Jul 2026 20:09:22 +0700 Subject: [PATCH] fix: selectInstrument auto-inits SpessaSynth if not ready MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: selectInstrument called before getAudioContext triggers init. _initialized=false → selectInstrument returns immediately → no sound bank. Fix: selectInstrument creates AudioContext + calls init() if not ready, waits for completion before proceeding with bank/program routing. --- app/static/js/services/soundfontPlayer.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/static/js/services/soundfontPlayer.js b/app/static/js/services/soundfontPlayer.js index 3404253..8faea4a 100644 --- a/app/static/js/services/soundfontPlayer.js +++ b/app/static/js/services/soundfontPlayer.js @@ -188,9 +188,22 @@ // ════════════════════════════════════════════════ selectInstrument: async function (channel, bank, program, sfId) { console.log("[SonicSF][DEBUG] PHASE3: selectInstrument ch=" + channel + " bank=" + bank + " prog=" + program + " sf=" + sfId + " _initialized=" + _initialized); + // Auto-init: create AudioContext + init SpessaSynth if not ready if (!_initialized || !_synthInstance) { - console.log("[SonicSF][DEBUG] PHASE3: engine not ready, returning"); - return; + console.log("[SonicSF][DEBUG] PHASE3: engine not ready, triggering init..."); + try { + const ctx = new (window.AudioContext || window.webkitAudioContext)(); + if (ctx.state === 'suspended') await ctx.resume(); + await this.init(ctx); + } catch (e) { + console.error("[SonicSF][DEBUG] PHASE3: auto-init failed:", e); + return; + } + if (!_initialized || !_synthInstance) { + console.warn("[SonicSF][DEBUG] PHASE3: engine still not ready after auto-init"); + return; + } + console.log("[SonicSF][DEBUG] PHASE3: auto-init complete"); } // Phase 2 first: ensure SoundFont is loaded if (sfId) {