File size: 4,461 Bytes
b179e87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pygame
import uuid
from pathlib import Path
import threading
import edge_tts
import asyncio
from gtts import gTTS
import shutil
import time

class AudioUtils:
    AVAILABLE_MODELS = {
        'EDGE': {
            'name': "es-MX-JorgeNeural",  # Voz en español mexicano
            'description': "Voz de Edge TTS",
            'type': 'cloud',
            'fallback': 'gTTS'
        },
        'VITS': {
            'name': "tts_models/es/css10/vits",
            'description': "Voz masculina clara y natural",
            'type': 'local',
            'fallback': 'gTTS'
        },
        'gTTS': {
            'name': "google_tts",
            'description': "Google Text-to-Speech",
            'type': 'cloud'
        }
    }

    def __init__(self, model_name='EDGE'):
        self.is_speaking = False
        self.should_stop = False
        self.temp_dir = Path("static/temp_audio")
        self.temp_dir.mkdir(parents=True, exist_ok=True)
        self.current_model = model_name
        self.play_lock = threading.Lock()
        self.init_audio()
        self.cleanup_old_files()

    def init_audio(self):
        try:
            pygame.init()
            pygame.mixer.init(frequency=44100, size=-16, channels=2, buffer=4096)
            return True
        except Exception as e:
            print(f"Error inicializando audio: {e}")
            return False

    async def generate_edge_tts(self, text, output_file):
        try:
            communicate = edge_tts.Communicate(text, self.AVAILABLE_MODELS['EDGE']['name'])
            await communicate.save(str(output_file))
            return True
        except Exception as e:
            print(f"Error con Edge TTS: {e}")
            return False

    def text_to_speech(self, text, return_file=False):
        if not text:
            return None

        try:
            filename = f"{uuid.uuid4()}"
            temp_file = self.temp_dir / filename

            print(f"Generando audio con modelo {self.current_model}")

            if self.current_model == 'EDGE':
                temp_file = temp_file.with_suffix('.mp3')
                # Ejecutar Edge TTS de manera asíncrona
                loop = asyncio.new_event_loop()
                asyncio.set_event_loop(loop)
                success = loop.run_until_complete(self.generate_edge_tts(text, temp_file))
                loop.close()
                
                if not success:
                    print("Fallback a gTTS")
                    temp_file = temp_file.with_suffix('.mp3')
                    tts = gTTS(text=text, lang='es', slow=False)
                    tts.save(str(temp_file))
            else:  # gTTS como fallback por defecto
                temp_file = temp_file.with_suffix('.mp3')
                tts = gTTS(text=text, lang='es', slow=False)
                tts.save(str(temp_file))

            if not temp_file.exists():
                raise Exception(f"El archivo no se generó: {temp_file}")

            print(f"Archivo generado exitosamente: {temp_file}")
            return temp_file.name if return_file else temp_file

        except Exception as e:
            print(f"Error crítico en text_to_speech: {e}")
            import traceback
            traceback.print_exc()
            return None

    def play_audio(self, file_path):
        try:
            with self.play_lock:
                if pygame.mixer.music.get_busy():
                    pygame.mixer.music.stop()
                
                pygame.mixer.music.load(str(file_path))
                pygame.mixer.music.play()
                
                while pygame.mixer.music.get_busy():
                    if self.should_stop:
                        pygame.mixer.music.stop()
                        self.should_stop = False
                        break
                    pygame.time.Clock().tick(10)
        except Exception as e:
            print(f"Error reproduciendo audio: {e}")

    def stop_audio(self):
        self.should_stop = True

    def cleanup_old_files(self, max_age_minutes=5):
        try:
            current_time = time.time()
            for file in self.temp_dir.glob("*"):
                if file.is_file():
                    file_age_minutes = (current_time - file.stat().st_mtime) / 60
                    if file_age_minutes > max_age_minutes:
                        file.unlink()
        except Exception as e:
            print(f"Error limpiando archivos temporales: {e}")