Skip to main content

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

Creates a default audio interface that uses PyAudio for audio input and output. Raises: ImportError if PyAudio is not installed.

Configuration

Audio streams use 16-bit PCM mono format at 16kHz sample rate.

Methods

start

Starts the audio interface. Called once before the conversation starts.
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

Stops the audio interface. Called once after the conversation ends. Cleans up resources and stops audio streams.

output

Output audio to the user.
bytes
required
Audio data in 16-bit PCM mono format at 16kHz. This method returns quickly and does not block.

interrupt

Interruption signal to stop audio output. Called when the user interrupts the agent, and all previously buffered audio output should be stopped.

Example


AsyncDefaultAudioInterface

Default implementation using PyAudio for asynchronous audio I/O.

Requirements

Constructor

Creates a default async audio interface that uses PyAudio for audio input and output. Raises: ImportError if PyAudio is not installed.

Configuration

Methods

All methods are async and should be awaited.

start

Starts the audio interface.
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

Stops the audio interface and cleans up resources.

output

Output audio to the user.
bytes
required
Audio data in 16-bit PCM mono format at 16kHz.

interrupt

Interruption signal to stop audio output.

Example


Custom Audio Interfaces

You can create custom audio interfaces by implementing the AudioInterface 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

  1. Non-blocking: The output() method should return quickly and not block the calling thread. Use buffering if needed.
  2. Input callback: The input_callback should be called regularly with fresh audio data. Don’t call it after stop() is called.
  3. Interruption: The interrupt() method should clear any buffered output audio immediately.
  4. Resource cleanup: Always clean up audio resources in the stop() method.