diff --git a/app/static/js/services/soundfontPlayer.js b/app/static/js/services/soundfontPlayer.js index 2d834f4..6494982 100644 --- a/app/static/js/services/soundfontPlayer.js +++ b/app/static/js/services/soundfontPlayer.js @@ -173,7 +173,6 @@ stopNote: function (channel, pitch) { if (channel < 0 || channel > 15) return; if (_initialized && _synthInstance) { - try { _synthInstance.noteOff(channel, pitch, 127); } catch (e) {} try { _synthInstance.noteOff(channel, pitch); } catch (e) {} try { _synthInstance.noteOff(channel, pitch, 0); } catch (e) {} try { _synthInstance.allNotesOff(channel); } catch (e) {} @@ -182,11 +181,24 @@ }, playNote: function (note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine) { - if (_initialized && _synthInstance) { - this._playNoteSpessa(note, velocity, durationMs, startTime, program, channel, synthEngine); + if (!_initialized || !_synthInstance) { + // Try auto-init — create AudioContext + SpessaSynth + this._lazyInit(); + // Use oscillator as fallback (short duration for keyboard preview) + this._playNoteOsc(note, velocity, 2000, startTime, program, destinationNode, channel, synthEngine); return; } - this._playNoteOsc(note, velocity, durationMs, startTime, program, destinationNode, channel, synthEngine); + this._playNoteSpessa(note, velocity, durationMs, startTime, program, channel, synthEngine); + }, + + _lazyInit: async function () { + if (_initialized && _synthInstance) return; + if (_initPromise) return; + try { + const ctx = new (window.AudioContext || window.webkitAudioContext)(); + if (ctx.state === 'suspended') await ctx.resume(); + await this.init(ctx); + } catch (e) {} }, _playNoteSpessa: function (note, velocity, durationMs, startTime, program, channel, synthEngine) {