139_spanish-voice-trainer/core/asset_generator.py

28 lines
No EOL
1 KiB
Python

import os
import edge_tts
class AssetGenerator:
def __init__(self):
# Explicit Castilian (es-ES) neural models
self.voices = {
"male": "es-ES-AlvaroNeural",
"female": "es-ES-ElviraNeural"
}
async def generate_speech(self, text: str, output_path: str, gender: str = "male"):
"""
Synthesizes Spanish text into an MP3 file using the selected Castilian voice gender.
Default is set to male, but can be explicitly overwritten with 'female'.
"""
# Fallback to male if an invalid string is passed
voice_key = gender.lower() if gender.lower() in self.voices else "male"
selected_voice = self.voices[voice_key]
# Ensure target media folder exists locally
os.makedirs(os.path.dirname(output_path), exist_ok=True)
# Configure and execute the asynchronous stream
communicate = edge_tts.Communicate(text, selected_voice)
await communicate.save(output_path)
return output_path