is there a echo cancellation from the agent side l...
# ask-ai
b
is there a echo cancellation from the agent side like how there is BVC, or if i just use BVC its enough to cancel echo
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.
Hi there! I’m happy to guide you through the audio features LiveKit offers to help you get the best sound quality possible. Background Voice Cancellation (BVC): This wonderful feature helps remove background noise and quiets other voices around you, making the audio much clearer. You can easily enable it in your agent code using
RoomInputOptions
like this:
Copy code
room_input_options=RoomInputOptions(
    noise_cancellation=noise_cancellation.BVC(),
)
Agent-Side Echo Cancellation: This one’s really important too! It stops the agent from picking up its own audio output, which can cause echo. Even if you’re using BVC, echo cancellation is still essential. It’s built right into WebRTC through the
echoCancellation
setting in
AudioCaptureOptions
, and some SDKs have it enabled by default. For example, in the Rust SDK, you can set it up like this:
Copy code
let apm = AudioProcessingModule::new(
    echo_cancellation,  // enabled via args
    auto_gain_control,
    false,
    noise_suppression,
);
For the best audio experience, I recommend turning on both BVC and echo cancellation. This combination really helps keep your audio clean and free from any unwanted noise or interference. Sources: Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | Noise & echo cancellation | LiveKit Docs | Sources/LiveKit/Types/Options/AudioCaptureOptions.swift | examples/local_audio/src/main.rs | SIP troubleshooting guide | LiveKit Docs
b
how to turn on echo cancellation in python livekit agents
t
To help you enable echo cancellation in Python LiveKit agents, you have two great options: using the
AudioProcessingModule
for custom audio processing or simply relying on LiveKit's built-in client-side echo cancellation. Let me walk you through both! 1. Using `AudioProcessingModule`:
Copy code
from livekit.rtc import AudioProcessingModule

# Enable echo cancellation
apm = AudioProcessingModule(echo_cancellation=True)

# Process far-end audio (reverse stream)
apm.process_reverse_stream(far_end_audio_frame)

# Process near-end audio (forward stream)
apm.process_stream(microphone_audio_frame)

# Set delay between reverse and forward stream processing
apm.set_stream_delay_ms(delay_ms=50)  # Adjust based on your system
Just a quick tip: make sure your audio frames are exactly 10ms long for the best results. 2. Client-Side Echo Cancellation: LiveKit’s default audio processing already includes echo cancellation using WebRTC’s
echoCancellation
setting. This is turned on by default and continues to work even if you’re using Background Voice Cancellation (BVC). For most situations, the built-in client-side echo cancellation should do the trick. But if you have any questions or need a hand setting things up, I’m here to help — just let me know! Sources: examples/local_audio/src/main.rs | Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | rag/main.py | livekit-agents/livekit/agents/voice/chat_cli.py | livekit-rtc/livekit/rtc/_proto/audio_frame_pb2.pyi
b
How to enable or integrate this for AgentSession like BVC. from livekit.rtc import AudioProcessingModule # Enable echo cancellation apm = AudioProcessingModule(echo_cancellation=True) # Process far-end audio (reverse stream) apm.process_reverse_stream(far_end_audio_frame) # Process near-end audio (forward stream) apm.process_stream(microphone_audio_frame) # Set delay between reverse and forward stream processing apm.set_stream_delay_ms(delay_ms=50) # Adjust based on your system
t
To help you integrate echo cancellation with
AgentSession
in LiveKit, you have two great options to choose from: 1. Enhanced Noise Cancellation (Recommended): I suggest using LiveKit's enhanced noise cancellation with Background Voice Cancellation (BVC), which also includes echo cancellation:
Copy code
from livekit.rtc import BackgroundVoiceCancellation

