fix: unload old SoundFont before loading new one

- Add _currentSfId tracking
- loadSoundFont calls deleteSoundBank(oldId) before addSoundBank(newId)
- Skip reload if same sfId requested
- Prevents bank accumulation in SpessaSynth
This commit is contained in:
2026-07-26 20:52:48 +07:00
parent 473a0afe0a
commit f96064cbab
@@ -34,6 +34,7 @@
let _synthInstance = null; let _synthInstance = null;
let _initialized = false; let _initialized = false;
let _initPromise = null; let _initPromise = null;
let _currentSfId = null;
const SonicSF = { const SonicSF = {
loadedFonts: {}, loadedFonts: {},
@@ -60,7 +61,12 @@
loadSoundFont: async function (sfId) { loadSoundFont: async function (sfId) {
if (!_initialized || !_synthInstance) return false; if (!_initialized || !_synthInstance) return false;
if (_currentSfId === sfId) return true;
try { try {
// Unload previous SoundFont
if (_currentSfId) {
try { _synthInstance.soundBankManager.deleteSoundBank(_currentSfId); } catch (e) {}
}
const cache = window.SonicSFStorage; const cache = window.SonicSFStorage;
let buf = cache ? await cache.getBuffer(sfId) : null; let buf = cache ? await cache.getBuffer(sfId) : null;
if (!buf) { if (!buf) {
@@ -72,6 +78,7 @@
} }
await _synthInstance.soundBankManager.addSoundBank(buf.slice(0), sfId); await _synthInstance.soundBankManager.addSoundBank(buf.slice(0), sfId);
await _synthInstance.isReady; await _synthInstance.isReady;
_currentSfId = sfId;
return true; return true;
} catch (e) { } catch (e) {
console.warn("[SonicSF] loadSoundFont failed:", e); console.warn("[SonicSF] loadSoundFont failed:", e);