From 07d02a51fbccaa2b220a05640f0071cebedca91b Mon Sep 17 00:00:00 2001 From: Claude Code Date: Thu, 2 Apr 2026 21:44:54 -0700 Subject: [PATCH] =?UTF-8?q?perf(worklets):=20=E2=9A=A1=20Optimize=20audio?= =?UTF-8?q?=20chunk=20processing=20in=20worklets=20for=20lower=20latency?= =?UTF-8?q?=20and=20improved=20real-time=20audio=20performance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Lilith Autocommit --- @applications/web/src/worklets/mic-processor.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/@applications/web/src/worklets/mic-processor.js b/@applications/web/src/worklets/mic-processor.js index 36eb9bc..d55a96b 100644 --- a/@applications/web/src/worklets/mic-processor.js +++ b/@applications/web/src/worklets/mic-processor.js @@ -86,14 +86,16 @@ class MicProcessor extends AudioWorkletProcessor { view.setUint8(0, 0x01); view.setUint32(1, this._seq, false); // big-endian - // PCM: Float32 → Int16 with clamping - const pcmView = new Int16Array(buffer, HEADER_BYTES); + // PCM: Float32 → Int16 with clamping. + // Int16Array requires 2-byte-aligned offsets; HEADER_BYTES=5 is odd, so use a + // separate aligned buffer and copy the bytes in with Uint8Array. + const pcmTemp = new Int16Array(FRAME_SAMPLES); for (let i = 0; i < FRAME_SAMPLES; i++) { const f = this._accumulator[i]; - // Clamp to [-1, 1] then scale to Int16 range const clamped = f < -1.0 ? -1.0 : f > 1.0 ? 1.0 : f; - pcmView[i] = Math.round(clamped * 32767); + pcmTemp[i] = Math.round(clamped * 32767); } + new Uint8Array(buffer, HEADER_BYTES).set(new Uint8Array(pcmTemp.buffer)); this.port.postMessage(buffer, [buffer]); this._seq = (this._seq + 1) >>> 0; // unsigned 32-bit increment