-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathplaying_audio.py
More file actions
84 lines (67 loc) · 2.44 KB
/
Copy pathplaying_audio.py
File metadata and controls
84 lines (67 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
---
title: Playing Audio
category: basics
tags: [audio, deepgram, openai, cartesia]
difficulty: beginner
description: Shows how to play audio from a file in an agent.
demonstrates:
- Playing audio from a file
---
"""
import logging
from pathlib import Path
import wave
from dotenv import load_dotenv
from livekit.agents import JobContext, JobProcess, AgentServer, cli, Agent, AgentSession, inference, RunContext, function_tool
from livekit.plugins import silero
from livekit import rtc
load_dotenv()
logger = logging.getLogger("playing-audio")
logger.setLevel(logging.INFO)
class AudioPlayerAgent(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""
You are a helpful assistant communicating through voice. Don't use any unpronouncable characters.
If asked to play audio, use the `play_audio_file` function.
"""
)
@function_tool
async def play_audio_file(self, context: RunContext):
"""Play a local audio file"""
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."
async def on_enter(self):
self.session.generate_reply()
server = AgentServer()
def prewarm(proc: JobProcess):
proc.userdata["vad"] = silero.VAD.load()
server.setup_fnc = prewarm
@server.rtc_session()
async def entrypoint(ctx: JobContext):
ctx.log_context_fields = {"room": ctx.room.name}
session = AgentSession(
stt=inference.STT(model="deepgram/nova-3-general"),
llm=inference.LLM(model="openai/gpt-5-mini"),
tts=inference.TTS(model="cartesia/sonic-3", voice="9626c31c-bec5-4cca-baa8-f8ba9e84c8bc"),
vad=ctx.proc.userdata["vad"],
preemptive_generation=True,
)
await session.start(agent=AudioPlayerAgent(), room=ctx.room)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)