-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
79 lines (65 loc) · 2.25 KB
/
__main__.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
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
import logging
import time
import numpy as np
import sounddevice as sd
from silero_tts_processor import SileroTTSProcessor
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler("tts_server.log", encoding="utf-8"),
],
)
logger = logging.getLogger(__name__)
def play_audio(audio: np.ndarray, sample_rate: int):
"""
Play generated audio with timeout and error handling.
:param audio: Audio numpy array
:param sample_rate: Sample rate
"""
try:
sd.play(audio, sample_rate)
sd.wait()
except sd.CallbackStop:
logger.warning("Audio playback interrupted")
except Exception as e:
logger.error(f"Audio playback failed: {e}")
raise
def main():
example_text = """
<speak>
<p>
В недрах тундры выдры в гетрах тырят в ведрах +ядра к+едров!
</p>
<p>
<prosody rate="slow">Сейчас я говорю медленно, а могу</prosody>
<prosody rate="x-slow">ещё медленнее</prosody>.
<break time="500ms"/>
<prosody pitch="x-high">ещё я могу говорить тоном выше</prosody>
<break time="500ms"/>
<prosody pitch="x-low">и низким тоном</prosody>
<break time="500ms"/>
<prosody rate="x-fast" pitch="x-low">и очень быстро тоже могу</prosody>
</p>
</speak>
"""
try:
tts_processor = SileroTTSProcessor(
language_id="ru",
model_id="v4_ru",
output_dir="./tts_outputs",
)
audio = tts_processor.generate_speech(
example_text,
speaker_id="xenia",
enhance_noise=True,
output_filename=time.strftime("%Y-%m-%d_%H-%M-%S") + ".wav",
)
play_audio(audio, tts_processor.sample_rate)
except Exception as e:
logging.error(f"TTS processing failed: {e}")
print(f"Error: {e}")
if __name__ == "__main__":
main()