quick-cartoon-22764
03/10/2026, 5:42 PMtall-airline-16756
03/10/2026, 5:42 PMtall-airline-16756
03/10/2026, 5:43 PMfrom livekit.agents import AgentSession, room_io
from livekit.plugins import noise_cancellation, silero
session = AgentSession(
vad=silero.VAD.load(),
turn_detection="vad",
stt=..., # your STT provider
llm=...,
tts=...,
)
await session.start(
room=ctx.room,
agent=my_agent,
room_options=room_io.RoomOptions(
audio_input=room_io.AudioInputOptions(
noise_cancellation=noise_cancellation.BVC(), # enable BVC
),
),
)
If your participant is a SIP/telephony endpoint, use BVCTelephony() instead:
filter = (
noise_cancellation.BVCTelephony()
if participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP
else noise_cancellation.BVC()
)
Notes and best practices
• BVC runs on the agent side in real time and adds minimal latency.
• Don’t chain multiple noise-cancellation solutions (e.g., BVC plus another NC service) — that can degrade audio quality.
• BVC will suppress background noise and other non-primary voices while the primary user speaks.
To tailor this for your setup
• Which platform/type are you integrating with? (WebRTC/browser, SIP/telephony trunk, iOS/Android client, or a server-side participant?)
• Are you already using a specific VAD or STT provider, or would you like recommendations?
• Do you need code for a different language or framework (JS, iOS Swift, etc.)?
Tell me the platform and any existing components (VAD/STT/agent code) and I’ll provide a concrete integration snippet and any platform-specific notes.
Sources: Choosing the right noise cancellation for your agent | LiveKitquick-cartoon-22764
03/10/2026, 7:31 PMtall-airline-16756
03/10/2026, 7:31 PMquick-cartoon-22764
03/10/2026, 7:32 PMtall-airline-16756
03/10/2026, 7:32 PMuser_state_changed and call background_audio.stop() when the user starts speaking; call background_audio.play() or background_audio.start() when they stop if you want it to resume.
from livekit.agents import AgentSession, BackgroundAudioPlayer
from livekit.plugins import silero
import asyncio
# Replace with your actual agent and room context usage
async def entrypoint(ctx, my_agent):
# Load a VAD for reliable turn detection (optional but recommended)
vad = silero.VAD.load()
session = AgentSession(
vad=vad,
turn_detection="vad", # use VAD for turn detection
# stt=..., llm=..., tts=... # your other agent components
)
# Create the background audio player with an ambient file or stream
background_audio = BackgroundAudioPlayer(ambient_sound="path/to/ambient.mp3")
# Start the background audio and attach it to the room/agent session
await background_audio.start(room=ctx.room, agent_session=session)
# React to user state changes
@session.on("user_state_changed")
def _on_user_state_changed(ev):
# ev.new_state values include "speaking", "listening", etc.
if ev.new_state == "speaking":
# Stop background audio immediately when user starts talking
try:
background_audio.stop()
except Exception:
# handle or log stop error if needed
pass
elif ev.new_state == "listening":
# Optionally resume background audio when user stops speaking
try:
background_audio.play()
except Exception:
# handle or log play error if needed
pass
# Start the agent session (blocks until session ends)
await session.start(agent=my_agent, room=ctx.room)
Key notes and recommendations
• Use VAD-based turn detection for the fastest, most reliable "user started speaking" signal.
• background_audio.stop() removes the background audio track so it doesn't interfere with the user's speech; background_audio.play() (or start() depending on your use) resumes it.
• Wrap the stop/play calls with error handling if your environment may return async errors.
• If you prefer muting instead of stopping (so state is preserved), you can implement a mute flag in your BackgroundAudioPlayer wrapper and call background_audio.set_volume(0) when speaking and restore volume after; the SDK methods above stop/play the track directly.
Would you like:
• A version that uses muting (volume control) instead of stop/play?
• A full runnable example showing agent setup (stt/llm/tts) and room context?