Skip to content

Commit 94b894c

Browse files
committed
Code clarity and an additional assertion in the AudioWorklet
1 parent 2080402 commit 94b894c

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/host/audioworklet/worklet.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,20 @@ registerProcessor("CpalProcessor", class WasmProcessor extends AudioWorkletProce
2727
const interleaved_start = interleaved_ptr / 4; // Convert byte offset to f32 index
2828
const interleaved = this.wasm_memory;
2929

30+
const total_samples = frame_size * channels_count;
31+
if (interleaved_start + total_samples > this.wasm_memory.length) {
32+
console.error("CpalProcessor: Audio buffer out of bounds! Ptr:", interleaved_ptr, "Len:", total_samples);
33+
return false; // Safely stop the node
34+
}
35+
3036
// Deinterleave: read strided from Wasm, write sequential to output
3137
for (let ch = 0; ch < channels_count; ch++) {
3238
const channel = channels[ch];
33-
let src = interleaved_start + ch;
39+
let read_pos = interleaved_start + ch;
3440

3541
for (let i = 0; i < frame_size; i++) {
36-
channel[i] = interleaved[src];
37-
src += channels_count;
42+
channel[i] = interleaved[read_pos];
43+
read_pos += channels_count;
3844
}
3945
}
4046

0 commit comments

Comments
 (0)