Varmirdocs
Docs/Guides/Browser microphone capture

Browser microphone capture

A working recipe for piping a browser microphone into the WebSocket stream. ~30 lines of code, no extra libraries.

Permissions

getUserMedia needs a secure context (HTTPS or localhost). The first call shows the browser's mic permission prompt; the user's choice is sticky per origin.

Capture & downsample

Create an AudioContext at 16 kHz (the model's native rate) so the browser does the resampling for you.

capture.js
const media = await navigator.mediaDevices.getUserMedia({ audio: true });
const ctx = new AudioContext({ sampleRate: 16000 });
const source = ctx.createMediaStreamSource(media);
const proc = ctx.createScriptProcessor(4096, 1, 1);

source.connect(proc);
proc.connect(ctx.destination);
proc.onaudioprocess = (e) => sendChunk(e.inputBuffer.getChannelData(0));

Send to /stream

The wire format is pcm_s16le. Convert the Float32 frame from Web Audio and ship it over the open WebSocket.

send.js
function sendChunk(float32) {
  // Convert Float32 [-1, 1] → Int16 little-endian
  const i16 = new Int16Array(float32.length);
  for (let i = 0; i < float32.length; i++) {
    i16[i] = Math.max(-1, Math.min(1, float32[i])) * 0x7fff;
  }
  ws.send(i16.buffer);
}
Browser microphone capture — Varmir docs