-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.py
158 lines (128 loc) · 3.84 KB
/
voice.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
from io import BytesIO
import threading
import time
from flask import Flask, request
import logging, os, signal
from gtts import gTTS
import pydub
from pydub.playback import play
from TTS.api import TTS
import sounddevice
import requests, termcolor
app = Flask(__name__)
tts = None
app.logger.disabled=True
logging.getLogger('werkzeug').disabled = True
sentences_received=[]
audios_made = []
flag_kill_threads = False
tts_worker_running = False
play_worker_running = False
def log(text, color):
print(termcolor.colored(text, color), flush=True)
@app.route('/sentence', methods=['POST'])
def api_words():
color='yellow'
global sentences_received
data = request.get_data(as_text=True) or '.'
accent = request.args.get('accent')
tts_engine = request.args.get('tts')
sentence = {
"data":data ,
"accent":accent,
"tts":tts_engine
}
sentences_received.append(sentence)
log(f"post /sentence | {sentence}", color)
return ''
@app.route('/exit', methods=['POST'])
def api_exit():
global flag_kill_threads
flag_kill_threads=True
os.kill(os.getpid(), signal.SIGINT)
return ''
def play_mp3(fp, type='mp3'):
mp3 = pydub.AudioSegment.from_file(fp, type)
mp3 = mp3.speedup(1.3)
play(mp3)
def tts_gtts(data, accent):
fp = BytesIO()
g=gTTS(data, accent, lang_check=False)
g.write_to_fp(fp)
fp.seek(0)
return fp
def tts_llm(data, accent):
global tts
speaker = 'p251' if accent == 'us' else 'p305'
if tts is None:
tts = TTS("tts_models/en/vctk/vits", progress_bar=False).to('cpu')
wav = tts.tts(text=data, speaker=speaker)
return wav
def play_wav(wav):
sounddevice.play(wav, 22050)
sounddevice.wait()
def worker_make_audio():
color='blue'
global sentences_received, audios_made, tts_worker_running
if tts_worker_running: return
tts_worker_running = True
while True:
if flag_kill_threads: break
if len(sentences_received) == 0:
time.sleep(0.1)
continue
copy = sentences_received.copy()
sentences_received = []
backlog = len(copy)
log( f"worker_make_audio: backlog is {backlog}", color)
for item in copy:
data = item['data']
accent = item['accent']
engine = item['tts']
result = item.copy()
if engine == 'coqui-tts':
wav = tts_llm(data, accent)
result['wav'] = wav
audios_made.append(result)
elif engine == 'gtts':
mp3 = tts_gtts(data, accent)
result['wav'] = mp3
audios_made.append(result)
tts_worker_running = False
def worker_play_audio():
color='green'
global audios_made, play_worker_running
if play_worker_running: return
play_worker_running = True
while True:
if flag_kill_threads:
break
if len(audios_made) == 0:
time.sleep(0.1)
continue
copy = audios_made.copy()
audios_made = []
backlog = len(copy)
log(f"worker_play_audio: backlog is {backlog}", color)
for item in copy:
c = item.copy()
del c['wav']
try:
requests.post('http://127.0.0.1:6000/sentence', json=c, timeout=1)
except:
pass
if item['tts'] == 'coqui-tts':
play_wav(item['wav'])
elif item['tts'] == 'gtts':
fp = item['wav']
play_mp3(fp)
fp.close()
play_worker_running = False
def main():
audioplayer = threading.Thread(target=worker_play_audio)
audioplayer.start()
audiomaker = threading.Thread(target=worker_make_audio)
audiomaker.start()
app.run('0.0.0.0', 5000)
if __name__ == "__main__":
main()