Files
SonicForgeStudio/app/static/js/services/trackInstrument.js
T

84 lines
4.0 KiB
JavaScript

// SonicForge TrackInstrument Service
// Bọc engine per-track: playNote/noteOff theo TrackInstrumentCtx
// (ch/program/synthEngine/sfId/bank/prog/dest) — dùng cho preview mọi nguồn
// (keybed, piano roll, draw, hardware MIDI, click) qua UnifiedMidiRouter.
// T15: native-first — SF track đi thẳng vào engine native (NativeAudioService
// + SF host bridge), SonicSF WASM chỉ là fallback khi native không sẵn sàng.
// Tạo mới mỗi note-on (overwrite registry) — voice cũ giữ engine cũ trong
// tracker nên note-off luôn stop đúng channel.
window.TrackInstrument = window.TrackInstrument || {};
(function () {
function TrackInstrument(trackId, ctx) {
this.trackId = trackId;
this.ch = ctx ? (ctx.ch != null ? ctx.ch : 0) : 0;
this.program = ctx ? ctx.program : undefined;
this.synthEngine = ctx ? ctx.synthEngine : undefined;
this.sfId = ctx ? ctx.sfId : undefined;
this.bank = ctx ? (ctx.bank || 0) : 0;
this.prog = ctx ? (ctx.prog || 0) : 0;
this.dest = ctx ? (ctx.dest || null) : null;
}
// Native engine sẵn sàng cho track SF? (chỉ khi có sfId — đường native)
TrackInstrument.prototype._nativeReady = function () {
try { return !!(window.SonicNativeAudio && this.sfId); } catch (e) { return false; }
};
// Đảm bảo channel đã select đúng instrument trước khi play (fire-and-forget:
// playNote tự load + retry nếu SF chưa load xong — dedup trong loadSoundFont).
TrackInstrument.prototype._ensure = function () {
try {
if (!window.SonicSF || !window.SonicSF.selectInstrument) return;
if (this.sfId) {
window.SonicSF.selectInstrument(this.ch, this.bank, this.prog, this.sfId);
} else if (this.program !== undefined) {
window.SonicSF.selectInstrument(this.ch, 0, this.program, null);
}
} catch (e) {
console.warn('[TrackInstrument] selectInstrument error:', e);
}
};
// velocity: router đã normalize int 1-127 (unifiedMidiRouter.normalizeVelocity).
TrackInstrument.prototype.playNote = function (pitch, velocity, durationMs, startTime) {
var vel = velocity != null ? velocity : 100;
if (this._nativeReady()) {
try {
var self = this;
var dur = (durationMs != null ? durationMs : 500) || 500;
// ensure native SF engine cho track (server dedup theo track_id)
window.SonicNativeAudio.ensureSf(this.trackId, this.sfId, this.bank, this.prog)
.catch(function () {});
window.SonicNativeAudio.noteOn('sf', this.trackId, this.ch, pitch, vel);
// ponytail: TrackInstrument không biết audioCtx → bỏ startTime
// offset (delay = duration); thêm scheduling chính xác khi cần
setTimeout(function () { self.noteOff(pitch); }, dur + 40);
return;
} catch (e) {
console.warn('[TrackInstrument] native playNote error:', e);
}
}
// Fallback SonicSF (WASM)
try {
if (!window.SonicSF || !window.SonicSF.playNote) return;
this._ensure();
var durF = (durationMs != null ? durationMs : 500) || 500;
window.SonicSF.playNote(pitch, velocity != null ? velocity : 0.8, durF, startTime, this.program, this.dest, this.ch, this.synthEngine);
} catch (e) {
console.warn('[TrackInstrument] playNote error:', e);
}
};
TrackInstrument.prototype.noteOff = function (pitch) {
if (this._nativeReady()) {
try { window.SonicNativeAudio.noteOff('sf', this.trackId, this.ch, pitch); return; } catch (e) {}
}
try {
if (window.SonicSF && window.SonicSF.stopNote) window.SonicSF.stopNote(this.ch, pitch);
} catch (e) {}
};
window.TrackInstrument = TrackInstrument;
})();