fix: SpessaSynth init race condition, remove auto-load of sgm_v2.01

- Add _initInProgress flag to prevent concurrent init calls
- Remove auto-loadSoundFont from init (confuses user when loading DSK)
- SF3 loading happens only when user selects an instrument
This commit is contained in:
2026-07-26 18:33:04 +07:00
parent 72186168d2
commit e7a0a85eca
+6 -8
View File
@@ -34,6 +34,7 @@
// ── SpessaSynth integration state ──
let _synthInstance = null;
let _initialized = false;
let _initInProgress = false;
let _currentSfId = null;
const SonicSF = {
@@ -42,8 +43,11 @@
// ── SpessaSynth init ──
init: async function (audioContext) {
if (_initialized && _synthInstance) return;
if (_initInProgress) return;
_initInProgress = true;
if (!window.SpessaSynthClass || !window.__SpessaSynthCDN) {
console.warn("[SonicSF] SpessaSynth not loaded yet. Retrying in 2s...");
_initInProgress = false;
setTimeout(() => {
if (!_initialized && window.SpessaSynthClass && audioContext) {
this.init(audioContext);
@@ -52,20 +56,15 @@
return;
}
try {
// Register the AudioWorklet processor from CDN
const procUrl = window.__SpessaSynthCDN + "spessasynth_processor.min.js";
await audioContext.audioWorklet.addModule(procUrl);
// Create WorkletSynthesizer
_synthInstance = new window.SpessaSynthClass(audioContext);
// Wait for synth to be ready (loads default soundbank)
await _synthInstance.isReady;
_initialized = true;
_initInProgress = false;
console.log("[SonicSF] SpessaSynth initialized via AudioWorklet.");
// Auto-load default SF3
if (_currentSfId === null) {
this.loadSoundFont("sgm_v2.01");
}
} catch (e) {
_initInProgress = false;
console.warn("[SonicSF] SpessaSynth AudioWorklet init failed:", e, "- trying WorkerSynthesizer...");
try {
const mod = await import("https://esm.sh/spessasynth_lib@4.3.1");
@@ -74,7 +73,6 @@
await _synthInstance.isReady;
_initialized = true;
console.log("[SonicSF] SpessaSynth initialized via Worker.");
if (_currentSfId === null) this.loadSoundFont("sgm_v2.01");
} catch (e2) {
console.error("[SonicSF] SpessaSynth init failed completely:", e2);
}