File size: 2,507 Bytes
c72d839
9c8546d
3ed3b5a
c72d839
 
60bd17d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ed3b5a
60bd17d
 
9740afc
60bd17d
 
 
 
c72d839
e734196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60bd17d
 
 
3ed3b5a
60bd17d
 
3ed3b5a
60bd17d
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)

# Function to get a TTS engine instance (for backward compatibility)
def get_tts_engine(engine_type: str = None, lang_code: str = 'z') -> TTSEngineBase:
    """Get a TTS engine instance
    
    This function is maintained for backward compatibility with app.py.
    New code should use the factory pattern implementation directly.
    
    Args:
        engine_type (str, optional): Type of engine to create ('kokoro', 'kokoro_space', 'dia', 'dummy')
                                    If None, the best available engine will be used
        lang_code (str): Language code for the engine
        
    Returns:
        TTSEngineBase: An instance of a TTS engine
    """
    return TTSFactory.create_engine(engine_type, 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)