From bc781b2769d44bf524f1b8cf081bb17134ecffa9 Mon Sep 17 00:00:00 2001 From: maltoze Date: Fri, 21 Apr 2023 17:39:28 +0800 Subject: [PATCH 1/2] Refactor header length retrieval to utilize more appropriate method --- src/edge_tts/communicate.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/edge_tts/communicate.py b/src/edge_tts/communicate.py index 7d1561c..98aca84 100644 --- a/src/edge_tts/communicate.py +++ b/src/edge_tts/communicate.py @@ -5,6 +5,7 @@ Communicate package. import json import re +import struct import time import uuid from contextlib import nullcontext @@ -414,12 +415,13 @@ class Communicate: "We received a binary message, but we are not expecting one." ) - yield { - "type": "audio", - "data": received.data[ - received.data.find(b"Path:audio\r\n") + 12 : - ], - } + # See: https://github.com/microsoft/cognitive-services-speech-sdk-js/blob/d071d11d1e9f34d6f79d0ab6114c90eecb02ba1f/src/common.speech/WebsocketMessageFormatter.ts#L46 + header_length = struct.unpack(">H", received.data[:2])[0] + if len(received.data) > header_length + 2: + yield { + "type": "audio", + "data": received.data[header_length + 2 :], + } audio_was_received = True elif received.type == aiohttp.WSMsgType.ERROR: raise WebSocketError( From 6e733a306b898952b6d2fb6b07d4563f6fcd8932 Mon Sep 17 00:00:00 2001 From: rany2 Date: Fri, 21 Apr 2023 14:20:01 +0300 Subject: [PATCH 2/2] Add some checks to ensure that it contains header length and drop struct --- src/edge_tts/communicate.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/edge_tts/communicate.py b/src/edge_tts/communicate.py index 98aca84..928b3be 100644 --- a/src/edge_tts/communicate.py +++ b/src/edge_tts/communicate.py @@ -5,7 +5,6 @@ Communicate package. import json import re -import struct import time import uuid from contextlib import nullcontext @@ -415,13 +414,22 @@ class Communicate: "We received a binary message, but we are not expecting one." ) + if len(received.data) < 2: + raise UnexpectedResponse( + "We received a binary message, but it is missing the header length." + ) + # See: https://github.com/microsoft/cognitive-services-speech-sdk-js/blob/d071d11d1e9f34d6f79d0ab6114c90eecb02ba1f/src/common.speech/WebsocketMessageFormatter.ts#L46 - header_length = struct.unpack(">H", received.data[:2])[0] - if len(received.data) > header_length + 2: - yield { - "type": "audio", - "data": received.data[header_length + 2 :], - } + header_length = int.from_bytes(received.data[:2], "big") + if len(received.data) < header_length + 2: + raise UnexpectedResponse( + "We received a binary message, but it is missing the audio data." + ) + + yield { + "type": "audio", + "data": received.data[header_length + 2 :], + } audio_was_received = True elif received.type == aiohttp.WSMsgType.ERROR: raise WebSocketError(