provide code which uses AudioProcessingModule in l...
# ask-ai
b
provide code which uses AudioProcessingModule in livekit agents
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.
To use the AudioProcessingModule in LiveKit agents for real-time audio processing (including echo cancellation, noise suppression, high-pass filtering, and gain control), follow this example:
Copy code
from livekit.rtc import AudioProcessingModule, AudioFrame

# Initialize the AudioProcessingModule with desired features
apm = AudioProcessingModule(
    echo_cancellation=True,
    noise_suppression=True,
    high_pass_filter=True,
    auto_gain_control=True,
)

sample_rate = 48000
num_channels = 1
frames_per_chunk = sample_rate // 100  # 10ms chunks

while processing_audio:
    # Create AudioFrame objects (10ms duration required)
    capture_frame = AudioFrame(
        data=capture_data.tobytes(),
        sample_rate=sample_rate,
        num_channels=num_channels,
        samples_per_channel=frames_per_chunk,
    )
    render_frame = AudioFrame(
        data=render_data.tobytes(),
        sample_rate=sample_rate,
        num_channels=num_channels,
        samples_per_channel=frames_per_chunk,
    )

    # Process both streams for echo cancellation
    apm.process_reverse_stream(render_frame)
    apm.process_stream(capture_frame)

# Set stream delay for echo cancellation (in ms)
total_delay = output_delay + input_delay
apm.set_stream_delay_ms(int(total_delay * 1000))
• Audio frames must be exactly 10 ms in duration and in 16-bit PCM format. • Call
set_stream_delay_ms()
if echo cancellation is enabled. • Integrate this module into your agent's audio pipeline for best results. Sources: livekit-rtc/tests/test_apm.py | livekit-agents/livekit/agents/voice/chat_cli.py | livekit-ffi/src/server/requests.rs