-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworking_tts.py
More file actions
118 lines (100 loc) · 4.38 KB
/
Copy pathworking_tts.py
File metadata and controls
118 lines (100 loc) · 4.38 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
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
# -*- coding: utf-8 -*-
def working_tts(text:str):
'''```
Лучший модуль. Озвучивает текст.
- text = строка, любой длинны.
Если больше 1000 символов то будет разделение на список
из элементов по 1000 символов и будет озвучен каждый элемент списка
- можно выбрать голос озвучки
- Нажмите ESC чтобы закончить воспроизведение
>>> speaker = 'eugene'
# aidar, baya, kseniya, xenia, eugene, random
'''
# Импортируем необходимые библиотеки
import torch
import sounddevice as sd
import time
from silero import silero_stt, silero_tts, silero_te
from transliterate import translit
import keyboard
from modules.working_symbols_to_list import split_string
from working_numbers_to_words import numbers_to_wards
from modules.working_remove_space_in_numbers import remove_spaces_in_numbers
# переменная для остановки аудиозвучки
global stop_signal
stop_signal = False
global print_counter
print_counter = 0
def stop_tts(counter_current_part=0):
'''
Для останов озвучивания достаточно нажать ESC
'''
global stop_signal
global print_counter
if print_counter == 0 :
print_counter += 1
print('''\n Озвучивание началось \n Для остановки нажмите ESC\n''')
if counter_current_part != 0: print(f'Часть {counter_current_part} из {len(text)} ...')
while sd.get_stream().active:
if keyboard.is_pressed('esc'):
sd.stop()
stop_signal = True
break
time.sleep(0.1)
# Переводим цифры в текст
text = numbers_to_wards(text)
# Перевод текста в русскиий язык из английского
# Sam = Сэм
text = translit(text,'ru')
# удаление пробелов между цифрами
text = remove_spaces_in_numbers(text)
# Устанавливаем параметры для TTS
language = 'ru'
model_id = 'v3_1_ru'
sample_rate = 48000
speaker = 'eugene' # aidar, baya, kseniya, xenia, eugene, random
put_accent = True
put_yo = True
device = torch.device('cpu')
# Загружаем модель TTS из репозитория snakers4/silero-models
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_tts', language=language, speaker=model_id)
model.to(device)
# если строка больше 1000 символов то разделяется на массивов и озвучивается
# 1000 = лимит
if len(text) >1000:
text = split_string(text,1000)
counter_current_part = 1
for i in text:
if stop_signal == False:
audio = model.apply_tts(text=i,
speaker=speaker,
sample_rate=sample_rate,
put_accent=put_accent,
put_yo=put_yo)
sd.play(audio, sample_rate)
#!
stop_tts(counter_current_part)
counter_current_part += 1
#!time.sleep(len(audio) / sample_rate)
sd.stop
else:
# Применяем TTS к тексту и получаем аудиоданные
text = str(text)
audio = model.apply_tts(text=text,
speaker=speaker,
sample_rate=sample_rate,
put_accent=put_accent,
put_yo=put_yo)
# Воспроизводим аудиоданные с помощью sounddevice
sd.play(audio, sample_rate)
#!
stop_tts()
#!time.sleep(len(audio) / sample_rate)
sd.stop
if __name__ == '__main__':
working_tts(
'''
прив
'''
)