fix: selectInstrument auto-inits SpessaSynth if not ready

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.
This commit is contained in:
2026-07-26 20:09:22 +07:00
parent 5eb0c26ce4
commit cca970c348
+15 -2
View File
@@ -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) {