Skip to content

Commit

Permalink
examples(audio-recording): record audio into a file
Browse files Browse the repository at this point in the history
  • Loading branch information
aconchillo committed Oct 20, 2024
1 parent 3815e9d commit f971dbe
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions examples/chatbot-audio-recording/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import sys

import aiohttp
import datetime
import wave
from dotenv import load_dotenv
from loguru import logger
from runner import configure
Expand All @@ -30,6 +32,20 @@
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)
Expand Down Expand Up @@ -114,11 +130,7 @@ async def on_first_participant_joined(transport, participant):
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()

Expand Down

0 comments on commit f971dbe

Please sign in to comment.