Skip to content

Commit

Permalink
Fix difference in command request from Edge
Browse files Browse the repository at this point in the history
"sentenceBoundaryEnabled" and "wordBoundaryEnabled" is sent as a
string by Microsoft Edge but we are sending it as a boolean.

As there is no difference in functionality (both boolean and string
are treated properly), we try to pretend we are Microsoft Edge as
much as possible.

Signed-off-by: rany <[email protected]>
  • Loading branch information
rany2 committed Nov 22, 2024
1 parent 4f5d79e commit 4846483
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/edge_tts/communicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,32 +319,19 @@ def __parse_metadata(self, data: bytes) -> TTSChunk:

async def __stream(self) -> AsyncGenerator[TTSChunk, None]:
async def send_command_request() -> None:
"""Sends the request to the service."""

# Prepare the request to be sent to the service.
#
# Note sentenceBoundaryEnabled and wordBoundaryEnabled are actually supposed
# to be booleans, but Edge Browser seems to send them as strings.
#
# This is a bug in Edge as Azure Cognitive Services actually sends them as
# bool and not string. For now I will send them as bool unless it causes
# any problems.
#
# Also pay close attention to double { } in request (escape for f-string).
"""Sends the command request to the service."""
await websocket.send_str(
f"X-Timestamp:{date_to_string()}\r\n"
"Content-Type:application/json; charset=utf-8\r\n"
"Path:speech.config\r\n\r\n"
'{"context":{"synthesis":{"audio":{"metadataoptions":{'
'"sentenceBoundaryEnabled":false,"wordBoundaryEnabled":true},'
'"sentenceBoundaryEnabled":"false","wordBoundaryEnabled":"true"},'
'"outputFormat":"audio-24khz-48kbitrate-mono-mp3"'
"}}}}\r\n"
)

async def send_ssml_request() -> None:
"""Sends the SSML request to the service."""

# Send the request to the service.
await websocket.send_str(
ssml_headers_plus_data(
connect_id(),
Expand Down Expand Up @@ -375,10 +362,8 @@ async def send_ssml_request() -> None:
headers=WSS_HEADERS,
ssl=ssl_ctx,
) as websocket:
# Send the request to the service.
await send_command_request()

# Send the SSML request to the service.
await send_ssml_request()

async for received in websocket:
Expand Down

0 comments on commit 4846483

Please sign in to comment.