|
|
@ -89,36 +89,30 @@ async def websocket_communication():
|
|
|
|
# Send data from the queue to the server
|
|
|
|
# Send data from the queue to the server
|
|
|
|
while not data_queue.empty():
|
|
|
|
while not data_queue.empty():
|
|
|
|
data = data_queue.get_nowait()
|
|
|
|
data = data_queue.get_nowait()
|
|
|
|
print(f"Sending data to the server: {data}")
|
|
|
|
|
|
|
|
await websocket.send(json.dumps(data))
|
|
|
|
await websocket.send(json.dumps(data))
|
|
|
|
|
|
|
|
|
|
|
|
# Listen for incoming messages from the server
|
|
|
|
# Listen for incoming messages from the server
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
chunk = await websocket.recv()
|
|
|
|
chunk = await asyncio.wait_for(websocket.recv(), timeout=1.0)
|
|
|
|
print(f"Received from server: {str(chunk)[:100]}")
|
|
|
|
print(f"Received from server: {str(chunk)[:100]}")
|
|
|
|
|
|
|
|
|
|
|
|
if chunk["type"] == "audio":
|
|
|
|
if chunk["type"] == "audio":
|
|
|
|
print("Received audio data from server.")
|
|
|
|
|
|
|
|
if "start" in chunk:
|
|
|
|
if "start" in chunk:
|
|
|
|
print("Start of audio data received.")
|
|
|
|
|
|
|
|
audio_chunks = bytearray()
|
|
|
|
audio_chunks = bytearray()
|
|
|
|
if "content" in chunk:
|
|
|
|
if "content" in chunk:
|
|
|
|
print("Audio content received.")
|
|
|
|
|
|
|
|
audio_chunks.extend(bytes(ast.literal_eval(chunk["content"])))
|
|
|
|
audio_chunks.extend(bytes(ast.literal_eval(chunk["content"])))
|
|
|
|
if "end" in chunk:
|
|
|
|
if "end" in chunk:
|
|
|
|
print("End of audio data received.")
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".mp3") as f:
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".mp3") as f:
|
|
|
|
f.write(audio_chunks)
|
|
|
|
f.write(audio_chunks)
|
|
|
|
f.seek(0)
|
|
|
|
f.seek(0)
|
|
|
|
seg = pydub.AudioSegment.from_mp3(f.name)
|
|
|
|
seg = pydub.AudioSegment.from_mp3(f.name)
|
|
|
|
print("Playing received audio.")
|
|
|
|
|
|
|
|
pydub.playback.play(seg)
|
|
|
|
pydub.playback.play(seg)
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
print(f"Error receiving data: {e}")
|
|
|
|
# No message received within timeout period
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
print("Sleeping for 0.05 seconds.")
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Websocket not ready, retrying... ({e})")
|
|
|
|
print(f"Websocket not ready, retrying... ({e})")
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
await asyncio.sleep(1)
|
|
|
|