Files
risa/audio/tts.py
2026-09-21 01:55:42 -04:00

33 lines
795 B
Python

import asyncio
import os
import tempfile
import time
import edge_tts
import pygame
def speak(text):
async def generate_speech():
voice = "en-US-ChristopherNeural"
communicate = edge_tts.Communicate(text, voice)
await communicate.save(temp_path)
with tempfile.TemporaryDirectory() as tmpdirname:
temp_path = os.path.join(tmpdirname, "speech.mp3")
asyncio.run(generate_speech())
pygame.mixer.init()
pygame.mixer.music.load(temp_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
pygame.mixer.music.unload()
pygame.mixer.quit()
if __name__ == "__main__":
speak("Hello! I am now speaking using Microsoft Edge's natural text to speech engine.")