-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_interface.py
More file actions
55 lines (41 loc) · 1.52 KB
/
Copy pathvoice_interface.py
File metadata and controls
55 lines (41 loc) · 1.52 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
import queue
import json
import pyttsx3
import sounddevice as sd
import vosk
from utils import SocketConnector
settings = json.load(open('settings.json'))
voice_engine = pyttsx3.init()
voices = voice_engine.getProperty('voices')
voice_engine.setProperty('voice', voices[settings['voice_id']].id)
device = 0
device_info = sd.query_devices(device, 'input')
samplerate = int(device_info['default_samplerate'])
model = vosk.Model(settings['vosk_path'])
q = queue.Queue()
mic_blocked = False
connector = SocketConnector(settings['main_ip'], settings['voice_in'], settings['voice_out'], buffer_size=settings['voice_buffer_size'])
def callback(indata, frames, time, status):
if status:
print(status)
if not mic_blocked:
q.put(bytes(indata))
with sd.RawInputStream(samplerate=samplerate, blocksize = 8000, device=device, dtype='int16',
channels=1, callback=callback):
rec = vosk.KaldiRecognizer(model, samplerate)
while True:
recieved = connector.recieve_str()
if recieved != '':
voice_engine.say(recieved)
voice_engine.runAndWait()
data = q.get()
if rec.AcceptWaveform(data):
recognized_data = rec.Result()
recognized_data = json.loads(recognized_data)
voice_input_str = recognized_data["text"]
if voice_input_str != "":
print(voice_input_str)
mic_blocked = False
connector.send_text(voice_input_str)
else:
pass