diff --git a/app/static/js/services/soundfontPlayer.js b/app/static/js/services/soundfontPlayer.js index c3c60a6..07d181e 100644 --- a/app/static/js/services/soundfontPlayer.js +++ b/app/static/js/services/soundfontPlayer.js @@ -159,13 +159,18 @@ } if (channel === undefined) channel = 0; const durSec = durationMs / 1000; - try { - _synthInstance.noteOn(channel, midiPitch, midiVel); - setTimeout(() => { try { _synthInstance.noteOff(channel, midiPitch); } catch (e) {} }, durSec * 1000); - } catch (e) { - console.warn("[SonicSF] SpessaSynth noteOn error, oscillator fallback:", e); - this._playNoteOsc(note, velocity, durationMs, startTime, program, null, channel, synthEngine); - } + const now = ctx.currentTime; + const delay = (typeof startTime === 'number' && startTime > now) ? (startTime - now) : 0; + const doNote = () => { + try { + _synthInstance.noteOn(channel, midiPitch, midiVel); + setTimeout(() => { try { _synthInstance.noteOff(channel, midiPitch); } catch (e) {} }, durSec * 1000); + } catch (e) { + console.warn("[SonicSF] SpessaSynth noteOn error:", e); + this._playNoteOsc(note, velocity, durationMs, startTime, program, null, channel, synthEngine); + } + }; + if (delay > 0) setTimeout(doNote, delay * 1000); else doNote(); }, _playNoteOsc: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) {