how to use the AudioProcessingModule to preprocess...
# ask-ai
b
how to use the AudioProcessingModule to preprocess the audio in livekit agents, give simple code example
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
AudioProcessingModule in LiveKit agents lets you preprocess audio with features like echo cancellation, noise suppression, high-pass filtering, and auto gain control.Basic Usage Example:from livekit import rtc # Initialize the AudioProcessingModule with desired features apm = rtc.AudioProcessingModule( echo_cancellation=True, noise_suppression=True, high_pass_filter=True, auto_gain_control=True, ) Processing Audio Frames:# Process the main audio stream (e.g., microphone input) apm.process_stream(audio_frame) # Process the reverse stream (e.g., speaker output for echo cancellation) apm.process_reverse_stream(render_frame) • Audio frames must be exactly 10 ms in duration • The input audio frame is modified in-place by the processing module • For echo cancellation, call
process_reverse_stream()
for the far-end audio (speaker output) and
process_stream()
for the near-end audio (microphone input) Optional: Set Stream Delay (for echo processing):# Set delay between reverse stream and main stream processing apm.set_stream_delay_ms(delay_ms) This module provides WebRTC-based audio processing to improve real-time audio quality in your LiveKit agent applications. Sources: livekit-rtc/livekit/rtc/apm.py | livekit-agents/livekit/agents/voice/chat_cli.py