-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_remoteva_voskrem.py
163 lines (128 loc) · 6.07 KB
/
run_remoteva_voskrem.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
import argparse
import os
import queue
import sounddevice as sd
import vosk
import sys
import requests
import json
import play_wav
version="1.3"
# main options
with open('options.json', 'r', encoding="utf-8") as f:
s = f.read(10000000)
f.close()
saved_options = json.loads(s)
ttsFormat = saved_options["ttsFormat"] # "none" (TTS on server) or "saytxt" (TTS on client)
# or "saywav" (TTS on server to WAV, Wav played on client)
# or mix of the params, splitted by , = "none,saytxt" as example
ttsFormatList = ttsFormat.split(",")
baseUrl = saved_options["baseUrl"] # server with Irene WEB api
from urllib.parse import urlparse
urlparsed = urlparse(baseUrl)
if os.path.exists("error_connection.wav"):
pass
else: # первый вызов, давайте получим файлы
print("Получаем WAV-файлы, которые будут играться в случае ошибок...")
r = requests.get(baseUrl+"ttsWav", params={"text": "Ошибка: потеряна связь с сервером"})
res = json.loads(r.text)
play_wav.saywav_to_file(res,'error_connection.wav')
r = requests.get(baseUrl+"ttsWav", params={"text": "Ошибка при обработке результата сервера"})
res = json.loads(r.text)
play_wav.saywav_to_file(res,'error_processing.wav')
print("WAV-файлы для ошибок получены!")
if "saytxt" in ttsFormatList:
import pyttsx3
ttsEngine = pyttsx3.init()
voices = ttsEngine.getProperty("voices")
ttsEngine.setProperty("voice", 0)
mic_blocked = False
import json
import os
import sys
import asyncio
import websockets
import logging
import sounddevice as sd
import argparse
# ------------------- vosk ------------------
def int_or_str(text):
"""Helper function for argument parsing."""
try:
return int(text)
except ValueError:
return text
def callback(indata, frames, time, status):
"""This is called (from a separate thread) for each audio block."""
loop.call_soon_threadsafe(audio_queue.put_nowait, bytes(indata))
async def run_test():
with sd.RawInputStream(samplerate=args.samplerate, blocksize = 4000, device=args.device, dtype='int16',
channels=1, callback=callback) as device:
async with websockets.connect(args.uri) as websocket:
await websocket.send('{ "config" : { "sample_rate" : %d } }' % (device.samplerate))
while True:
data = await audio_queue.get()
await websocket.send(data)
#print (await websocket.recv())
res = await websocket.recv()
resj = json.loads(res)
if "text" in resj:
voice_input_str = resj["text"]
#print(restext)
if voice_input_str != "" and voice_input_str != None:
print(voice_input_str)
try:
r = requests.get(baseUrl+"sendRawTxt", params={"rawtxt": voice_input_str, "returnFormat": ttsFormat})
if r.text != "":
res = json.loads(r.text)
if res != "NO_VA_NAME": # some cmd was run
if res != None and res != "": # there is some response to play
if "saytxt" in ttsFormatList:
if "restxt" in res.keys():
ttsEngine.say(res["restxt"])
ttsEngine.runAndWait()
if "saywav" in ttsFormatList:
play_wav.saywav_to_file(res,'tmpfile.wav')
mic_blocked = True
try:
play_wav.play_wav('tmpfile.wav')
except Exception as e:
print("Can't play received WAV")
mic_blocked = False
except requests.ConnectionError as e:
play_wav.play_wav('error_connection.wav')
except Exception as e:
play_wav.play_wav('error_processing.wav')
else:
#print("2",rec.PartialResult())
pass
await websocket.send('{"eof" : 1}')
#print (await websocket.recv())
async def main():
global args
global loop
global audio_queue
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-l', '--list-devices', action='store_true',
help='show list of audio devices and exit')
args, remaining = parser.parse_known_args()
if args.list_devices:
print(sd.query_devices())
parser.exit(0)
parser = argparse.ArgumentParser(description="ASR Server",
formatter_class=argparse.RawDescriptionHelpFormatter,
parents=[parser])
parser.add_argument('-u', '--uri', type=str, metavar='URL',
# by default connect to WEBAPI server with PORT+1
help='Server URL', default='ws://{0}:{1}'.format(urlparsed.hostname,urlparsed.port+1))
parser.add_argument('-d', '--device', type=int_or_str,
help='input device (numeric ID or substring)')
parser.add_argument('-r', '--samplerate', type=int, help='sampling rate', default=16000)
args = parser.parse_args(remaining)
loop = asyncio.get_running_loop()
audio_queue = asyncio.Queue()
logging.basicConfig(level=logging.INFO)
print("Remote Irene (VOSK REMOTE recognizer) v{0} started! ttsFormat={1}, baseUrl={2}, speechRecognizerWebsocketUrl={3}".format(version,ttsFormat,baseUrl,args.uri))
await run_test()
if __name__ == '__main__':
asyncio.run(main())