-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoiceInterpreter.py
114 lines (88 loc) · 3.02 KB
/
voiceInterpreter.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
import pyttsx3
import threading
import pyaudio
from vosk import Model, KaldiRecognizer, SetLogLevel
from time import sleep
import json
import moduleManager
from os.path import exists
isActive = False
isTalking = False
doNotStartSpeak = False
loaded = False
def toggleActive():
global isActive
global doNotStartSpeak
global loaded
if not loaded:
from window import ttsText
ttsText("Voice module is still loading, please wait.")
return
if doNotStartSpeak == False:
if isActive == False:
isActive = True
x = threading.Thread(target=speak, args=("I am active.",), daemon=True)
x.start()
elif isActive == True:
isActive = False
x = threading.Thread(target=speak, args=("I am not active.",), daemon=True)
x.start()
def speak(command):
global isTalking
global doNotStartSpeak
doNotStartSpeak = True
sleep(0.1)
if isTalking == True:
engine = pyttsx3.init()
engine.stop()
x = threading.Thread(target=speak, args=(command,), daemon=True)
x.start()
return
elif isTalking == False:
from window import ttsText
ttsText(command)
isTalking = True
doNotStartSpeak = False
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
print("Done")
isTalking = False
return
def listening():
global loaded
if exists("vosk-model-en-us-0.22"):
model = Model(r"vosk-model-en-us-0.22")
elif exists("vosk-model-en-us-0.22-lgraph"):
model = Model(r"vosk-model-en-us-0.22-lgraph")
else:
sleep(1)
from window import ttsText
ttsText("Error, can't find model")
raise Exception("Error, can't find model")
recognizer = KaldiRecognizer(model, 16000)
from window import inputText
inputText("Ready to listen!")
loaded = True
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192)
while True:
while isActive == True:
stream.start_stream()
while isActive == True:
data = stream.read(4096, exception_on_overflow = False)
if recognizer.AcceptWaveform(data):
output = json.loads(recognizer.Result())
print(output['text'])
if recognizer.Result() != "":
if output['text'] != "" and output['text'] != " " and output['text'] != "the":
inputText(output['text'])
if output['text'] != "" and output['text'] != "the":
x = threading.Thread(target=speak, args=(moduleManager.redirectInformation(output['text'])[1],))
x.start()
if stream.is_active():
stream.stop_stream()
sleep(0.1)
#SetLogLevel(-1)
z = threading.Thread(target=listening, daemon=True)
z.start()