rany2 / edge-tts

Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key
https://pypi.org/project/edge-tts/
GNU General Public License v3.0
4.24k stars 447 forks source link

Use edge-tts without asyncio #127

Closed KonerDev closed 11 months ago

KonerDev commented 11 months ago

Can you use edge-tts without asyncio?

rany2 commented 11 months ago

You need asyncio but you could use it just fine if your application does not make use of async, for example:

import asyncio

import edge_tts as et

def et_save_to_file(text, *, voice="en-GB-SoniaNeural", file="test.mp3"):
    async def wrapper():
        c = et.Communicate(text, voice)
        await c.save(file)

    loop = asyncio.get_event_loop_policy().get_event_loop()
    try:
        loop.run_until_complete(wrapper())
    finally:
        loop.close()

if __name__ == "__main__":
    et_save_to_file("Hey, this is a test!")

This way you won't need to change your style at all and could use it as though it wasn't asyncio