4d2da5a272
- ScriptProcessorNode fallback khi AudioWorkletNode fails - Render trực tiếp trong onaudioprocess callback, không cần setInterval - Worklet log peak + queue depth mỗi 50 process() calls - Log audioCtx state + sampleRate khi init
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
class FluidSynthBridge extends AudioWorkletProcessor {
|
|
constructor() {
|
|
super();
|
|
this.leftQ = [];
|
|
this.rightQ = [];
|
|
this.called = 0;
|
|
this.port.onmessage = (e) => {
|
|
const d = e.data;
|
|
if (d.type === 'PCM') {
|
|
this.leftQ.push(d.L);
|
|
this.rightQ.push(d.R);
|
|
}
|
|
};
|
|
}
|
|
|
|
process(inputs, outputs) {
|
|
const out = outputs[0];
|
|
if (!out) return true;
|
|
this.called++;
|
|
const len = out[0].length;
|
|
const qL = this.leftQ;
|
|
const qR = this.rightQ;
|
|
let fi = 0;
|
|
let si = 0;
|
|
for (let i = 0; i < len; i++) {
|
|
if (fi >= qL.length) { out[0][i] = 0; out[1][i] = 0; continue; }
|
|
out[0][i] = qL[fi][si];
|
|
out[1][i] = qR[fi][si];
|
|
si++;
|
|
if (si >= qL[fi].length) { fi++; si = 0; }
|
|
}
|
|
if (fi > 0) { this.leftQ.splice(0, fi); this.rightQ.splice(0, fi); }
|
|
if (this.called % 50 === 0) {
|
|
var pk = 0;
|
|
for (var j = 0; j < len; j++) {
|
|
var v = out[0][j] > 0 ? out[0][j] : -out[0][j];
|
|
if (v > pk) pk = v;
|
|
}
|
|
if (pk > 0) console.log('[FluidSynth:bridge] process #' + this.called + ' peak:' + pk.toFixed(6) + ' q:' + qL.length);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
registerProcessor('fluidsynth-bridge', FluidSynthBridge);
|