Merge pull request #75 from maltoze/refactor-header-length

Refactor header length retrieval to utilize more appropriate method
This commit is contained in:
rany 2023-04-21 15:01:17 +03:00 committed by GitHub
commit b6baafa177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -414,11 +414,21 @@ class Communicate:
"We received a binary message, but we are not expecting one." "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 = 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 { yield {
"type": "audio", "type": "audio",
"data": received.data[ "data": received.data[header_length + 2 :],
received.data.find(b"Path:audio\r\n") + 12 :
],
} }
audio_was_received = True audio_was_received = True
elif received.type == aiohttp.WSMsgType.ERROR: elif received.type == aiohttp.WSMsgType.ERROR: