Is it possible to include ambient background noise...
# ask-ai
r
Is it possible to include ambient background noise merged in with the tts output?
t
Thanks for reaching out, Gary. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Yes, it is possible to include ambient background noise merged with TTS output in LiveKit. You can use the
BackgroundAudioPlayer
class to achieve this. This allows you to add realism by playing ambient background audio alongside the synthesized speech. Here is an example of how to implement it:
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip

async def entrypoint(ctx: agents.JobContext):
    await ctx.connect()

    session = AgentSession(
        # ... your stt, llm, tts, vad, turn_detection, etc.
    )

    await session.start(
        room=ctx.room,
        # ... agent, etc.
    )
    
    background_audio = BackgroundAudioPlayer(
        # play office ambience sound looping in the background
        ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
        # play keyboard typing sound when the agent is thinking
        thinking_sound=[
            AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
            AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
        ],
    )
    
    await background_audio.start(room=ctx.room, agent_session=session)
The
BackgroundAudioPlayer
class supports two types of audio: • Ambient sound: A looping audio file that plays continuously in the background • Thinking sound: Audio that plays while the agent is processing/thinking You can adjust the volume of these sounds using the
volume
parameter in the
AudioConfig
object, with values between 0 and 1.0. Sources: Agent speech and audio | LiveKit Docs | GitHub::livekit::agents | examples/voice_agents/background_audio.py