CustomAudioSource and CustomAudioTrack classes for sending custom audio to a call. This allows you to inject audio from files, generated audio, or processed audio streams.
CustomAudioSource
TheCustomAudioSource class represents a source of custom audio data that you can write audio frames to.
Class Definition
Constructor
sample_rate(int) - Audio sample rate in Hz (e.g., 16000, 44100, 48000)channels(int) - Number of audio channels (1 for mono, 2 for stereo)
Properties
sample_rate
int - Sample rate in Hz
channels
int - Number of channels (1 for mono, 2 for stereo)
Methods
write_frames
frame(bytes) - Raw audio data as bytes (16-bit PCM)completion(Optional[Callable[[int], None]]) - Optional callback called with the number of frames written
int - Number of frames written
Example:
CustomAudioTrack
TheCustomAudioTrack class wraps a CustomAudioSource and can be added to a call.
Class Definition
Constructor
audio_source(CustomAudioSource) - The audio source to use for this track
Properties
id
str - Track ID
Usage with CallClient
To send custom audio to a call, you need to:- Create a
CustomAudioSource - Create a
CustomAudioTrackfrom the source - Add the track to the call using
add_custom_audio_track() - Write audio frames to the source
Example: Playing Audio from File
Example: Generating Synthetic Audio
Example: Streaming Audio with Thread
Managing Custom Audio Tracks
You can update or remove custom audio tracks using CallClient methods:Audio Format Requirements
- Audio data must be 16-bit PCM (Pulse Code Modulation)
- Sample rate can be 8000, 16000, 24000, 48000 Hz (16000 recommended)
- Channels can be 1 (mono) or 2 (stereo)
- Audio frames should be provided in regular intervals (typically 10-20ms chunks)
Notes
- Custom audio tracks are mixed with microphone audio in the call
- Use
ignore_audio_level=Trueif you don’t want the audio to affect active speaker detection - Ensure audio frames are written at the correct rate to avoid stuttering or gaps
- The
completioncallback inwrite_frames()is called asynchronously when frames are consumed
See Also
- CallClient.add_custom_audio_track() - Add custom audio to call
- CallClient.update_custom_audio_track() - Update custom audio track
- CallClient.remove_custom_audio_track() - Remove custom audio track
- AudioData - Receiving audio from calls
- VirtualMicrophoneDevice - Alternative approach for custom audio