able-jackal-84140
07/11/2025, 3:23 PMtall-airline-16756
07/11/2025, 3:23 PMtall-airline-16756
07/11/2025, 3:24 PMsession.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.
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