File size: 2,068 Bytes
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
import logging
from typing import Optional, List

# Configure logging
logger = logging.getLogger(__name__)

# Import the base class
from utils.tts_base import TTSEngineBase, DummyTTSEngine

class TTSFactory:
    """Factory class for creating TTS engines
    
    This class is responsible for creating the appropriate TTS engine based on
    availability and configuration.
    """
    
    @staticmethod
    def create_engine(engine_type: Optional[str] = None, lang_code: str = 'z') -> TTSEngineBase:
        """Create a TTS engine instance
        
        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
        """
        from utils.tts_engines import get_available_engines, create_engine
        
        # Get available engines
        available_engines = get_available_engines()
        logger.info(f"Available TTS engines: {available_engines}")
        
        # If engine_type is specified, try to create that specific engine
        if engine_type is not None:
            if engine_type in available_engines:
                logger.info(f"Creating requested engine: {engine_type}")
                return create_engine(engine_type, lang_code)
            else:
                logger.warning(f"Requested engine '{engine_type}' is not available")
        
        # Try to create the best available engine
        # Priority: kokoro > kokoro_space > dia > dummy
        for engine in ['kokoro', 'kokoro_space', 'dia']:
            if engine in available_engines:
                logger.info(f"Creating best available engine: {engine}")
                return create_engine(engine, lang_code)
        
        # Fall back to dummy engine
        logger.warning("No TTS engines available, falling back to dummy engine")
        return DummyTTSEngine(lang_code)