Skip to main content

Overview

The AudioInterface 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 pyaudio

Audio 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

  1. Microphone captures user audio via PyAudio callback
  2. Audio is passed to input_callback provided by the Conversation
  3. Audio is encoded and sent to the agent via WebSocket

Output Flow

  1. Agent sends audio chunks via WebSocket
  2. Audio is queued in a thread-safe output queue
  3. Background thread writes audio to PyAudio output stream
  4. Audio plays through speakers/headphones

Interruption Handling

When the user interrupts the agent:
  1. Conversation detects interruption event
  2. interrupt() is called on the audio interface
  3. Output queue is cleared to stop current playback
  4. New agent response starts playing

Custom Audio Interface

Implement your own audio interface by subclassing AudioInterface:

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, use AsyncAudioInterface:
Use with AsyncConversation:

File-Based Audio Interface

Example: Read from a file instead of microphone:

Troubleshooting

If you encounter errors installing PyAudio:macOS:
Ubuntu/Debian:
Windows: Download the appropriate wheel from PyPI
Check that your system has a default microphone and speakers configured:
Reduce buffer sizes for lower latency (at the cost of potential audio glitches):
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)
Convert if needed:

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.