Live captions in the browser
Stream the microphone to Duyu and show the words as they are spoken: partial drafts while someone talks, a settled final line when they pause.
1 · A token from your server
Do not give the browser a key. Mint a 10-minute session token on your server; the browser opens the WebSocket with ?token=. Details are on Authentication.
Next.js
2 · Streaming the microphone
A small AudioWorklet collects the microphone samples, which go out as Int16 PCM about every 50 ms. Tell Duyu the browser's own sample rate (usually 48,000) in the start message; it resamples.
captions.js
3 · Showing the words
Replace the draft on every partial; on a final, append it and clear the draft. A partial may still change; only a final is settled.
Tips
- Keep the browser's
echoCancellationandnoiseSuppressionon, so audio from the speakers does not end up in the captions. - Draw a level meter from the
vadevents; no separate VAD is needed. See Live streaming. - Send
stopwhen you are done: the open sentence closes as afinal, thenstoppedfollows. The billed time is instopped.audio_s.