if i have a wav file , how would i use session.say...
# ask-ai
f
if i have a wav file , how would i use session.say with audio
t
Thanks for reaching out, Huy. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To use
session.say
with a WAV file, you can pass an asynchronous audio generator as the
audio
parameter. This is a great way to play pre-recorded audio from your file while optionally adding some text to the transcript, making everything clear and seamless. Here’s a simple example to guide you:
Copy code
import wave
from livekit import rtc

# Read the WAV file
audio_path = "path/to/your/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())

# Create AudioFrame
audio_frame = rtc.AudioFrame(
    data=frames,
    sample_rate=sample_rate,
    num_channels=num_channels,
    samples_per_channel=len(frames) // num_channels // 2  # assuming 16-bit audio
)

# Define async generator
async def audio_generator():
    yield audio_frame

# Play the audio using session.say
await self.session.say("Playing audio file", audio=audio_generator())
Just to clarify: • The
text
parameter (like "Playing audio file") helps add a transcript entry. • The
audio
parameter actually plays the WAV file content instead of generating speech. If you prefer not to add anything to the transcript, you can simply leave out the
text
parameter. I hope this helps you get started smoothly! Sources: basics/exit_message.py | Agent speech and audio | LiveKit docs