how can I get the audio frames of a room?
# ask-ai
a
how can I get the audio frames of a room?
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 happy to help you access audio frames from a room in LiveKit! You can use the
AudioStream
class to get raw PCM audio data in real-time from participant audio tracks. Let me walk you through the process step-by-step: 1. Connect to a LiveKit Room: Start by initializing a
Room
instance and connecting with a valid access token. 2. Subscribe to Audio Tracks: Keep an eye on the
track_subscribed
event handler—it lets you know when an audio track is available. Make sure
auto_subscribe=True
is set in
RoomOptions
, or you can subscribe to tracks manually. 3. Create an AudioStream: Wrap the subscribed audio track using
rtc.AudioStream(track)
. This gives you an asynchronous iterator that provides
AudioFrame
objects containing the raw PCM data. 4. Process Audio Frames: You can then iterate over the
AudioStream
asynchronously to access each
AudioFrame
. This data is great for transcription, playback, or any analysis you want to do. If you're looking to extract audio on the server side, Universal Egress is a fantastic option to export raw PCM audio directly to a WebSocket endpoint. Please feel free to reach out if you'd like more details or examples—I’m here to help! Sources: Processing raw media tracks | LiveKit Docs | terminal-client/client.py | packages/livekit-rtc/src/audio_stream.ts | stream_audio.py | Universal Egress
a
give me example with an agent
t
I'm happy to share a simple example of how you can access audio frames with an agent in LiveKit:
Copy code
from livekit import rtc

@room.on("track_subscribed")
async def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
    if track.kind == rtc.TrackKind.KIND_AUDIO:
        # Create an AudioStream from the subscribed track
        audio_stream = rtc.AudioStream(track)

        # Process audio frames in real-time
        async for frame in audio_stream:
            # Access raw audio data
            audio_data = frame.data  # PCM data as bytes
            print(f"Received audio frame: {len(audio_data)} bytes, {frame.sample_rate} Hz, {frame.num_channels} channels")
This example listens for when a participant’s audio track is subscribed, then creates an
AudioStream
to process each audio frame asynchronously. You can take advantage of the raw PCM data for things like transcription, playback, or any analysis you might want to do. If you’d like, I’m here to help with more details or additional examples—just let me know! Sources: Capturing the First Few Seconds of a Call for Processing | Processing raw media tracks | LiveKit Docs | livekit-rtc/livekit/rtc/audio_stream.py | realtime-agents/openai-realtime-pitch-shift.py | examples/voice_agents/error_callback.py
a
Give an end to end example with an Agent
t
Here is an end-to-end example of accessing audio frames with an agent in LiveKit:
Copy code
from livekit import rtc
from livekit.agents import combine_audio_frames

async def entrypoint(ctx):
    # Connect to the room and wait for a participant
    await ctx.connect(auto_subscribe=rtc.AutoSubscribe.AUDIO_ONLY)
    participant = await ctx.wait_for_participant()

    # Create an AudioStream from the participant's microphone
    stream = rtc.AudioStream.from_participant(
        participant=participant,
        track_source=rtc.TrackSource.SOURCE_MICROPHONE,
    )

    # Buffer audio frames for 7 seconds
    buffer = []
    async for audio_event in stream:
        frame = audio_event.frame
        buffer.append(frame)

        if frame.timestamp > 7_000:  # Stop after ~7 seconds
            break

    # Combine frames and convert to WAV
    combined = combine_audio_frames(buffer)
    wav_bytes = combined.to_wav_bytes()

    # Save the audio to a file
    with open("output.wav", "wb") as f:
        f.write(wav_bytes)

    print("Audio saved to output.wav")
This example gently walks you through how to: • Connect to a room and subscribe to audio tracks. • Capture and buffer audio frames for a specific duration. • Combine the frames and save them as a WAV file. If you have any questions or would like me to walk you through any part in more detail, please don’t hesitate to ask—I’m here to help! Sources: Capturing the First Few Seconds of a Call for Processing | examples/avatar_agents/bey/README.md | Camera & microphone | LiveKit Docs | realtime-agents/openai-realtime-pitch-shift.py