🎤Micdrop

Track the Call State in the Browser Client

Monitor and react to real-time conversation state changes during voice calls.

StateChange Event

The StateChange event is emitted whenever the client state changes. It provides both the current state and the previous state for comparison:

Micdrop.on(
'StateChange',
(currentState: MicdropState, previousState: MicdropState) => {
// Handle state change
}
)

See MicdropClient for more details.

State Properties

The MicdropState object contains these properties:

PropertyTypeDescription
statusMicdropStatusWhere the call stands, see below
activityMicdropActivityWho has the floor, see below
isStartingbooleanCall is connecting and starting
isStartedbooleanCall is active and ready
isReconnectingbooleanCall is attempting to reconnect
isMutedbooleanCall is muted, and still running
isPausedbooleanCall is paused (no audio processing)
isListeningbooleanReady to detect user speech
isProcessingbooleanProcessing user input on server
isUserSpeakingbooleanUser voice detected
isAssistantSpeakingbooleanAssistant audio playing
isMicStartedbooleanMicrophone is active
isMicMutedbooleanMicrophone is muted
micDeviceIdstringActive microphone device ID
speakerDeviceIdstringActive speaker device ID
micDevicesMicdropDevice[]Available microphones
speakerDevicesMicdropDevice[]Available speakers
conversationMicdropConversationMessage history
partialAssistantMessagestringAnswer being written, if streamed
errorMicdropClientErrorCurrent error if any

Status and Activity

Two properties hold the whole state of a call, each with one value at a time. The booleans above spell them out, and stay handy for a quick check.

status follows the call itself:

ValueMeaningBooleans
stoppedNo call. The microphone may still be on, for a test
startingOpening the microphone and the connectionisStarting
startedThe call is onisStarted
reconnectingThe connection dropped, and the call waits for a new oneisStarted, isReconnecting

activity says who has the floor:

ValueMeaningBoolean
idleNothing to hear nor to wait for: stopped, paused or muted
listeningWaiting for the user to speakisListening
userSpeakingThe user speaksisUserSpeaking
processingThe server prepares the answerisProcessing
assistantSpeakingThe answer is playingisAssistantSpeaking

A switch on them covers every case once:

Micdrop.on('StateChange', (state) => {
if (state.status === 'reconnecting') return showBanner('Reconnecting…')
switch (state.activity) {
case 'listening':
return showIndicator('🎤 Listening')
case 'userSpeaking':
return showIndicator('🗣️ You are speaking')
case 'processing':
return showIndicator('⚡ Thinking')
case 'assistantSpeaking':
return showIndicator('🤖 Speaking')
case 'idle':
return hideIndicator()
}
})

State Diagram

stateDiagram-v2
    [*] --> isStarting: Start call
    isStarting --> isStarted: Start mic and connection
    isStarted --> [*]: Stop call

    state isStarted {
        [*] --> isProcessing
        isListening --> isUserSpeaking : User speaks
        isUserSpeaking --> isProcessing : User stops speaking
        isProcessing --> isAssistantSpeaking : Generate and play answer
        isProcessing --> isUserSpeaking : User interrupts
        isProcessing --> isListening : No answer generated
        isAssistantSpeaking --> isListening : Assistant finished
        isAssistantSpeaking --> isUserSpeaking : User interrupts
    }

    isStarted --> isPaused : Pause call
    isPaused --> isStarted : Resume call

Notes:

  • User can interrupt the assistant at any time by speaking during the isProcessing or isAssistantSpeaking states. This allows for natural conversation flow. To prevent interruptions during assistant responses, use the disableInterruption setting. Speech heard during the first 2 seconds of the first answer is taken for echo rather than an interruption.
  • When isPaused is true, isStarted stays true.
  • When isReconnecting is true, isStarted stays true.
  • On a phone, speakerDeviceId is a route rather than a device, 'speaker' or 'earpiece'. See Audio output and devices.
  • isMicStarted becomes true when we start the mic (with Micdrop.startMic) or start the call (with Micdrop.start).

Starting a Call

Micdrop.on('StateChange', (state, prevState) => {
if (state.status === prevState.status) return
if (state.status === 'starting') {
console.log('🔄 Connecting...')
} else if (state.status === 'started') {
console.log('✅ Connected and ready!')
} else if (state.status === 'reconnecting') {
console.log('🔄 Connection lost, reconnecting...')
}
})

Conversation Flow

The typical conversation flow follows this pattern:

Micdrop.on('StateChange', (state, prevState) => {
// 1. Listening for user input
if (state.isListening && !prevState.isListening) {
console.log('🎤 Listening...')
// Show listening indicator
}
// 2. User starts speaking
if (state.isUserSpeaking && !prevState.isUserSpeaking) {
console.log('🗣️ User speaking...')
// Show speaking indicator
}
// 3. Processing user input
if (state.isProcessing && !prevState.isProcessing) {
console.log('⚡ Processing...')
// Show processing indicator
}
// 4. Assistant responds
if (state.isAssistantSpeaking && !prevState.isAssistantSpeaking) {
console.log('🤖 Assistant speaking...')
// Show assistant speaking indicator
}
})

With React

For React applications, use the useMicdropState hook to automatically subscribe to state changes.

First, install the React package:

Terminal window
npm install @micdrop/react

Then use the hook:

import { useMicdropState } from '@micdrop/react'
function CallInterface() {
const {
isStarting,
isStarted,
isReconnecting,
isListening,
isUserSpeaking,
isProcessing,
isAssistantSpeaking,
isPaused,
} = useMicdropState()
return (
<div className="call-interface">
{isStarting && <div className="status starting">Starting...</div>}
{isStarted && <div className="status started">Started</div>}
{isReconnecting && (
<div className="status reconnecting">🔄 Reconnecting...</div>
)}
{isPaused && <div className="status paused">⏸️ Call paused</div>}
{isMuted && <div className="status muted">⏸️ Call muted</div>}
{isListening && <div className="status listening">🎤 Listening</div>}
{isUserSpeaking && (
<div className="status user-speaking">🗣️ You're speaking</div>
)}
{isProcessing && <div className="status processing">⚡ Processing</div>}
{isAssistantSpeaking && (
<div className="status assistant-speaking">🤖 Assistant speaking</div>
)}
</div>
)
}

More information about hooks in the React hooks section.