From 6d2bc68933332cfdccd8bb8fbfb213cf54fd4446 Mon Sep 17 00:00:00 2001 From: Vuizur Date: Wed, 4 Jan 2023 20:31:57 +0100 Subject: [PATCH 1/3] Add voices manager to find voices --- src/edge_tts/list_voices.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/edge_tts/list_voices.py b/src/edge_tts/list_voices.py index 73bc6dc..f1d50a3 100644 --- a/src/edge_tts/list_voices.py +++ b/src/edge_tts/list_voices.py @@ -40,3 +40,29 @@ async def list_voices(proxy=None): ) as url: data = json.loads(await url.text()) return data + + +class VoicesManager: + """ + A class to find the correct voice based on their attributes. + """ + + @classmethod + async def create(cls): + self = VoicesManager() + self.voices = await list_voices() + self.voices = [ + {**voice, **{"Language": voice["Locale"].split("-")[0]}} + for voice in self.voices + ] + return self + + def find(self, **kwargs): + """ + Finds all matching voices based on the provided attributes. + """ + + matching_voices = [ + voice for voice in self.voices if kwargs.items() <= voice.items() + ] + return matching_voices From 0dc5154b06c8f2ad6eb853d62b90f65eff491b9f Mon Sep 17 00:00:00 2001 From: Vuizur Date: Wed, 4 Jan 2023 20:37:00 +0100 Subject: [PATCH 2/3] Add shortcut to VoicesManager --- src/edge_tts/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/edge_tts/__init__.py b/src/edge_tts/__init__.py index 3880896..24b4e77 100644 --- a/src/edge_tts/__init__.py +++ b/src/edge_tts/__init__.py @@ -3,5 +3,5 @@ __init__ for edge_tts """ from .communicate import Communicate -from .list_voices import list_voices -from .submaker import SubMaker +from .list_voices import list_voices, VoicesManager +from .submaker import SubMaker \ No newline at end of file From 4b529c8f2b43816cbaebbacc289aeed9c3f3f7a1 Mon Sep 17 00:00:00 2001 From: Vuizur Date: Wed, 4 Jan 2023 20:51:30 +0100 Subject: [PATCH 3/3] Add example for getting a voice for language --- README.md | 1 + examples/dynamic_voice_selection.py | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 examples/dynamic_voice_selection.py diff --git a/README.md b/README.md index d44f5c0..5c9dfea 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ In addition, it is required to use `--pitch=-10Hz` instead of `--pitch -10Hz` ot It is possible to use the `edge-tts` module directly from Python. For a list of example applications: * https://github.com/rany2/edge-tts/blob/master/examples/example.py +* https://github.com/rany2/edge-tts/blob/master/examples/dynamic_voice_selection.py * https://github.com/rany2/edge-tts/blob/master/src/edge_tts/util.py * https://github.com/rany2/edge-srt-to-speech/blob/master/src/edge_srt_to_speech/__main__.py * https://github.com/hasscc/hass-edge-tts/blob/main/custom_components/edge_tts/tts.py diff --git a/examples/dynamic_voice_selection.py b/examples/dynamic_voice_selection.py new file mode 100644 index 0000000..fb85a1c --- /dev/null +++ b/examples/dynamic_voice_selection.py @@ -0,0 +1,26 @@ +import asyncio +import edge_tts +from edge_tts import VoicesManager +import random + +async def main(): + """ + Main function + """ + voices = await VoicesManager.create() + voice = voices.find(Gender="Male", Language="es") + # Also supports Locales + # voice = voices.find(Gender="Female", Locale="es-AR") + VOICE = random.choice(voice)["ShortName"] + TEXT = "Hoy es un buen día." + OUTPUT_FILE = "spanish.mp3" + + communicate = edge_tts.Communicate() + + with open(OUTPUT_FILE, "wb") as f: + async for i in communicate.run(TEXT, voice=VOICE): + if i[2] is not None: + f.write(i[2]) + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(main())