hi can i add background sound only if latency is i...
# ask-ai
e
hi can i add background sound only if latency is increased to latency is increased.
t
Thanks for reaching out, Shehryar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can totally add background sound in LiveKit without worrying about adding latency. The
BackgroundAudioPlayer
class is perfect for playing ambient and thinking sounds during your session. Here’s a quick example of how you might set it up:
Copy code
background_audio = BackgroundAudioPlayer(
    ambient_sound=AudioConfig(BuiltinAudioClip.OFFICE_AMBIENCE, volume=0.8),
    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)
This setup usually won’t increase latency, but it’s a good idea to keep an eye on your system’s performance just to be safe. If you do notice any lag, you might want to try: • Turning down the volume of your background sounds • Using simpler audio files • Making sure your system has enough resources available If you want me to help you tweak anything or have more questions, just let me know—I’m here for you! Sources: examples/voice_agents/zapier_mcp_integration.py | Agent speech and audio | LiveKit Docs | Agent speech and audio | LiveKit Docs
e
i donot want the background sound everytime agents is think i want when agents long time or when tool call takes along time.
t
Hey there! If you want to have background sounds play only when the agent is in those longer "thinking" moments or during tool calls, you can set up the
BackgroundAudioPlayer
to do just that. It kicks in specifically when the agent enters the "thinking" state, which happens during those extended processing times. Here’s a quick example to get you started:
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip

background_audio = BackgroundAudioPlayer(
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)

async def entrypoint(ctx: JobContext):
    session = AgentSession(
        # Initialize your agent session with required components
    )
    await session.start(room=ctx.room, agent=agent)

    await background_audio.start(
        room=ctx.room,
        agent_session=session
    )
The
BackgroundAudioPlayer
will automatically notice when the agent is "thinking" and play those sounds you set up. If you have any questions or want to tweak it further, just let me know—I’m here to help!
e
this is same as before can you share the code to link background sound only when tool calling.
t
Hey there! If you want to have background sound play only during tool calls, you can set up the
BackgroundAudioPlayer
to kick in specifically when the agent is in the "thinking" state while executing tools. Here's a little example to get you started:
Copy code
from livekit.agents import BackgroundAudioPlayer, AudioConfig, BuiltinAudioClip

# Setting up the BackgroundAudioPlayer with some thinking sounds
background_audio = BackgroundAudioPlayer(
    thinking_sound=[
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING, volume=0.8),
        AudioConfig(BuiltinAudioClip.KEYBOARD_TYPING2, volume=0.7),
    ],
)

async def entrypoint(ctx: JobContext):
    session = AgentSession(
        # Just initialize your agent session with whatever you need
    )
    await session.start(room=ctx.room, agent=agent)

    # Now, start the BackgroundAudioPlayer
    await background_audio.start(
        room=ctx.room,
        agent_session=session
    )
This way, the background sound will only play when the agent’s in the "thinking" state — like during those tool calls. If you want to tweak it more or have any questions, just let me know. I’m here to help!
e
can i get the event for tool calls like agent version 0
t
If you want to catch events for tool calls in LiveKit, you can use the
FunctionToolsExecutedEvent
. Here’s a simple way to set it up:
Copy code
# Add an event listener to capture tool call events
session.on("function_tools_executed", function_tools_executed_event_handler)

# Implement the event handler
def function_tools_executed_event_handler(event: FunctionToolsExecutedEvent):
    # Access tool call details
    for function_call in event.function_calls:
        print(f"Tool Name: {function_call.name}")
        print(f"Arguments: {function_call.arguments}")
        print(f"Call ID: {function_call.call_id}")

    # Access tool call outputs
    for function_call_output in event.function_call_outputs:
        if function_call_output:
            print(f"Output: {function_call_output.output}")
The
FunctionToolsExecutedEvent
gives you all the info about tool executions—like the tool name, arguments, call ID, and outputs. It’s automatically fired during the agent’s tool execution, so you don’t have to do anything extra. Also, if you’re trying to recreate something from "agent version 0," could you share a bit more about what you’re aiming for? That way, I can help you better and make sure you get exactly what you need. Sources: livekit-agents/livekit/agents/voice/run_result.py
e
can event before the tools is executed.
t
Hey there! So, LiveKit doesn’t have a specific event to catch details before a tool runs. But no worries—you can use the
function_tools_executed
event to keep an eye on when a tool starts executing. This event kicks in right as the tool execution begins and gives you info like the tool’s name, its arguments, and the call ID. Here’s a quick example:
Copy code
# Listen for the function_tools_executed event
session.on("function_tools_executed", callback_function)

# Access function call details in the callback
def callback_function(event: FunctionToolsExecutedEvent):
    for call in event.function_calls:
        print(f"Tool Name: {call.name}")
        print(f"Arguments: {call.arguments}")
If you want to grab details before the tool actually starts, you might need to add some custom logic in your app to log or handle those details beforehand. Just give me a shout if you want a hand setting that up!
e
can you share the import to use this tool.
t
To get started with the
function_tools_executed
event in LiveKit, you'll want to import these:
Copy code
from livekit.agents import AgentSession
from livekit.agents.voice.events import FunctionToolsExecutedEvent
Here’s a simple example of how to set up the event listener:
Copy code
# Set up event listener
@session.on("function_tools_executed")
def on_function_tools_executed(event: FunctionToolsExecutedEvent):
    # Handle the event here
    print("Function tools executed:")
    for call, output in event.zipped():
        print(f"  - {call.name}: {output}")
If you have any questions or want me to walk you through anything else, just let me know! Sources: livekit-agents/livekit/agents/voice/generation.py | Events and error handling | LiveKit Docs