From f971dbe027afd77d02d77014232d7d7faddd3949 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Sat, 19 Oct 2024 16:26:49 -0700 Subject: [PATCH] examples(audio-recording): record audio into a file --- examples/chatbot-audio-recording/bot.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/examples/chatbot-audio-recording/bot.py b/examples/chatbot-audio-recording/bot.py index b04f6d695..2542a9e40 100644 --- a/examples/chatbot-audio-recording/bot.py +++ b/examples/chatbot-audio-recording/bot.py @@ -9,6 +9,8 @@ import os import sys import aiohttp +import datetime +import wave from dotenv import load_dotenv from loguru import logger from runner import configure @@ -30,6 +32,20 @@ logger.remove(0) logger.add(sys.stderr, level="DEBUG") +async def save_audio(audiobuffer): + if audiobuffer.has_audio(): + merged_audio = audiobuffer.merge_audio_buffers() + filename = f"conversation_recording{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.wav" + with wave.open(filename, "wb") as wf: + wf.setnchannels(2) + wf.setsampwidth(2) + wf.setframerate(audiobuffer._sample_rate) + wf.writeframes(merged_audio) + print(f"Merged audio saved to {filename}") + else: + print("No audio data to save") + + async def main(): async with aiohttp.ClientSession() as session: (room_url, token) = await configure(session) @@ -114,11 +130,7 @@ async def main(): async def on_participant_left(transport, participant, reason): print(f"Participant left: {participant}") await task.queue_frame(EndFrame()) - - @transport.event_handler("on_call_state_updated") - async def on_call_state_updated(transport, state): - if state == "left": - await task.queue_frame(EndFrame()) + await save_audio(audiobuffer) runner = PipelineRunner()