mirror of
https://github.com/rany2/edge-tts
synced 2024-11-22 01:45:02 +00:00
Add some checks to ensure that it contains header length and drop struct
This commit is contained in:
parent
bc781b2769
commit
6e733a306b
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user