await session.start({
  agent,
  room: ctx.room,
  inputOptions: {
    noiseCancellation: BackgroundVoiceCancellation(),
  },
});
This method applies enhanced noise and echo cancellation to inbound audio processing and is especially optimized for voice AI applications. It’s straightforward and effective! 2. Direct AudioProcessingModule Integration (Advanced): If you’re working on more advanced scenarios, you can integrate
AudioProcessingModule
directly at the audio frame level like this:
Copy code
from livekit.rtc import apm, AudioFrame

# Initialize APM with echo cancellation
audio_processor = apm.AudioProcessingModule(
    echo_cancellation=True,
    noise_suppression=True,
    high_pass_filter=True,
    auto_gain_control=True
)

# Process reverse stream (far-end audio played to user)
audio_processor.process_reverse_stream(render_frame)

# Process forward stream (mic input from user)
audio_processor.process_stream(capture_frame)

# Set delay in milliseconds for accurate echo cancellation
audio_processor.set_stream_delay_ms(delay_ms)
Just a quick tip: make sure your audio frames are exactly 10ms long and that you process the reverse stream before the forward stream for the best results. Recommendation: For most situations, I’d recommend going with the enhanced noise cancellation using
inputOptions
because it’s simpler and delivers great performance. If you have any questions or need a hand with this, please don’t hesitate to reach out—I’m here to help! Sources: Noise & echo cancellation | LiveKit docs | examples/src/basic_agent.ts | livekit-rtc/tests/test_apm.py | stream_audio.py | Enhanced noise cancellation | LiveKit docs
b
where to put AudioProcessingModule in agent session.?
t
Thanks for reaching out! I wanted to share that the
AudioProcessingModule
isn’t directly built to plug into an
AgentSession
in LiveKit. The
AgentSession
actually handles the whole audio processing pipeline for you, including STT, TTS, and VAD, and even supports noise cancellation through
inputOptions
like
BackgroundVoiceCancellation
. If you’re looking for more advanced audio processing with
AudioProcessingModule
, it would need to be applied to the raw audio data outside of the
AgentSession
pipeline. I’m here to help if you’d like some guidance on how to use
AudioProcessingModule
on its own or explore other options that might fit your needs better. Just let me know! Sources: webrtc-sys/include/livekit/apm.h | examples/src/basic_agent.ts | livekit-agents/livekit/agents/voice/agent_session.py | Node.js Agents Architecture | livekit/livekit_composite | DeepWiki | krisp-minimal/README.md
b
Is there any way to intercept the audio and after processing it using AudioProcessingModule, send back to AgentSession ?
t
Absolutely, you can intercept audio, process it using
AudioProcessingModule
, and then send it back to the
AgentSession
. Let me walk you through two friendly approaches: 1. Using `AudioProcessingModule`: You can process audio frames before sending them to the session like this:
Copy code
from livekit.rtc import AudioProcessingModule

# Initialize APM with echo cancellation
audio_processor = AudioProcessingModule(
    echo_cancellation=True,
    noise_suppression=True,
    high_pass_filter=True,
    auto_gain_control=True
)

# Process audio frames
processed_frame = audio_processor.process_stream(input_frame)
Just a quick tip: make sure your audio frames are exactly 10ms long. Also, if you want full-duplex echo cancellation, remember to use
process_reverse_stream()
for the far-end audio. 2. Overriding `realtime_audio_output_node`: This approach lets you intercept and process audio output directly in the agent pipeline:
Copy code
async def realtime_audio_output_node(self, audio, model_settings):
    async for frame in audio:
        processed_frame = self._process_audio(frame)
        yield processed_frame

def _process_audio(self, frame):
    # Apply custom processing to the frame
    return frame
This way, you can apply any custom transformations you need before sending the audio back to the session. If you’d like, I’m here to help with more details or examples—just let me know! Sources: livekit-rtc/livekit/rtc/apm.py | examples/voice_agents/speedup_output_audio.py | realtime/openai-realtime-pitch-shift.py | Capturing the First Few Seconds of a Call for Processing
b
provide me a full working code that implements AudioProcessingModule in AgentSession
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!