Overview
Audio interfaces provide an abstraction for handling audio input and output in conversational AI sessions. ElevenLabs provides default implementations, but you can also create custom interfaces.DefaultAudioInterface
Default implementation using PyAudio for synchronous audio I/O.Requirements
Constructor
ImportError if PyAudio is not installed.
Configuration
Methods
start
Callable[[bytes], None]
required
Callback function that will be called regularly with input audio chunks from the user. Audio is in 16-bit PCM mono format at 16kHz. Recommended chunk size is 4000 samples (250 milliseconds).
stop
output
bytes
required
Audio data in 16-bit PCM mono format at 16kHz. This method returns quickly and does not block.
interrupt
Example
AsyncDefaultAudioInterface
Default implementation using PyAudio for asynchronous audio I/O.Requirements
Constructor
ImportError if PyAudio is not installed.
Configuration
Methods
All methods are async and should be awaited.start
Callable[[bytes], Awaitable[None]]
required
Async callback function that will be called regularly with input audio chunks from the user. Audio is in 16-bit PCM mono format at 16kHz.
stop
output
bytes
required
Audio data in 16-bit PCM mono format at 16kHz.
interrupt
Example
Custom Audio Interfaces
You can create custom audio interfaces by implementing theAudioInterface or AsyncAudioInterface abstract base classes.
AudioInterface (Sync)
AsyncAudioInterface (Async)
Custom Interface Example
Here’s an example of a custom audio interface that saves audio to files:Audio Format Requirements
All audio interfaces must use the following format:- Format: 16-bit PCM
- Channels: Mono (1 channel)
- Sample Rate: 16kHz (16000 Hz)
- Recommended chunk size: 4000 samples (250 milliseconds)
Implementation Notes
-
Non-blocking: The
output()method should return quickly and not block the calling thread. Use buffering if needed. -
Input callback: The
input_callbackshould be called regularly with fresh audio data. Don’t call it afterstop()is called. -
Interruption: The
interrupt()method should clear any buffered output audio immediately. -
Resource cleanup: Always clean up audio resources in the
stop()method.