File size: 6,619 Bytes
eeb0f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
Text-to-Speech Module
Handles text-to-speech conversion with Vietnamese language support
"""

import logging
import tempfile
import os
from pathlib import Path
from gtts import gTTS
import io
import base64

logger = logging.getLogger(__name__)

class VietnameseTTS:
    """Vietnamese Text-to-Speech class using gTTS"""
    
    def __init__(self, language='vi', slow=False):
        """
        Initialize Vietnamese TTS
        
        Args:
            language (str): Language code (default: 'vi' for Vietnamese)
            slow (bool): Whether to speak slowly (default: False)
        """
        self.language = language
        self.slow = slow
        
    def text_to_speech(self, text, output_path=None):
        """
        Convert text to speech and save as audio file
        
        Args:
            text (str): Text to convert to speech
            output_path (str, optional): Path to save audio file. If None, returns temp file path
            
        Returns:
            str: Path to the generated audio file
            
        Raises:
            Exception: If TTS conversion fails
        """
        try:
            if not text or not text.strip():
                raise ValueError("Text cannot be empty")
                
            # Create gTTS object
            tts = gTTS(text=text.strip(), lang=self.language, slow=self.slow)
            
            # If no output path specified, create temporary file
            if output_path is None:
                temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
                output_path = temp_file.name
                temp_file.close()
            
            # Save audio file
            tts.save(output_path)
            
            logger.info(f"TTS audio saved to: {output_path}")
            return output_path
            
        except Exception as e:
            logger.error(f"TTS conversion failed: {str(e)}")
            raise Exception(f"Không thể chuyển đổi văn bản thành giọng nói: {str(e)}")
    
    def text_to_speech_bytes(self, text):
        """
        Convert text to speech and return as bytes
        
        Args:
            text (str): Text to convert to speech
            
        Returns:
            bytes: Audio data as bytes
            
        Raises:
            Exception: If TTS conversion fails
        """
        try:
            if not text or not text.strip():
                raise ValueError("Text cannot be empty")
                
            # Create gTTS object
            tts = gTTS(text=text.strip(), lang=self.language, slow=self.slow)
            
            # Save to BytesIO buffer
            audio_buffer = io.BytesIO()
            tts.write_to_fp(audio_buffer)
            audio_buffer.seek(0)
            
            return audio_buffer.getvalue()
            
        except Exception as e:
            logger.error(f"TTS conversion to bytes failed: {str(e)}")
            raise Exception(f"Không thể chuyển đổi văn bản thành giọng nói: {str(e)}")
    
    def text_to_speech_base64(self, text):
        """
        Convert text to speech and return as base64 encoded string
        
        Args:
            text (str): Text to convert to speech
            
        Returns:
            str: Base64 encoded audio data
            
        Raises:
            Exception: If TTS conversion fails
        """
        try:
            audio_bytes = self.text_to_speech_bytes(text)
            return base64.b64encode(audio_bytes).decode('utf-8')
            
        except Exception as e:
            logger.error(f"TTS conversion to base64 failed: {str(e)}")
            raise Exception(f"Không thể chuyển đổi văn bản thành giọng nói: {str(e)}")


# Global TTS instance
_tts_instance = None

def get_tts_instance():
    """Get or create global TTS instance"""
    global _tts_instance
    if _tts_instance is None:
        _tts_instance = VietnameseTTS()
    return _tts_instance

def text_to_speech(text, output_path=None):
    """
    Convenience function to convert text to speech
    
    Args:
        text (str): Text to convert to speech
        output_path (str, optional): Path to save audio file
        
    Returns:
        str: Path to the generated audio file
    """
    tts = get_tts_instance()
    return tts.text_to_speech(text, output_path)

def text_to_speech_bytes(text):
    """
    Convenience function to convert text to speech bytes
    
    Args:
        text (str): Text to convert to speech
        
    Returns:
        bytes: Audio data as bytes
    """
    tts = get_tts_instance()
    return tts.text_to_speech_bytes(text)

def text_to_speech_base64(text):
    """
    Convenience function to convert text to speech base64
    
    Args:
        text (str): Text to convert to speech
        
    Returns:
        str: Base64 encoded audio data
    """
    tts = get_tts_instance()
    return tts.text_to_speech_base64(text)

def cleanup_temp_files(file_path):
    """
    Clean up temporary audio files
    
    Args:
        file_path (str): Path to the file to delete
    """
    try:
        if file_path and os.path.exists(file_path):
            os.unlink(file_path)
            logger.info(f"Cleaned up temp file: {file_path}")
    except Exception as e:
        logger.warning(f"Failed to cleanup temp file {file_path}: {str(e)}")

def is_vietnamese_text(text):
    """
    Check if text contains Vietnamese characters
    
    Args:
        text (str): Text to check
        
    Returns:
        bool: True if text contains Vietnamese characters
    """
    vietnamese_chars = set('àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ')
    vietnamese_chars.update('ÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸĐ')
    
    return any(char in vietnamese_chars for char in text.lower())

def get_supported_languages():
    """
    Get list of supported languages for TTS
    
    Returns:
        dict: Dictionary of language codes and names
    """
    return {
        'vi': 'Tiếng Việt',
        'en': 'English',
        'zh': '中文',
        'ja': '日本語',
        'ko': '한국어',
        'th': 'ไทย',
        'fr': 'Français',
        'de': 'Deutsch',
        'es': 'Español',
        'it': 'Italiano',
        'pt': 'Português',
        'ru': 'Русский'
    }