🎤Micdrop

Detect the Turns of the User on the Server

By default, the client finds the turns: its voice activity detection hears the user start and stop speaking, and the server answers when it is told. Give the server a vad and it finds them itself, in the audio it receives, with the same code the client runs on its microphone.

Terminal window
npm install @micdrop/vad onnxruntime-node
import { MicdropServer } from '@micdrop/server'
import '@micdrop/vad/node'
new MicdropServer(socket, {
vad: 'silero',
agent,
stt,
tts,
})

When to detect the turns on the server

  • A phone call streams the caller continuously, with no client to say when a sentence ends. The server has to do it.
  • A client that cannot run a VAD, a microcontroller or a script, streams its audio and lets the server listen.
  • Keeping every decision in one place, with the same settings for all users whatever their device.

The client remains the better place otherwise. It sends audio only while the user speaks, and a turn detector in the browser can close a turn sooner, since the client is what closes it.

The client follows

A server with vad first tells the client ServerTurns. A Micdrop client from version 3.8 then streams its whole microphone and follows the server: its state shows the user speaking when the server says so, and it stops the assistant when the server hears the user over it. Nothing changes in the code of the page. The protocol page shows what changes on the wire.

The client stops its own VAD once the server announced the turns. A page that knows its server finds them can say so with serverTurns, and then runs no VAD at all, not even during the connection, which spares the browser the Silero download:

await Micdrop.start({ url: 'wss://example.com/call', serverTurns: true })

A server that turns out to leave the turns to the client gets them all the same: the client starts its VAD at the first message that is not ServerTurns.

A muted or paused client streams nothing. A client without echo cancellation also stops streaming while the assistant speaks, so the server never takes the assistant for the user.

Choosing the VAD

vad takes the same values as on the client:

ValueWhat it hears
'silero'A voice, told apart from noise by the Silero model. Needs import '@micdrop/vad/node' and onnxruntime-node.
'volume'A level above a threshold, with nothing to install. Fine in a quiet room, fooled by noise.
A VAD instancenew SileroVAD({ ... }) or new VolumeVAD({ ... }), to set its thresholds
An arraySeveral VADs that must all agree, such as ['volume', 'silero']

Silero takes well under a millisecond per window of 32 ms on one core, and every call of the process shares one copy of the model, so a server holds hundreds of calls before it weighs anything. The model is fetched from a CDN on the first call. Serve it from your own disk so a server never reaches out at boot:

import { preloadSilero, setSileroOptions } from '@micdrop/vad/node'
setSileroOptions({ model: './models/silero_vad_v5.onnx' })
await preloadSilero()

With a turn detector

With vad set, the turn detector of the server decides when each turn it detects is over. A pause in the middle of a sentence leaves the turn open, the audio keeps flowing into the same transcript, and the turn closes when the sentence lands or after turnMaxWait:

import { SmartTurn } from '@micdrop/smart-turn'
import '@micdrop/smart-turn/node'
new MicdropServer(transport, {
vad: 'silero',
turnDetector: new SmartTurn(),
agent,
stt,
tts,
})

One recorder for both sides

The VADs and the recorder that turns them into turns live in @micdrop/vad, shared by the client and the server. The server feeds an AudioInput with what it receives and runs a MicRecorder on it, the class the client runs on its microphone, with its reserve of audio that keeps the first syllable of a sentence. A turn found on the server starts and ends where the client would have put it.