Call State
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:
Property | Type | Description |
---|---|---|
isStarting | boolean | Call is connecting and starting |
isStarted | boolean | Call is active and ready |
isPaused | boolean | Call is paused (no audio processing) |
isListening | boolean | Ready to detect user speech |
isProcessing | boolean | Processing user input on server |
isUserSpeaking | boolean | User voice detected |
isAssistantSpeaking | boolean | Assistant audio playing |
isMicStarted | boolean | Microphone is active |
isMicMuted | boolean | Microphone is muted |
micDeviceId | string | Active microphone device ID |
speakerDeviceId | string | Active speaker device ID |
micDevices | MediaDeviceInfo[] | Available microphones |
speakerDevices | MediaDeviceInfo[] | Available speakers |
conversation | MicdropConversation | Message history |
error | MicdropClientError | Current error if any |
State Diagramβ
Notes:
- User can interrupt the assistant at any time by speaking during the
isProcessing
orisAssistantSpeaking
states. This allows for natural conversation flow. To prevent interruptions during assistant responses, use thedisableInterruption
setting. - When
isPaused
is true,isStarted
stays true. isMicStarted
becomes true when we start the mic (withMicdrop.startMic
) or start the call (withMicdrop.start
).
Starting a Callβ
Micdrop.on('StateChange', (state, prevState) => {
if (state.isStarting && !prevState.isStarting) {
console.log('π Connecting...')
}
if (state.isStarted && !prevState.isStarted) {
console.log('β
Connected and ready!')
}
})
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:
npm install @micdrop/react
Then use the hook:
import { useMicdropState } from '@micdrop/react'
function CallInterface() {
const {
isStarting,
isListening,
isUserSpeaking,
isProcessing,
isAssistantSpeaking,
isPaused,
} = useMicdropState()
return (
<div className="call-interface">
{isStarting && <div className="status connecting">Connecting...</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>
)}
{isPaused && <div className="status paused">βΈοΈ Call paused</div>}
</div>
)
}
More information about hooks in the React hooks section.