-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
215 lines (186 loc) · 8.7 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# This is a sample Python script.
import threading
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import PySimpleGUI as sg
from pages import asr_page, vc_page
from utils import i18n_util, tts_util, file_util, audio_util
from enumeration import language_type
from constants import constants
# international language configuration
i18n = i18n_util.I18nUtil()
# print(i18n.language_map)
# TTS is working or not
is_reading = False
# record current tab, only three choices: tts, asr, vc
current_tab = constants.TAB_TTS
vc_page_instance = vc_page.VCPage()
def build_gui():
global vc_page_instance
tts_text_in_English = 'VITS is Awesome!'
tts_text_in_Chinese = '遥望星空作文独自坐在乡间的小丘上,看着阳光渐渐变暗,听着鸟鸣渐渐变弱,触着清风渐渐变凉。'
# Define the window's contents
# tts subview
tts_subview = [[sg.Checkbox("", key="-CHECKBOX_TEXT-", default=True, enable_events=True),
sg.Multiline(tts_text_in_Chinese, key="-INPUT-", expand_x=True, expand_y=True, justification='left',
enable_events=True)],
[sg.Checkbox("", key="-CHECKBOX_FILE-", default=False, enable_events=True),
sg.Text("Choose a file: "), sg.Input(key="-FILE_PATH-", enable_events=True),
sg.FileBrowse(file_types=(("Text Files", "*.txt"),))],
[sg.Button(i18n("朗读"), key="-READ-", pad=(40, 0), button_color=('black', 'white')),
sg.Checkbox(i18n("英语"), key="-CHECKBOX_ENGLISH-", default=True, enable_events=True, pad=(40, 0)),
sg.Checkbox(i18n("中文"), key="-CHECKBOX_CHINESE-", default=False, enable_events=True)]]
tts_layout = [[sg.Frame('Text to Speech', tts_subview, size=(750, 400), font='Any 12', title_color='blue')]]
# asr subview
asr_layout = asr_page.ASRPage.build_asr_gui()
# rvc subview
rvc_layout = vc_page_instance.build_vc_gui()
# quickvc subview
quickvc_layout = [[sg.Text('quick voice conversion')]]
main_layout = [[sg.TabGroup([[sg.Tab(constants.TAB_TTS, tts_layout),
sg.Tab(constants.TAB_ASR, asr_layout),
sg.Tab(constants.TAB_RVC, rvc_layout),
sg.Tab(constants.TAB_QUICK_VC, quickvc_layout)]],
key="-TAB_GROUP-",
enable_events=True)]]
return main_layout
def change_gui_when_starting_reading():
global is_reading
is_reading = True
# update UI of read button
window["-READ-"].update(text=i18n("停止"), button_color=('white', 'red'))
def change_gui_when_finishing_reading():
global is_reading
is_reading = False
# update UI of read button
window["-READ-"].update(text=i18n("朗读"), button_color=('black', 'white'))
def generate_English_audio_and_read(content):
global is_reading
# Generate audio
audio, sr = tts_util.TTSUtil.generate_audio_from_text(content)
if not is_reading:
return
# Play audio
audio_util.AudioUtil.play_audio(audio, sr)
# reset UI of read button
change_gui_when_finishing_reading()
def generate_Chinese_audio_and_read(content):
global is_reading
# Generate audio
audio, sr = tts_util.TTSUtil.generate_Chinese_audio_from_text(content)
if not is_reading:
return
# Play audio
audio_util.AudioUtil.play_audio(audio, sr)
# reset UI of read button
change_gui_when_finishing_reading()
def readTextFromInputField(content, language):
if content == '':
sg.popup_ok(i18n('请先输入文本'))
return
change_gui_when_starting_reading()
print(language == language_type.LanguageType.ENGLISH)
print("text to be read is ", content)
if language == language_type.LanguageType.ENGLISH:
# create a new thread with the function as the target and a parameter
t = threading.Thread(target=generate_English_audio_and_read, args=(content,))
# start the thread
t.start()
elif language == language_type.LanguageType.CHINESE:
# create a new thread with the function as the target and a parameter
t = threading.Thread(target=generate_Chinese_audio_and_read, args=(content,))
# start the thread
t.start()
else:
change_gui_when_finishing_reading()
def readTextFromLocalFile(file_path, language):
if file_path == "" or not file_path.endswith(".txt"):
sg.popup_ok(i18n("请先选择txt文件"))
return
content = file_util.FileUtil.get_text_from_file(file_path)
if content == '':
sg.popup_ok(i18n('请先输入文本'))
return
change_gui_when_starting_reading()
if language == language_type.LanguageType.ENGLISH:
# create a new thread with the function as the target and a parameter
t = threading.Thread(target=generate_English_audio_and_read, args=(content,))
# start the thread
t.start()
elif language == language_type.LanguageType.CHINESE:
# create a new thread with the function as the target and a parameter
t = threading.Thread(target=generate_Chinese_audio_and_read, args=(content,))
# start the thread
t.start()
else:
change_gui_when_finishing_reading()
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# Create directories
file_util.FileUtil.create_data_storage_dir()
# Create the window
window = sg.Window('Voicer', build_gui(), size=(800, 560), resizable=True)
# Display and interact with the Window using an Event Loop
while True:
event, values = window.read()
# See if user wants to quit or window was closed
if event == sg.WINDOW_CLOSED or event == 'Quit':
break
if event == "-READ-":
if is_reading:
# stop playing audio if audio is being played now
audio_util.AudioUtil.stop_playing_audio()
# reset UI of read button
change_gui_when_finishing_reading()
else:
# text in English to speech
lang = None
if values["-CHECKBOX_ENGLISH-"]:
lang = language_type.LanguageType.ENGLISH
elif values["-CHECKBOX_CHINESE-"]:
lang = language_type.LanguageType.CHINESE
if lang is None:
continue
if values["-CHECKBOX_TEXT-"]:
readTextFromInputField(values["-INPUT-"], lang)
else:
readTextFromLocalFile(values["-FILE_PATH-"], lang)
elif event == "-CHECKBOX_TEXT-":
if values["-CHECKBOX_TEXT-"]:
window["-CHECKBOX_FILE-"].update(value=False)
else:
window["-CHECKBOX_FILE-"].update(value=True)
elif event == "-CHECKBOX_FILE-":
if values["-CHECKBOX_FILE-"]:
window["-CHECKBOX_TEXT-"].update(value=False)
else:
window["-CHECKBOX_TEXT-"].update(value=True)
elif event == "-INPUT-":
# after operation on MULTILINE widget, if the content of MULTILINE is not empty and CHECKBOX_TEXT is not
# ticked, just tick it
if values["-INPUT-"] != "" and not values["-CHECKBOX_TEXT-"]:
window["-CHECKBOX_TEXT-"].update(value=True)
window["-CHECKBOX_FILE-"].update(value=False)
elif event == "-FILE_PATH-":
print("file path is ", values["-FILE_PATH-"])
if values["-FILE_PATH-"] != "" and not values["-CHECKBOX_FILE-"]:
window["-CHECKBOX_FILE-"].update(value=True)
window["-CHECKBOX_TEXT-"].update(value=False)
elif event == "-CHECKBOX_ENGLISH-":
window["-CHECKBOX_ENGLISH-"].update(value=True)
window["-CHECKBOX_CHINESE-"].update(value=False)
elif event == "-CHECKBOX_CHINESE-":
window["-CHECKBOX_ENGLISH-"].update(value=False)
window["-CHECKBOX_CHINESE-"].update(value=True)
if event == '-TAB_GROUP-':
print(f'Tab switched to: {values["-TAB_GROUP-"]}')
current_tab = values["-TAB_GROUP-"]
if current_tab == constants.TAB_ASR:
asr_page.ASRPage.handle_event(window, event, values)
elif current_tab == constants.TAB_RVC:
vc_page_instance.handle_event(window, event, values)
elif current_tab == constants.TAB_QUICK_VC:
print("transfer event to quick vc")
# Finish up by removing from the screen
window.close()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/