From 61dd64f469372dd162f4f5a3775cdba368fc4f1c Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Thu, 8 Feb 2024 13:00:16 -0800 Subject: [PATCH] `bash OS/01/start.sh` --- OS/01/assistant/conversations/user.json | 2 +- OS/01/assistant/tts.py | 8 ++++---- OS/01/user/user.py | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/OS/01/assistant/conversations/user.json b/OS/01/assistant/conversations/user.json index 02895a6..eb1136c 100644 --- a/OS/01/assistant/conversations/user.json +++ b/OS/01/assistant/conversations/user.json @@ -1 +1 @@ -[{"role": "user", "type": "message", "content": "Disgusting.\n"}] \ No newline at end of file +[{"role": "user", "type": "message", "content": "And it works really well.\n"}] \ No newline at end of file diff --git a/OS/01/assistant/tts.py b/OS/01/assistant/tts.py index 1dff048..14e1ba0 100644 --- a/OS/01/assistant/tts.py +++ b/OS/01/assistant/tts.py @@ -19,9 +19,9 @@ def tts(text): with tempfile.NamedTemporaryFile() as temp_file: response.stream_to_file(temp_file.name) - # audio = AudioSegment.from_file(temp_file.name, format="mp3") - # # Gradual fade in and out over 0.2 seconds - # audio = audio.fade_in(200).fade_out(200) - # play(audio) + audio = AudioSegment.from_file(temp_file.name, format="mp3") + # Gradual fade in and out over 0.2 seconds + audio = audio.fade_in(200).fade_out(200) + play(audio) return temp_file.read() diff --git a/OS/01/user/user.py b/OS/01/user/user.py index e2768af..2a50636 100644 --- a/OS/01/user/user.py +++ b/OS/01/user/user.py @@ -10,6 +10,9 @@ import websockets import queue import pydub import ast +from pydub import AudioSegment +from pydub.playback import play +import io # Configuration for Audio Recording CHUNK = 1024 # Record in chunks of 1024 samples @@ -109,7 +112,18 @@ async def websocket_communication(WS_URL): async for message in websocket: print(message) - await asyncio.sleep(1) + + if message["type"] == "audio" and "content" in message: + audio_bytes = bytes(ast.literal_eval(message["content"])) + + # Convert bytes to audio file + audio_file = io.BytesIO(audio_bytes) + audio = AudioSegment.from_mp3(audio_file) + + # Play the audio + play(audio) + + await asyncio.sleep(1) except: print("Connecting...") await asyncio.sleep(2)