-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtts_module.py
More file actions
52 lines (40 loc) · 1.37 KB
/
tts_module.py
File metadata and controls
52 lines (40 loc) · 1.37 KB
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 asyncio
import edge_tts
import io
import pygame
import utils
try:
pygame.mixer.init()
print("--- [TTS] Audio Engine Pre-Loaded ---")
except pygame.error:
print("--- [TTS] Can't pre-load the audio engine. ---")
def list_voices():
voice_options = []
for i, data in utils.TTS_VOICES.items():
voice_options.append({
"id": i,
"name": data["name"]
})
return voice_options
async def speak(text: str, voice_id: int, volume: int = 100):
if voice_id not in utils.TTS_VOICES:
print(f"Error: Voice ID {voice_id} not found. Using 0. (default)")
voice_id = 0
if not text or not text.strip():
return
VOICE = utils.TTS_VOICES[voice_id]["code"]
try:
communicate = edge_tts.Communicate(text, VOICE)
audio_buffer = io.BytesIO()
async for chunk in communicate.stream():
if chunk["type"] == "audio":
audio_buffer.write(chunk["data"])
audio_buffer.seek(0)
vol_float = volume / 100.0
pygame.mixer.music.set_volume(vol_float)
pygame.mixer.music.load(audio_buffer)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
await asyncio.sleep(0.05)
except Exception as e:
print(f"EdgeTTS Error: {e}")