fix: explicit selectInstrument for SpessaSynth bank+program routing
Per md/37_FIXVST3_SUGGEST.md: - Add SonicSF.selectInstrument(channel, bank, program, sfId) method - Always sends controllerChange(CC0, bank) + programChange(prog) to SpessaSynth - _playNoteSpessa also sends CC32 (bank LSB) before programChange - Remove default bank auto-load from init (cleaner startup) - Fix syntax error (double }} from previous edit) - playNote Spessa: use program arg + synthEngine bank/program properly
This commit is contained in:
@@ -31,7 +31,6 @@
|
||||
const _channels = Array.from({ length: 16 }, () => ({ bank: 0, program: 0, isPercussion: false }));
|
||||
let _nextMelodicChannel = 0;
|
||||
|
||||
// ── SpessaSynth integration state ──
|
||||
let _synthInstance = null;
|
||||
let _initialized = false;
|
||||
let _initInProgress = false;
|
||||
@@ -40,18 +39,15 @@
|
||||
const SonicSF = {
|
||||
loadedFonts: {},
|
||||
|
||||
// ── 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...");
|
||||
console.warn("[SonicSF] SpessaSynth CDN not loaded. Retrying in 2s...");
|
||||
_initInProgress = false;
|
||||
setTimeout(() => {
|
||||
if (!_initialized && window.SpessaSynthClass && audioContext) {
|
||||
this.init(audioContext);
|
||||
}
|
||||
if (!_initialized && window.SpessaSynthClass && audioContext) this.init(audioContext);
|
||||
}, 2000);
|
||||
return;
|
||||
}
|
||||
@@ -60,46 +56,16 @@
|
||||
await audioContext.audioWorklet.addModule(procUrl);
|
||||
_synthInstance = new window.SpessaSynthClass(audioContext);
|
||||
await _synthInstance.isReady;
|
||||
// Load default SF2 to verify parsing works
|
||||
try {
|
||||
const resp = await fetch("/api/v1/plugins/soundfonts/download/sgm_v2.01?t=" + Date.now());
|
||||
if (resp.ok) {
|
||||
const buf = await resp.arrayBuffer();
|
||||
await _synthInstance.soundBankManager.addSoundBank(buf.slice(0), "sgm_v2.01");
|
||||
await _synthInstance.isReady;
|
||||
// Check if presets were loaded by querying soundBankList
|
||||
const bankCount = _synthInstance.soundBankManager?.soundBankList?.length ?? 0;
|
||||
console.log("[SonicSF] SpessaSynth initialized. Banks loaded:", bankCount);
|
||||
if (bankCount > 0) {
|
||||
_initialized = true;
|
||||
_initInProgress = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (bankErr) {
|
||||
console.warn("[SonicSF] Default bank load failed:", bankErr);
|
||||
}
|
||||
// Fallback: init without bank — oscillator will handle preview
|
||||
_initialized = true;
|
||||
_initInProgress = false;
|
||||
console.log("[SonicSF] SpessaSynth initialized (no sound bank). Oscillator fallback active.");
|
||||
console.log("[SonicSF] SpessaSynth initialized.");
|
||||
} catch (e) {
|
||||
_initInProgress = false;
|
||||
console.warn("[SonicSF] AudioWorklet failed:", e);
|
||||
// Try WorkerSynthesizer as fallback
|
||||
console.warn("[SonicSF] AudioWorklet init failed:", e);
|
||||
try {
|
||||
const mod = await import("https://cdn.jsdelivr.net/npm/spessasynth_lib@4.3.1/dist/index.js");
|
||||
_synthInstance = new mod.WorkerSynthesizer(audioContext);
|
||||
await _synthInstance.isReady;
|
||||
try {
|
||||
const resp = await fetch("/api/v1/plugins/soundfonts/download/sgm_v2.01?t=" + Date.now());
|
||||
if (resp.ok) {
|
||||
const buf = await resp.arrayBuffer();
|
||||
await _synthInstance.soundBankManager.addSoundBank(buf.slice(0), "sgm_v2.01");
|
||||
await _synthInstance.isReady;
|
||||
console.log("[SonicSF] Worker synth with bank OK.");
|
||||
}
|
||||
} catch (e2) {}
|
||||
_initialized = true;
|
||||
console.log("[SonicSF] SpessaSynth initialized via Worker.");
|
||||
} catch (e2) {
|
||||
@@ -107,9 +73,18 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
selectInstrument: async function (channel, bank, program, sfId) {
|
||||
if (!_initialized || !_synthInstance) return;
|
||||
if (sfId) await this.loadSoundFont(sfId);
|
||||
try { _synthInstance.controllerChange(channel, 0, bank); } catch (e) {}
|
||||
try { _synthInstance.controllerChange(channel, 32, 0); } catch (e) {}
|
||||
try { _synthInstance.programChange(channel, program); } catch (e) {}
|
||||
this.controllerChange(channel, 0, bank);
|
||||
this.programChange(channel, program);
|
||||
console.log("[SonicSF] selectInstrument ch", channel, "bank", bank, "prog", program, "sf", sfId);
|
||||
},
|
||||
|
||||
// ── Load SF3 from IndexedDB cache or server ──
|
||||
loadSoundFont: async function (sfId) {
|
||||
if (!_initialized || !_synthInstance) return;
|
||||
if (_currentSfId === sfId) return;
|
||||
@@ -128,36 +103,28 @@
|
||||
await window.SonicSFStorage.saveBuffer(sfId, buffer);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("[SonicSF] Failed to load SoundFont:", sfId, e);
|
||||
return;
|
||||
console.error("[SonicSF] Failed to download:", sfId, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
try {
|
||||
// Clone buffer before sending to worklet (avoids transfer neutering)
|
||||
const bufCopy = buffer.slice(0);
|
||||
await _synthInstance.soundBankManager.addSoundBank(bufCopy, sfId);
|
||||
await _synthInstance.isReady;
|
||||
// Force bank+program select on all channels to activate loaded bank
|
||||
for (let ch = 0; ch < 16; ch++) {
|
||||
try { _synthInstance.controllerChange(ch, 0, 0); } catch (e) {}
|
||||
try { _synthInstance.controllerChange(ch, 32, 0); } catch (e) {}
|
||||
try { _synthInstance.programChange(ch, 0); } catch (e) {}
|
||||
}
|
||||
_currentSfId = sfId;
|
||||
console.log("[SonicSF] SoundFont loaded:", sfId);
|
||||
return true;
|
||||
} catch (e) {
|
||||
console.error("[SonicSF] Error parsing SF in SpessaSynth:", sfId, e);
|
||||
// Invalidate cache and retry once with fresh download
|
||||
console.error("[SonicSF] Error loading SF in SpessaSynth:", sfId, e);
|
||||
if (window.SonicSFStorage) {
|
||||
try {
|
||||
const db = await window.SonicSFStorage.openDB();
|
||||
const tx = db.transaction(window.SonicSFStorage.storeName, "readwrite");
|
||||
tx.objectStore(window.SonicSFStorage.storeName).delete(sfId);
|
||||
console.log("[SonicSF] Invalidated cache for:", sfId);
|
||||
} catch (ce) {}
|
||||
}
|
||||
// Clear current SF so next load will actually run
|
||||
_currentSfId = null;
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -193,11 +160,7 @@
|
||||
program = program !== undefined ? program : (synthEngine.soundfont_program || 0);
|
||||
}
|
||||
const channel = this.allocateChannel(bank);
|
||||
this.controllerChange(channel, 0, bank);
|
||||
this.programChange(channel, program);
|
||||
if (_initialized && synthEngine && synthEngine.soundfont_id) {
|
||||
this.loadSoundFont(synthEngine.soundfont_id);
|
||||
}
|
||||
this.selectInstrument(channel, bank, program, synthEngine?.soundfont_id);
|
||||
return channel;
|
||||
},
|
||||
|
||||
@@ -206,33 +169,23 @@
|
||||
return { ..._channels[channel] };
|
||||
},
|
||||
|
||||
loadSoundFontLegacy: async function (url) {
|
||||
if (this.loadedFonts[url]) return this.loadedFonts[url];
|
||||
const resp = await fetch(url);
|
||||
if (!resp.ok) throw new Error('Failed to load SoundFont: ' + url);
|
||||
const buffer = await resp.arrayBuffer();
|
||||
this.loadedFonts[url] = buffer;
|
||||
return buffer;
|
||||
},
|
||||
|
||||
playNote: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
|
||||
// SpessaSynth path
|
||||
if (_initialized && _synthInstance && _currentSfId) {
|
||||
return this._playNoteSpessa(note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine);
|
||||
return this._playNoteSpessa(note, velocity, durationMs, startTime, program, channel, synthEngine);
|
||||
}
|
||||
// Fallback oscillator path
|
||||
return this._playNoteOsc(note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine);
|
||||
},
|
||||
|
||||
_playNoteSpessa: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
|
||||
_playNoteSpessa: function (note, velocity, durationMs, startTime, program, channel, synthEngine) {
|
||||
const ctx = getCtx();
|
||||
const midiPitch = Math.min(127, Math.max(0, parseInt(note) || 60));
|
||||
const midiVel = Math.min(127, Math.max(1, Math.floor((typeof velocity === 'number' ? (velocity > 1 ? velocity : velocity * 127) : 100))));
|
||||
|
||||
if (synthEngine) {
|
||||
const ch = channel !== undefined ? channel : (synthEngine.soundfont_bank === 128 ? 9 : 0);
|
||||
this.controllerChange(ch, 0, synthEngine.soundfont_bank || 0);
|
||||
this.programChange(ch, synthEngine.soundfont_program || 0);
|
||||
try { _synthInstance.controllerChange(ch, 0, synthEngine.soundfont_bank || 0); } catch (e) {}
|
||||
try { _synthInstance.controllerChange(ch, 32, 0); } catch (e) {}
|
||||
try { _synthInstance.programChange(ch, synthEngine.soundfont_program || 0); } catch (e) {}
|
||||
if (channel === undefined) channel = ch;
|
||||
}
|
||||
if (channel === undefined) channel = 0;
|
||||
@@ -241,20 +194,13 @@
|
||||
const now = ctx.currentTime;
|
||||
const scheduledTime = (typeof startTime === 'number' && startTime > now) ? (startTime - now) : 0;
|
||||
|
||||
if (scheduledTime > 0) {
|
||||
setTimeout(() => {
|
||||
if (!_synthInstance) return;
|
||||
try {
|
||||
_synthInstance.noteOn(channel, midiPitch, midiVel);
|
||||
setTimeout(() => { try { _synthInstance.noteOff(channel, midiPitch); } catch (e) {} }, durSec * 1000);
|
||||
} catch (e) {}
|
||||
}, scheduledTime * 1000);
|
||||
} else {
|
||||
const doNoteOn = () => {
|
||||
try {
|
||||
_synthInstance.noteOn(channel, midiPitch, midiVel);
|
||||
setTimeout(() => { try { _synthInstance.noteOff(channel, midiPitch); } catch (e) {} }, durSec * 1000);
|
||||
} catch (e) {}
|
||||
}
|
||||
};
|
||||
if (scheduledTime > 0) setTimeout(doNoteOn, scheduledTime * 1000); else doNoteOn();
|
||||
},
|
||||
|
||||
_playNoteOsc: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
|
||||
@@ -389,9 +335,7 @@
|
||||
stopAll: function () {
|
||||
if (_initialized && _synthInstance) {
|
||||
try {
|
||||
for (let ch = 0; ch < 16; ch++) {
|
||||
_synthInstance.allNotesOff(ch);
|
||||
}
|
||||
for (let ch = 0; ch < 16; ch++) _synthInstance.allNotesOff(ch);
|
||||
} catch (e) {}
|
||||
}
|
||||
const ctx = getCtx();
|
||||
@@ -402,9 +346,7 @@
|
||||
entry.gain.gain.cancelScheduledValues(now);
|
||||
entry.gain.gain.setValueAtTime(0, now);
|
||||
}
|
||||
if (entry.osc) {
|
||||
try { entry.osc.stop(now); } catch (e) { }
|
||||
}
|
||||
if (entry.osc) try { entry.osc.stop(now); } catch (e) { }
|
||||
} catch (e) { }
|
||||
});
|
||||
Object.keys(activeOscillators).forEach(k => delete activeOscillators[k]);
|
||||
|
||||
Reference in New Issue
Block a user