Revert chunk size to 8KB — larger sizes cause AWS deserialization errors

Keep 8KB CHUNK_SIZE (proven stable) but replace 10ms setTimeout delay
with a microtask break every 16 chunks. This avoids the AWS SDK
"Deserialization error: inspect {error}.\$response" while still
eliminating the ~1.25s/MB artificial delay from the old 10ms sleep.
This commit is contained in:
ifedan-ed 2026-03-29 01:21:43 +00:00
parent 35f03ac0ba
commit 72e91e940c

View file

@ -59,11 +59,16 @@ function convertToPCM(inputBuffer) {
}
async function* makeAudioStream(buffer) {
// Send chunks as fast as possible — AWS handles backpressure via HTTP/2 flow control.
// Larger chunks (32KB) reduce overhead and speed up streaming significantly.
var streamChunkSize = 32768; // 32 KB (AWS max per event frame)
for (var i = 0; i < buffer.length; i += streamChunkSize) {
yield { AudioEvent: { AudioChunk: buffer.slice(i, i + streamChunkSize) } };
// 8KB chunks — larger sizes cause AWS SDK deserialization errors
// ("to see the raw response, inspect the hidden field {error}.$response").
// No artificial delay between chunks; yield with a microtask break
// to avoid blocking the event loop without adding real latency.
for (var i = 0; i < buffer.length; i += CHUNK_SIZE) {
yield { AudioEvent: { AudioChunk: buffer.slice(i, i + CHUNK_SIZE) } };
// Microtask break every 16 chunks (~128KB) to keep event loop responsive
if ((i / CHUNK_SIZE) % 16 === 15) {
await new Promise(function(r) { setImmediate(r); });
}
}
}