fix: use Float32Array intermediate for AudioData.copyTo

- AudioData.copyTo requires destination Float32Array, not AudioBuffer channel
- Create temp Float32Array per channel, copyTo into it, then set into AudioBuffer
- Properly handle 48kHz stereo Opus output to AudioContext
This commit is contained in:
MythEclipse
2026-05-13 22:17:43 +07:00
parent 26efaa9e4d
commit 9066e5b1f2

View File

@@ -485,7 +485,11 @@ const state = {
}
function playAudioData(audioData) {
if (!state.audioContextListen) return;
if (!state.audioContextListen) {
audioData.close();
return;
}
try {
const sampleRate = audioData.sampleRate;
const frameCount = audioData.numberOfFrames;
const numberOfChannels = audioData.numberOfChannels;
@@ -495,7 +499,10 @@ const state = {
sampleRate
);
for (let ch = 0; ch < numberOfChannels; ch++) {
audioData.copyTo(audioBuffer.getChannelData(ch), { planeIndex: ch });
const channelData = audioBuffer.getChannelData(ch);
const tempArray = new Float32Array(frameCount);
audioData.copyTo(tempArray, { planeIndex: ch });
channelData.set(tempArray);
}
const source = state.audioContextListen.createBufferSource();
source.buffer = audioBuffer;
@@ -503,8 +510,12 @@ const state = {
const startAt = Math.max(state.nextStartTime, state.audioContextListen.currentTime);
source.start(startAt);
state.nextStartTime = startAt + audioBuffer.duration;
} catch (error) {
console.error('Play audio error:', error);
} finally {
audioData.close();
}
}
function playPcm(arrayBuffer) {
if (!state.audioContextListen) return;