-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathedge.py
51 lines (42 loc) · 1.74 KB
/
edge.py
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
"""Edge TTS provider implementation."""
import edge_tts
import os
import tempfile
from typing import List
from ..base import TTSProvider
class EdgeTTS(TTSProvider):
def __init__(self, api_key: str = None, model: str = None):
"""
Initialize Edge TTS provider.
Args:
api_key (str): Not used for Edge TTS
model (str): Model name to use
"""
self.model = model or "default" # Edge TTS doesn't use models, but we set it for consistency
def generate_audio(self, text: str, voice: str, model: str, voice2: str = None) -> bytes:
"""Generate audio using Edge TTS."""
import nest_asyncio
import asyncio
# Apply nest_asyncio to allow nested event loops
nest_asyncio.apply()
async def _generate():
communicate = edge_tts.Communicate(text, voice)
# Create a temporary file with proper context management
with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as tmp_file:
temp_path = tmp_file.name
try:
# Save audio to temporary file
await communicate.save(temp_path)
# Read the audio data
with open(temp_path, 'rb') as f:
return f.read()
finally:
# Clean up temporary file
if os.path.exists(temp_path):
os.remove(temp_path)
# Use nest_asyncio to handle nested event loops
loop = asyncio.get_event_loop()
return loop.run_until_complete(_generate())
def get_supported_tags(self) -> List[str]:
"""Get supported SSML tags."""
return self.COMMON_SSML_TAGS