Files
SonicForgeStudio/app/static/js/services/soundfontPlayer.js
T
3dtours ef1238b87d feat: migrate SpessaSynth → FluidSynth WASM (SF3 native, loop Gen 54 fix)
Replace SpessaSynth JS/AudioWorklet with FluidSynth C++ WASM
(@enikey87/fluidsynth-emscripten@0.1.1). CDN for dev, self-host
for prod. Fixes stuck Tremolo/Saxophone notes via compliant
Gen 44 loop mode processing. 100% audio parity with server
pyfluidsynth render (same C++ core).

- soundfontPlayer.js: FluidSynth WASM engine, preserve SonicSF API
- fluidsynthLoader.js: auto-select CDN (dev) vs self-host (prod)
- fluidsynth-bridge.js: PCM bridge AudioWorklet processor
- index.html: remove SpessaSynth importmap + module, add loader
- vendor/: libfluidsynth-2.3.0-sf3.js + .wasm (1.8MB self-host)
2026-07-27 12:48:30 +07:00

453 lines
20 KiB
JavaScript

(function () {
const RENDER_BLOCK = 512;
const QUEUE_TARGET = 4;
let _audioCtx = null;
let _fluidModule = null;
let _synthPtr = null;
let _settingsPtr = null;
let _workletNode = null;
let _initialized = false;
let _initPromise = null;
let _currentSfId = null;
let _sfHandleMap = new Map();
let _channels = Array.from({ length: 16 }, () => ({ bank: 0, program: 0, isPercussion: false }));
let _nextMelodicChannel = 0;
let _sustainStates = new Array(16).fill(false);
let _leftBufPtr = null;
let _rightBufPtr = null;
let _renderTimer = null;
let _pendingNoteTimers = [];
let _loadedFonts = {};
let _activeOscillators = {};
let _gainNode = null;
let _scheduledNotes = [];
const getCtx = function () {
if (typeof getAudioContext === 'function') {
var ctx = getAudioContext();
if (!_gainNode) {
_gainNode = ctx.createGain();
_gainNode.gain.value = 0.3;
_gainNode.connect(ctx.destination);
}
return ctx;
}
if (!window.__sharedAudioCtx) {
window.__sharedAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
if (window.__sharedAudioCtx.state === 'suspended') {
window.__sharedAudioCtx.resume();
}
if (!_gainNode) {
_gainNode = window.__sharedAudioCtx.createGain();
_gainNode.gain.value = 0.3;
_gainNode.connect(window.__sharedAudioCtx.destination);
}
return window.__sharedAudioCtx;
};
const SonicSF = {
loadedFonts: _loadedFonts,
init: async function (audioContext) {
if (_initialized && _fluidModule) return;
if (_initPromise) return _initPromise;
_initPromise = (async () => {
if (!window.__FluidSynthModuleFactory) {
console.log("[SonicSF] Waiting for FluidSynth WASM module to load...");
await window.__FluidSynthReady;
if (!window.__FluidSynthModuleFactory) {
console.warn("[SonicSF] FluidSynth WASM still not available after waiting.");
_initPromise = null;
return;
}
}
try {
_audioCtx = audioContext;
if (_audioCtx.state === 'suspended') await _audioCtx.resume();
try {
await _audioCtx.audioWorklet.addModule('/static/js/worklets/fluidsynth-bridge.js');
} catch (e) {
console.warn("[SonicSF] Worklet reg failed:", e);
}
console.log("[SonicSF] Initializing FluidSynth WASM Engine...");
_fluidModule = await window.__FluidSynthModuleFactory();
if (!_fluidModule || !_fluidModule._new_fluid_settings) {
throw new Error("FluidSynth WASM module loaded but API missing");
}
_settingsPtr = _fluidModule._new_fluid_settings();
_fluidModule._fluid_settings_setnum(_settingsPtr, "synth.sample-rate", _audioCtx.sampleRate);
_fluidModule._fluid_settings_setnum(_settingsPtr, "synth.gain", 0.5);
_fluidModule._fluid_settings_setnum(_settingsPtr, "synth.polyphony", 256);
_synthPtr = _fluidModule._new_fluid_synth(_settingsPtr);
if (!_synthPtr) throw new Error("Failed to create FluidSynth synthesizer");
try { _fluidModule.FS.mkdir('/soundfonts'); } catch (e) {}
_workletNode = new AudioWorkletNode(_audioCtx, 'fluidsynth-bridge');
_leftBufPtr = _fluidModule._malloc(RENDER_BLOCK * 4);
_rightBufPtr = _fluidModule._malloc(RENDER_BLOCK * 4);
_workletNode.connect(_audioCtx.destination);
_startRenderLoop();
_initialized = true;
console.log("[SonicSF] FluidSynth WASM Engine initialized.");
} catch (e) {
console.error("[SonicSF] FluidSynth init failed:", e);
_initPromise = null;
_cleanupFluid();
}
})();
return _initPromise;
},
loadSoundFont: async function (sfId) {
if (!_initialized || !_fluidModule) return false;
if (_currentSfId === sfId) return true;
if (_sfHandleMap.has(sfId)) {
_currentSfId = sfId;
return true;
}
try {
var cache = window.SonicSFStorage;
var buf = cache ? await cache.getBuffer(sfId) : null;
if (!buf) {
var url = "/api/v1/plugins/soundfonts/download/" + encodeURIComponent(sfId) + "?t=" + Date.now();
var resp = await fetch(url);
if (!resp.ok) {
console.warn("[SonicSF] SoundFont not found:", sfId);
return false;
}
buf = await resp.arrayBuffer();
if (cache) await cache.saveBuffer(sfId, buf);
}
var virtualPath = '/soundfonts/' + sfId + '.sf3';
_fluidModule.FS.writeFile(virtualPath, new Uint8Array(buf));
var sfHandle = _fluidModule._fluid_synth_sfload(_synthPtr, virtualPath, 1);
if (sfHandle === -1) {
console.error("[SonicSF] FluidSynth failed to parse:", sfId);
return false;
}
_sfHandleMap.set(sfId, sfHandle);
_currentSfId = sfId;
_loadedFonts[sfId] = true;
console.log("[SonicSF] SoundFont loaded:", sfId, "handle:", sfHandle);
return true;
} catch (e) {
console.warn("[SonicSF] loadSoundFont error:", e);
return false;
}
},
selectInstrument: async function (channel, bank, program, sfId) {
if (!_initialized || !_fluidModule) {
var ctx = new (window.AudioContext || window.webkitAudioContext)();
await this.init(ctx);
}
if (sfId) {
var ok = await this.loadSoundFont(sfId);
if (!ok) return;
}
try {
_fluidModule._fluid_synth_bank_select(_synthPtr, channel, bank);
} catch (e) {}
try {
_fluidModule._fluid_synth_program_change(_synthPtr, channel, program);
} catch (e) {}
_channels[channel].bank = bank;
_channels[channel].program = program;
_channels[channel].isPercussion = (bank === 128);
},
controllerChange: function (channel, controller, value) {
if (channel < 0 || channel > 15) return;
if (_initialized && _fluidModule) {
try {
_fluidModule._fluid_synth_cc(_synthPtr, channel, controller, value);
} catch (e) {}
}
if (controller === 0) {
_channels[channel].bank = value;
_channels[channel].isPercussion = (value === 128);
}
if (controller === 64) {
_sustainStates[channel] = value >= 64;
}
},
programChange: function (channel, program) {
if (channel < 0 || channel > 15) return;
if (_initialized && _fluidModule) {
try {
_fluidModule._fluid_synth_program_change(_synthPtr, channel, program);
} catch (e) {}
}
_channels[channel].program = program;
},
allocateChannel: function (bank) {
if (bank === 128) return 9;
var ch = _nextMelodicChannel % 9;
_nextMelodicChannel = (_nextMelodicChannel + 1) % 9;
return ch;
},
applyAITrackInstrument: function (bank, program, synthEngine) {
if (synthEngine) {
bank = bank !== undefined ? bank : (synthEngine.soundfont_bank || 0);
program = program !== undefined ? program : (synthEngine.soundfont_program || 0);
}
var channel = this.allocateChannel(bank);
this.selectInstrument(channel, bank, program, synthEngine && synthEngine.soundfont_id);
return channel;
},
getChannelState: function (channel) {
if (channel < 0 || channel > 15) return null;
return { bank: _channels[channel].bank, program: _channels[channel].program, isPercussion: _channels[channel].isPercussion };
},
sustainActive: function (channel) {
if (channel < 0 || channel > 15) return false;
return _sustainStates[channel];
},
pitchBend: function (channel, value) {
if (channel < 0 || channel > 15) return;
if (_initialized && _fluidModule) {
try {
_fluidModule._fluid_synth_pitch_bend(_synthPtr, channel, value);
} catch (e) {}
}
},
stopNote: function (channel, pitch) {
if (channel < 0 || channel > 15) return;
if (_initialized && _fluidModule) {
try {
_fluidModule._fluid_synth_noteoff(_synthPtr, channel, pitch);
} catch (e) {}
}
},
playNote: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
if (!_initialized || !_fluidModule) {
this._lazyInit();
this._playNoteFallback(note, velocity, 2000, startTime, program, destinationNode, channel, synthEngine);
return;
}
this._playNoteFluid(note, velocity, durationMs, startTime, program, channel, synthEngine);
},
_lazyInit: async function () {
if (_initialized && _fluidModule) return;
if (_initPromise) return;
try {
var ctx = new (window.AudioContext || window.webkitAudioContext)();
if (ctx.state === 'suspended') await ctx.resume();
await this.init(ctx);
} catch (e) {}
},
_playNoteFluid: function (note, velocity, durationMs, startTime, program, channel, synthEngine) {
var midiPitch = Math.min(127, Math.max(0, parseInt(note) || 60));
var midiVel = Math.min(127, Math.max(1, Math.floor(
typeof velocity === 'number' ? (velocity > 1 ? velocity : velocity * 127) : 100
)));
var ch = channel;
if (synthEngine) {
if (ch === undefined) {
ch = synthEngine.soundfont_bank === 128 ? 9 : 0;
}
try {
_fluidModule._fluid_synth_bank_select(_synthPtr, ch, synthEngine.soundfont_bank || 0);
} catch (e) {}
try {
_fluidModule._fluid_synth_program_change(_synthPtr, ch, synthEngine.soundfont_program || 0);
} catch (e) {}
if (channel === undefined) channel = ch;
}
if (ch === undefined) ch = 0;
var ctx = getCtx();
var now = ctx.currentTime;
var delay = (typeof startTime === 'number' && startTime > now) ? (startTime - now) : 0;
var durSec = (durationMs || 500) / 1000;
var scheduledNote = { on: null, off: null };
var self = this;
var doNote = function () {
try {
_fluidModule._fluid_synth_noteon(_synthPtr, ch, midiPitch, midiVel);
if (delay > 0) {
scheduledNote.off = setTimeout(function () {
try {
_fluidModule._fluid_synth_noteoff(_synthPtr, ch, midiPitch);
} catch (e) {}
}, durSec * 1000);
}
} catch (e) {
console.warn("[SonicSF] FluidSynth noteOn error:", e);
self._playNoteFallback(note, velocity, durationMs, startTime, program, null, channel, synthEngine);
}
};
if (delay > 0) {
scheduledNote.on = setTimeout(doNote, delay * 1000);
_scheduledNotes.push(scheduledNote);
} else {
doNote();
}
},
_playNoteFallback: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {
var ctx = getCtx();
var freq = 440 * Math.pow(2, (note - 69) / 12);
if (freq <= 0 || isNaN(freq)) return null;
if (synthEngine) {
var 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);
if (channel === undefined) channel = ch;
if (program === undefined) program = synthEngine.soundfont_program;
}
var osc = ctx.createOscillator();
var noteGain = ctx.createGain();
var oscType = 'triangle';
var attackTime = 0.03, decayTime = 0.1, sustainLevel = 0.5, releaseTime = 0.2, volFactor = 0.25;
var prog = program !== undefined ? parseInt(program) : 0;
if (channel !== undefined && channel >= 0 && channel < 16) {
prog = _channels[channel].program || prog;
}
if (prog >= 0 && prog <= 7) { oscType = 'sine'; decayTime = 0.3; sustainLevel = 0.1; releaseTime = 0.2; }
else if (prog >= 8 && prog <= 15) { oscType = 'sine'; decayTime = 0.1; sustainLevel = 0.0; releaseTime = 0.1; }
else if (prog >= 16 && prog <= 23) { oscType = 'sine'; attackTime = 0.05; sustainLevel = 0.8; releaseTime = 0.1; }
else if (prog >= 24 && prog <= 31) { oscType = 'triangle'; decayTime = 0.4; sustainLevel = 0.2; releaseTime = 0.3; }
else if (prog >= 32 && prog <= 39) { oscType = 'triangle'; attackTime = 0.02; decayTime = 0.2; sustainLevel = 0.6; releaseTime = 0.2; }
else if (prog >= 40 && prog <= 47) { oscType = 'sawtooth'; attackTime = 0.15; sustainLevel = 0.8; releaseTime = 0.5; volFactor = 0.15; }
else if (prog >= 48 && prog <= 55) { oscType = 'sawtooth'; attackTime = 0.2; sustainLevel = 0.8; releaseTime = 0.6; volFactor = 0.12; }
else if (prog >= 56 && prog <= 63) { oscType = 'sawtooth'; attackTime = 0.08; sustainLevel = 0.7; releaseTime = 0.3; volFactor = 0.15; }
else if (prog >= 64 && prog <= 71) { oscType = 'square'; attackTime = 0.05; sustainLevel = 0.6; releaseTime = 0.2; volFactor = 0.15; }
else if (prog >= 72 && prog <= 79) { oscType = 'sine'; attackTime = 0.1; sustainLevel = 0.7; releaseTime = 0.3; volFactor = 0.2; }
else if (prog >= 80 && prog <= 119) { oscType = 'sawtooth'; attackTime = 0.05; sustainLevel = 0.6; releaseTime = 0.4; volFactor = 0.15; }
osc.type = oscType;
osc.frequency.setValueAtTime(freq, 0);
var startAt = startTime !== undefined ? startTime : ctx.currentTime;
var durSec = durationMs / 1000;
var vel = typeof velocity === 'number' ? (velocity > 1 ? velocity / 127 : velocity) : 0.8;
var targetGain = vel * volFactor;
noteGain.gain.setValueAtTime(0, startAt);
noteGain.gain.linearRampToValueAtTime(targetGain, startAt + attackTime);
noteGain.gain.linearRampToValueAtTime(targetGain * sustainLevel, startAt + attackTime + decayTime);
var releaseStart = startAt + Math.max(attackTime + decayTime, durSec);
noteGain.gain.linearRampToValueAtTime(targetGain * sustainLevel, releaseStart);
noteGain.gain.linearRampToValueAtTime(0, releaseStart + releaseTime);
osc.connect(noteGain);
noteGain.connect(destinationNode || _gainNode || ctx.destination);
osc.start(startAt);
var stopAt = releaseStart + releaseTime + 0.02;
osc.stop(stopAt);
var oscId = note + '_' + Date.now() + '_' + Math.random();
_activeOscillators[oscId] = { osc: osc, gain: noteGain };
setTimeout(function () { delete _activeOscillators[oscId]; }, (stopAt - ctx.currentTime) * 1000 + 100);
return osc;
},
stopAll: function () {
if (_initialized && _fluidModule) {
for (var ch = 0; ch < 16; ch++) {
try { _fluidModule._fluid_synth_all_notes_off(_synthPtr, ch); } catch (e) {}
try { _fluidModule._fluid_synth_all_sounds_off(_synthPtr, ch); } catch (e) {}
}
}
while (_scheduledNotes.length > 0) {
var n = _scheduledNotes.pop();
if (n.on) { clearTimeout(n.on); n.on = null; }
if (n.off) { clearTimeout(n.off); n.off = null; }
}
var ctx = getCtx();
var now = ctx.currentTime;
Object.values(_activeOscillators).forEach(function (entry) {
try {
if (entry.gain) { entry.gain.gain.cancelScheduledValues(now); entry.gain.gain.setValueAtTime(0, now); }
if (entry.osc) try { entry.osc.stop(now); } catch (e) {}
} catch (e) {}
});
Object.keys(_activeOscillators).forEach(function (k) { delete _activeOscillators[k]; });
},
saveToIndexedDB: async function (name, arrayBuffer) {
if (window.SonicStorage && window.SonicStorage.saveToIndexedDB) {
await window.SonicStorage.saveToIndexedDB('soundfont_' + name, arrayBuffer);
}
},
loadFromIndexedDB: async function (name) {
if (window.SonicStorage && window.SonicStorage.loadFromIndexedDB) {
return await window.SonicStorage.loadFromIndexedDB('soundfont_' + name);
}
return null;
}
};
function _startRenderLoop() {
if (_renderTimer) return;
var Module = _fluidModule;
var synth = _synthPtr;
var node = _workletNode;
var leftPtr = _leftBufPtr;
var rightPtr = _rightBufPtr;
var block = RENDER_BLOCK;
var queueDepth = 0;
var maxQueue = QUEUE_TARGET;
function pushFrame() {
if (!Module || !synth || !node) return;
try {
Module._fluid_synth_write_float(synth, block, leftPtr, 0, 1, rightPtr, 0, 1);
var leftArr = new Float32Array(Module.HEAPF32.subarray(leftPtr >> 2, (leftPtr >> 2) + block));
var rightArr = new Float32Array(Module.HEAPF32.subarray(rightPtr >> 2, (rightPtr >> 2) + block));
node.port.postMessage({ type: 'PCM', L: leftArr, R: rightArr }, [leftArr.buffer, rightArr.buffer]);
queueDepth++;
} catch (e) {}
}
function fillLoop() {
if (!Module || !synth || !node || !_initialized) {
_renderTimer = null;
return;
}
var needed = maxQueue - queueDepth;
for (var i = 0; i < needed; i++) {
pushFrame();
}
queueDepth = Math.max(0, queueDepth - 1);
}
_renderTimer = setInterval(fillLoop, Math.max(8, (block / _audioCtx.sampleRate) * 1000 * 0.75));
}
function _stopRenderLoop() {
if (_renderTimer) {
clearInterval(_renderTimer);
_renderTimer = null;
}
}
function _cleanupFluid() {
_stopRenderLoop();
if (_leftBufPtr && _fluidModule) { try { _fluidModule._free(_leftBufPtr); } catch (e) {} _leftBufPtr = null; }
if (_rightBufPtr && _fluidModule) { try { _fluidModule._free(_rightBufPtr); } catch (e) {} _rightBufPtr = null; }
if (_synthPtr && _fluidModule) { try { _fluidModule._delete_fluid_synth(_synthPtr); } catch (e) {} _synthPtr = null; }
if (_settingsPtr && _fluidModule) { try { _fluidModule._delete_fluid_settings(_settingsPtr); } catch (e) {} _settingsPtr = null; }
_fluidModule = null;
_initialized = false;
}
window.SonicSF = SonicSF;
})();