use queue for output and put it in a thread, threads are back baby
This commit is contained in:
parent
c62138996f
commit
0de8bb3ed7
2 changed files with 16 additions and 2 deletions
|
|
@ -12,7 +12,7 @@ logging.basicConfig()
|
||||||
logging.root.setLevel(logging.INFO)
|
logging.root.setLevel(logging.INFO)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
microphone_input, speaker_output = create_microphone_input_and_speaker_output(use_first_available_device=True)
|
microphone_input, speaker_output = create_microphone_input_and_speaker_output(use_first_available_device=False)
|
||||||
|
|
||||||
conversation = Conversation(
|
conversation = Conversation(
|
||||||
input_device=microphone_input,
|
input_device=microphone_input,
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,8 @@ import asyncio
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
import threading
|
||||||
|
import queue
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|
@ -34,6 +36,8 @@ class Conversation:
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
self.receiver_ready = False
|
self.receiver_ready = False
|
||||||
self.active = True
|
self.active = True
|
||||||
|
self.output_loop = asyncio.new_event_loop()
|
||||||
|
self.output_audio_queue = queue.Queue()
|
||||||
|
|
||||||
async def wait_for_ready(self):
|
async def wait_for_ready(self):
|
||||||
while not self.receiver_ready:
|
while not self.receiver_ready:
|
||||||
|
|
@ -43,6 +47,14 @@ class Conversation:
|
||||||
def deactivate(self):
|
def deactivate(self):
|
||||||
self.active = False
|
self.active = False
|
||||||
|
|
||||||
|
def play_audio(self):
|
||||||
|
async def run():
|
||||||
|
while self.active:
|
||||||
|
audio = self.output_audio_queue.get()
|
||||||
|
await self.output_device.send_async(audio)
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
loop.run_until_complete(run())
|
||||||
|
|
||||||
async def start(self):
|
async def start(self):
|
||||||
async with websockets.connect(f"{VOCODE_WEBSOCKET_URL}?key={api_key}") as ws:
|
async with websockets.connect(f"{VOCODE_WEBSOCKET_URL}?key={api_key}") as ws:
|
||||||
async def sender(ws):
|
async def sender(ws):
|
||||||
|
|
@ -66,8 +78,10 @@ class Conversation:
|
||||||
self.receiver_ready = True
|
self.receiver_ready = True
|
||||||
async for msg in ws:
|
async for msg in ws:
|
||||||
audio_message = AudioMessage.parse_raw(msg)
|
audio_message = AudioMessage.parse_raw(msg)
|
||||||
await self.output_device.send_async(audio_message.get_bytes())
|
self.output_audio_queue.put_nowait(audio_message.get_bytes())
|
||||||
|
|
||||||
|
|
||||||
|
output_thread = threading.Thread(target=self.play_audio)
|
||||||
|
output_thread.start()
|
||||||
return await asyncio.gather(sender(ws), receiver(ws))
|
return await asyncio.gather(sender(ws), receiver(ws))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue