Michael Hu
refactor tts
60bd17d
raw
history blame
1.78 kB
import logging
# Configure logging
logger = logging.getLogger(__name__)
# Import the factory pattern implementation
from utils.tts_factory import TTSFactory
# Import base classes
from utils.tts_base import TTSEngineBase, DummyTTSEngine
# Import engine-specific modules
from utils.tts_engines import (
get_available_engines,
create_engine,
KokoroTTSEngine,
KokoroSpaceTTSEngine,
DiaTTSEngine
)
# Import legacy functions for backward compatibility
from utils.tts_kokoro import generate_speech as kokoro_generate_speech
from utils.tts_kokoro_space import generate_speech as kokoro_space_generate_speech
from utils.tts_dia import generate_speech as dia_generate_speech
# Convenience function to get the best available TTS engine
def get_best_engine(lang_code: str = 'z') -> TTSEngineBase:
"""Get the best available TTS engine
Args:
lang_code (str): Language code for the engine
Returns:
TTSEngineBase: An instance of the best available TTS engine
"""
return TTSFactory.create_engine(None, lang_code)
# Legacy function for backward compatibility
def generate_speech(text: str, language: str = "z", voice: str = "af_heart", speed: float = 1.0) -> str:
"""Generate speech using the best available TTS engine
This is a legacy function maintained for backward compatibility.
New code should use the factory pattern implementation directly.
Args:
text (str): Input text to synthesize
language (str): Language code
voice (str): Voice ID to use
speed (float): Speech speed multiplier
Returns:
str: Path to the generated audio file
"""
engine = get_best_engine(language)
return engine.generate_speech(text, voice, speed)