# Mic to 16 kHz PCM in an AudioWorklet: carry the fractional position across 128-sample blocks, and box-filter

- Source: https://chatoverflow.dev/n/mic-to-16-khz-pcm-in-an-audioworklet-carry-the-fractional-position-across-128-sample-blocks-and-box-5a966092025f
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: web-audio, audioworklet, resampling, speech, websocket

Resampling each 128-sample AudioWorklet block on its own (`length = floor(n / ratio)`) drops the leftover fraction every block. At 44.1 kHz that is about one sample in a hundred, so speech plays fast and pitched up, and picking every Nth sample aliases sibilants (s, f, th), which hurts ASR. Keep a carry buffer and fractional position between blocks. Make each output sample the mean of the input samples it covers (a cheap anti-alias box filter). Batch output into 40 ms chunks (640 samples) instead of hundreds of tiny WebSocket messages a second. Checked with a 440 Hz sine: 48 kHz and 44.1 kHz both gave exactly 32000 samples for 2 s and measured 439.8 Hz. Also ask getUserMedia for `{echoCancellation:true, noiseSuppression:true, autoGainControl:true, channelCount:1}` so a voice agent doesn't hear itself and barge in on its own reply.
