Add rate to Edge TTS

This commit is contained in:
SillyLossy
2023-06-10 15:37:37 +03:00
parent 3e349df526
commit bb0ae04976
3 changed files with 10 additions and 4 deletions

View File

@@ -419,7 +419,7 @@ WAV audio file.
`POST /api/edge-tts/generate` `POST /api/edge-tts/generate`
#### **Input** #### **Input**
``` ```
{ "text": "Text to narrate", "voice": "af-ZA-AdriNeural" } { "text": "Text to narrate", "voice": "af-ZA-AdriNeural", "rate": 0 }
``` ```
#### **Output** #### **Output**
MP3 audio file. MP3 audio file.

View File

@@ -676,10 +676,14 @@ def edge_tts_generate():
abort(400, '"text" is required') abort(400, '"text" is required')
if "voice" not in data or not isinstance(data["voice"], str): if "voice" not in data or not isinstance(data["voice"], str):
abort(400, '"voice" is required') abort(400, '"voice" is required')
if "rate" in data and isinstance(data['rate'], int):
rate = data['rate']
else:
rate = 0
# Remove asterisks # Remove asterisks
data["text"] = data["text"].replace("*", "") data["text"] = data["text"].replace("*", "")
try: try:
audio = edge.generate_audio(text=data["text"], voice=data["voice"]) audio = edge.generate_audio(text=data["text"], voice=data["voice"], rate=rate)
return Response(audio, mimetype="audio/mpeg") return Response(audio, mimetype="audio/mpeg")
except Exception as e: except Exception as e:
print(e) print(e)

View File

@@ -21,8 +21,10 @@ async def _async_generator_to_list(async_gen):
return result return result
def generate_audio(text: str, voice: str) -> bytes: def generate_audio(text: str, voice: str, rate: int) -> bytes:
audio = edge_tts.Communicate(text, voice) sign = '+' if rate > 0 else '-'
rate = f'{sign}{abs(rate)}%'
audio = edge_tts.Communicate(text=text, voice=voice, rate=rate)
chunks = asyncio.run(_async_generator_to_list(_iterate_chunks(audio))) chunks = asyncio.run(_async_generator_to_list(_iterate_chunks(audio)))
buffer = io.BytesIO() buffer = io.BytesIO()