diff --git a/app/static/js/services/fluidsynthLoader.js b/app/static/js/services/fluidsynthLoader.js index ce9f229..d83cce9 100644 --- a/app/static/js/services/fluidsynthLoader.js +++ b/app/static/js/services/fluidsynthLoader.js @@ -1,23 +1,37 @@ -window.__FluidSynthReady = new Promise(function (resolve) { - var FLUIDSYNTH_SRC; +(function () { + if (window.__FluidSynthReady) return; + + var FLUIDSYNTH_JS_URL; + var FLUIDSYNTH_WASM_URL; if (window.__FLUIDSYNTH_CDN) { - FLUIDSYNTH_SRC = window.__FLUIDSYNTH_CDN; + FLUIDSYNTH_JS_URL = window.__FLUIDSYNTH_CDN; } else if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') { - FLUIDSYNTH_SRC = 'https://cdn.jsdelivr.net/npm/@enikey87/fluidsynth-emscripten@0.1.1/dist/libfluidsynth-2.3.0-sf3.js'; + FLUIDSYNTH_JS_URL = 'https://cdn.jsdelivr.net/npm/@enikey87/fluidsynth-emscripten@0.1.1/dist/libfluidsynth-2.3.0-sf3.js'; } else { - FLUIDSYNTH_SRC = '/static/js/vendor/libfluidsynth-2.3.0-sf3.js'; + FLUIDSYNTH_JS_URL = '/static/js/vendor/libfluidsynth-2.3.0-sf3.js'; } - var script = document.createElement('script'); - script.type = 'module'; - script.textContent = [ - 'import FluidsynthModule from "' + FLUIDSYNTH_SRC + '";', - 'window.__FluidSynthModuleFactory = FluidsynthModule;', - 'window.__FluidSynthReadyResolver = function() { window.__FluidSynthReadyResolve(); };', - 'console.log("[FluidSynth] Loaded from:", "' + FLUIDSYNTH_SRC + '");' - ].join('\n'); - document.head.appendChild(script); + FLUIDSYNTH_WASM_URL = FLUIDSYNTH_JS_URL.replace(/\.js$/, '.wasm'); - window.__FluidSynthReadyResolve = resolve; -}); + window.__FluidSynthLocateWasm = function () { return FLUIDSYNTH_WASM_URL; }; + + window.__FluidSynthReady = new Promise(function (resolve) { + var script = document.createElement('script'); + script.type = 'module'; + script.textContent = [ + 'import FluidsynthModule from "' + FLUIDSYNTH_JS_URL + '";', + 'window.__FluidSynthModuleFactory = FluidsynthModule;', + 'console.log("[FluidSynth] Loaded:", "' + FLUIDSYNTH_JS_URL + '");' + ].join('\n'); + document.head.appendChild(script); + + var check = setInterval(function () { + if (window.__FluidSynthModuleFactory) { + clearInterval(check); + resolve(); + } + }, 50); + setTimeout(function () { clearInterval(check); resolve(); }, 15000); + }); +})(); diff --git a/app/static/js/services/soundfontPlayer.js b/app/static/js/services/soundfontPlayer.js index d750ca2..f040c13 100644 --- a/app/static/js/services/soundfontPlayer.js +++ b/app/static/js/services/soundfontPlayer.js @@ -73,7 +73,14 @@ } console.log("[SonicSF] Initializing FluidSynth WASM Engine..."); - _fluidModule = await window.__FluidSynthModuleFactory(); + _fluidModule = await window.__FluidSynthModuleFactory({ + locateFile: function (path) { + if (path.endsWith('.wasm')) { + return window.__FluidSynthLocateWasm ? window.__FluidSynthLocateWasm() : path; + } + return path; + } + }); if (!_fluidModule || !_fluidModule._new_fluid_settings) { throw new Error("FluidSynth WASM module loaded but API missing");