fix: crash AudioWorklet khi output mono - xử lý mọi số channel trong fluidsynth-bridge + ép stereo outputChannelCount=2

This commit is contained in:
2026-08-03 11:20:30 +07:00
parent 47b1633bd3
commit 34ad02dd6b
4 changed files with 26 additions and 6 deletions
+9 -1
View File
@@ -141,7 +141,15 @@
if (!_useScriptNode) {
try {
_workletNode = new AudioWorkletNode(_audioCtx, 'fluidsynth-bridge');
// Force stereo output regardless of the device's
// channel count — FluidSynth renders stereo, and a
// mono output would crash the worklet (out[1] undefined).
_workletNode = new AudioWorkletNode(_audioCtx, 'fluidsynth-bridge', {
numberOfOutputs: 1,
outputChannelCount: [2],
channelCount: 2,
channelCountMode: 'explicit'
});
_workletNode.connect(_gainNode);
console.log("[SonicSF] AudioWorklet node connected via gain");
_startRenderLoop();
+11 -4
View File
@@ -15,17 +15,24 @@ class FluidSynthBridge extends AudioWorkletProcessor {
process(inputs, outputs) {
const out = outputs[0];
if (!out) return true;
if (!out || out.length === 0) return true;
this.called++;
const numCh = out.length;
const len = out[0].length;
const qL = this.leftQ;
const qR = this.rightQ;
let fi = 0;
let si = 0;
// Handle any output channel count (mono devices produce 1 channel, so
// out[1] may be undefined — never write into a missing channel).
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];
if (fi >= qL.length) {
for (let c = 0; c < numCh; c++) out[c][i] = 0;
continue;
}
for (let c = 0; c < numCh; c++) {
out[c][i] = c % 2 === 0 ? qL[fi][si] : qR[fi][si];
}
si++;
if (si >= qL[fi].length) { fi++; si = 0; }
}
+1 -1
View File
@@ -16,7 +16,7 @@
<script src="/static/js/services/audioEngine.js?v=202607271016"></script>
<script src="/static/js/services/storage.js?v=202607271016"></script>
<script src="/static/js/services/soundfontStorage.js?v=202607271016"></script>
<script src="/static/js/services/soundfontPlayer.js?v=202608031210"></script>
<script src="/static/js/services/soundfontPlayer.js?v=202608031230"></script>
<script src="/static/js/services/aiGateway.js?v=202607271016"></script>
<script src="/static/js/services/dawCommandDispatcher.js?v=202607271016"></script>
<script src="/static/js/services/pianoRollTabService.js?v=202607272044"></script>