[Python] Can anyone help me speeding my pygame and edge-tts code

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    So this is my code

    import asyncio
    import edge_tts
    import os
    import pygame

    def speak(audio):
    VOICES = ['en-AU-NatashaNeural', 'en-AU-WilliumNeural', 'en-CA-ClaraNeural', 'en-CA-LiamNeural', 'en-GB-LibbyNeural', 'en-GB-MaisieNeural', 'en-US-AndrewMultilingualNeural']
    COMMAND = f'edge-tts --voice "{VOICES[6]}" --text "{audio}" --write-media "audio.mp3"'
    os.system(COMMAND)
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load("audio.mp3")

    try:
    pygame.mixer.music.play()

    while pygame.mixer.music.get_busy():
    pygame.time.Clock().tick(10)

    except Exception as e:
    print(e)

    finally:
    pygame.mixer.music.stop()
    pygame.mixer.quit()


    Now when I use this, it makes a audio.mp3 file and I use it in my program. but when I use multiples together like this:

    Speak("Hello")
    Speak("How are you?")


    It takes like 2 to 3 secs between this 2 line to run because of pygame. Now can you make it faster for me and give it too me. I don't know how can I do that.

    Continue reading...

Compartilhe esta Página