42 lines
1004 B
JavaScript
42 lines
1004 B
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);
|
|
this.port.postMessage({ type: 'CONSUMED', n: fi });
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
registerProcessor('fluidsynth-bridge', FluidSynthBridge);
|