how to stream an audio within a tool execution?
# ask-ai
a
how to stream an audio within a tool execution?
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.
To stream audio within a tool execution, the recommended approach is to use the
session.say()
method with an audio generator that yields audio frames. Here is a step-by-step method using the LiveKit SDK: 1. Load your audio file and extract its frames, sample rate, and channel count. 2. Create an
rtc.AudioFrame
with the relevant audio properties and data. 3. Define an asynchronous generator that yields audio frames. 4. Use
await self.session.say(text, audio=audio_generator())
in your tool execution to stream the audio. This integrates with the agent’s voice pipeline and manages audio streaming automatically. For advanced scenarios such as custom sources or direct track publication, you can manage audio sources and tracks manually, but
session.say()
is the most straightforward for tool-based execution.
Copy code
from livekit import rtc
from livekit.agents.llm import function_tool
from livekit.agents.voice import RunContext
import wave
from pathlib import Path

@function_tool
async def play_audio_file(self, context: RunContext):
    audio_path = Path(__file__).parent / "audio.wav"
    with wave.open(str(audio_path), 'rb') as wav_file:
        num_channels = wav_file.getnchannels()
        sample_rate = wav_file.getframerate()
        frames = wav_file.readframes(wav_file.getnframes())
    audio_frame = rtc.AudioFrame(data=frames, sample_rate=sample_rate, num_channels=num_channels, samples_per_channel=wav_file.getnframes())
    async def audio_generator():
        yield audio_frame
    await self.session.say("Playing audio file", audio=audio_generator())
    return None, "I've played the audio file for you."
Let me know if you need details on a specific method or advanced audio streaming scenarios. Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | basics/playing_audio.py | stream_audio.py