how to access the audio of a room when the agent i...
# ask-ai
a
how to access the audio of a room when the agent is dispatched.
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're looking into how audio works when an agent joins a room! When an agent is dispatched, they automatically receive audio from all participants in the room, thanks to the
RoomIO
system, which smoothly manages the media streams between the agent and everyone else. By default, the agent hears all participant audio streams, but if you want to change that, you can easily configure it using
RoomInputOptions
to decide which audio to receive. Likewise, if the agent needs to send audio back into the room, that’s handled through
RoomOutputOptions
. If you'd like, I’m here to help with more detailed steps or examples, especially if you're working on setting up audio input/output or integrating with avatar-based systems. Just let me know, and we’ll figure it out together! Sources: Building voice agents | LiveKit Docs | basics/playing_audio.py | livekit-plugins/livekit-plugins-tavus/livekit/plugins/tavus/avatar.py | Web and mobile frontends | LiveKit Docs | LiveKit Docs
a
Give me python example of how to access it
t
I'd love to share a Python example with you that shows how to access the audio of a room when the agent is dispatched. This should help you get started smoothly:
Copy code
from livekit.agents.voice.room_io import RoomInputOptions, RoomOutputOptions

# Configure audio input options
input_options = RoomInputOptions(
    audio_enabled=True,  # Enable audio input
    text_enabled=True,   # Enable text input (optional)
    audio_sample_rate=24000,
    audio_num_channels=1
)

# Configure audio output options
output_options = RoomOutputOptions(
    audio_enabled=True,  # Enable audio output
    transcription_enabled=True,
    audio_sample_rate=24000,
    audio_num_channels=1
)

# Start the agent session with the configured options
await session.start(
    agent=agent,
    room=ctx.room,
    room_input_options=input_options,
    room_output_options=output_options
)

# Example: Agent speaking in the room
await session.say("Hello, I can speak now!", allow_interruptions=False)
This example shows how you can set up the agent to both receive audio from the room and send audio back into it. If you have any questions or would like me to walk you through any part of this, just let me know—I’m here to help!
a
how can I intercept the audio and save it?
t
I'm happy to help you with intercepting and saving audio from a room in LiveKit! There are a couple of approaches you can take, depending on what you need: 1. Full-Session Recording Using Egress This approach lets you record the entire session and save it directly to cloud storage. Here's a handy example to get you started:
Copy code
from livekit import api

req = api.RoomCompositeEgressRequest(
    room_name="my-room",
    layout="speaker",
    preset=api.EncodingOptionsPreset.H264_720P_30,
    audio_only=False,
    segment_outputs=[api.SegmentedFileOutput(
        filename_prefix="my-output",
        playlist_name="my-playlist.m3u8",
        segment_duration=5,
        gcp=api.GCPUpload(
            credentials="<your-gcp-credentials-json>",
            bucket="your-bucket-name",
        ),
    )],
)

lkapi = api.LiveKitAPI()
res = await lkapi.egress.start_room_composite_egress(req)
This will capture all the audio and video in the room and save it to your chosen cloud destination like S3 or GCP. It's a great way to get a full recording effortlessly. 2. Real-Time Audio Interception If you're looking to process audio selectively or save shorter snippets, you can use
rtc.AudioStream
to access raw audio frames as they come in. Here's how you might do that:
Copy code
from livekit import rtc
from livekit.agents import AudioStream, combine_audio_frames

buffer = []

async def on_track_subscribed(track: rtc.Track, *_):
    if track.kind == rtc.TrackKind.KIND_AUDIO:
        stream = rtc.AudioStream(track)

        async for frame in stream:
            buffer.append(frame)
            # Stop after ~7 seconds
            if frame.timestamp > 7_000:
                break

        # Export as WAV bytes
        combined = combine_audio_frames(buffer)
        wav_bytes = combined.to_wav_bytes()
        # Save to file or send to another processor
This method is perfect if you want to handle audio in real-time or focus on specific parts. Feel free to ask if you'd like me to walk you through this in more detail or provide additional examples! Sources: docs/index.yaml | Session recording and transcripts | LiveKit Docs | Capturing the First Few Seconds of a Call for Processing | Processing raw media tracks | LiveKit Docs