Overview
TheAudioInterface provides an abstraction for handling audio input and output in conversations. The SDK includes DefaultAudioInterface, a PyAudio-based implementation for real-time audio streaming.
DefaultAudioInterface
The built-in audio interface for capturing microphone input and playing agent responses:DefaultAudioInterface requires PyAudio. Install it with: pip install pyaudioAudio Specifications
The audio interface uses the following format:16-bit PCM
Audio format for input and output
Mono (1)
Single channel audio
16kHz
16,000 samples per second
4000 samples
250ms of audio per input chunk
1000 samples
62.5ms of audio per output chunk
How It Works
Input Flow
- Microphone captures user audio via PyAudio callback
- Audio is passed to
input_callbackprovided by the Conversation - Audio is encoded and sent to the agent via WebSocket
Output Flow
- Agent sends audio chunks via WebSocket
- Audio is queued in a thread-safe output queue
- Background thread writes audio to PyAudio output stream
- Audio plays through speakers/headphones
Interruption Handling
When the user interrupts the agent:- Conversation detects interruption event
interrupt()is called on the audio interface- Output queue is cleared to stop current playback
- New agent response starts playing
Custom Audio Interface
Implement your own audio interface by subclassingAudioInterface:
AudioInterface Methods
method
required
Called once before conversation starts. Set up audio capture and playback.Parameters:
input_callback: Function to call with captured audio chunks (16-bit PCM, 16kHz, mono)
method
required
Called once after conversation ends. Clean up all audio resources.
method
required
Called with audio bytes to play. Should return quickly without blocking.Parameters:
audio: Audio bytes in 16-bit PCM, 16kHz, mono format
method
required
Called when user interrupts. Stop current playback immediately.
Async Audio Interface
For async workflows, useAsyncAudioInterface:
AsyncConversation:
File-Based Audio Interface
Example: Read from a file instead of microphone:Troubleshooting
PyAudio installation errors
PyAudio installation errors
If you encounter errors installing PyAudio:macOS:Ubuntu/Debian:Windows:
Download the appropriate wheel from PyPI
No audio input/output
No audio input/output
Check that your system has a default microphone and speakers configured:
Audio latency issues
Audio latency issues
Reduce buffer sizes for lower latency (at the cost of potential audio glitches):
Audio quality issues
Audio quality issues
Ensure your audio is in the correct format:
- 16-bit PCM (not 8-bit or 32-bit float)
- 16kHz sample rate (not 44.1kHz or 48kHz)
- Mono (not stereo)
Best Practices
Buffer Management
Use appropriate buffer sizes. Smaller buffers reduce latency but increase CPU usage and potential glitches.
Thread Safety
Use thread-safe queues for audio output. PyAudio callbacks run in separate threads.
Error Handling
Handle audio device errors gracefully. Devices can disconnect or change during a session.
Resource Cleanup
Always clean up audio resources in
stop() to prevent memory leaks and device locks.