fix: SpessaSynth CDN URL + AudioWorklet init flow

- Fix CDN URL: spessasynth_lib@4.3.1/dist/index.js (was @latest with wrong path)
- Use WorkletSynthesizer instead of Synthesizer (correct class name)
- Add audioWorklet.addModule() for processor CDN URL
- Await synth.isReady before use
- Use soundBankManager.addSoundBank() (correct API)
- Fallback to WorkerSynthesizer if AudioWorklet fails
This commit is contained in:
2026-07-26 18:05:44 +07:00
parent 23601007bf
commit 0271be4484
2 changed files with 27 additions and 11 deletions
+24 -9
View File
@@ -42,26 +42,41 @@
// ── SpessaSynth init ──
init: async function (audioContext) {
if (_initialized && _synthInstance) return;
if (!window.SpessaSynthClass) {
console.warn("[SonicSF] SpessaSynth not loaded yet. Retrying in 1s...");
// Retry once after module script loads
if (!window.SpessaSynthClass || !window.__SpessaSynthCDN) {
console.warn("[SonicSF] SpessaSynth not loaded yet. Retrying in 2s...");
setTimeout(() => {
if (!_initialized && window.SpessaSynthClass && audioContext) {
this.init(audioContext);
}
}, 1000);
}, 2000);
return;
}
try {
_synthInstance = new window.SpessaSynthClass(audioContext.destination);
// 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;
console.log("[SonicSF] SpessaSynth initialized.");
console.log("[SonicSF] SpessaSynth initialized via AudioWorklet.");
// Auto-load default SF3
if (_currentSfId === null) {
this.loadSoundFont("sgm_v2.01");
}
} catch (e) {
console.error("[SonicSF] SpessaSynth init failed:", e);
console.warn("[SonicSF] SpessaSynth AudioWorklet init failed:", e, "- trying WorkerSynthesizer...");
try {
const { WorkerSynthesizer } = await import(window.__SpessaSynthCDN + "index.js");
_synthInstance = new WorkerSynthesizer(audioContext);
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);
}
}
},
@@ -89,11 +104,11 @@
}
}
try {
await _synthInstance.soundFontManager.addSoundFont(buffer);
await _synthInstance.soundBankManager.addSoundBank(buffer, sfId);
_currentSfId = sfId;
console.log("[SonicSF] SoundFont loaded:", sfId);
} catch (e) {
console.error("[SonicSF] Error parsing SF3:", e);
console.error("[SonicSF] Error parsing SF in SpessaSynth:", e);
}
},
+3 -2
View File
@@ -11,8 +11,9 @@
<script src="https://unpkg.com/react@18.3.1/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.production.min.js"></script>
<script type="module">
import { Synthesizer } from 'https://cdn.jsdelivr.net/npm/spessasynth_lib@latest/dist/spessasynth_lib.js';
window.SpessaSynthClass = Synthesizer;
import { WorkletSynthesizer } from 'https://cdn.jsdelivr.net/npm/spessasynth_lib@4.3.1/dist/index.js';
window.SpessaSynthClass = WorkletSynthesizer;
window.__SpessaSynthCDN = 'https://cdn.jsdelivr.net/npm/spessasynth_lib@4.3.1/dist/';
console.log('[SonicSF] SpessaSynth library loaded.');
</script>
<script src="/static/js/services/api.js?v=202607232105"></script>