-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliri-commander.py
139 lines (104 loc) · 3.81 KB
/
liri-commander.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
#!/usr/bin/env python3
import speech_recognition as sr
import subprocess
import simplejson as json
from commands import Commander, Response
import pyaudio
import wave
import sys, os
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qt import *
from PyQt5.QtQuick import *
command = ""
active = True
def open_file(filename):
chunk = 1024
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(
format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True)
data = wf.readframes(chunk)
while data:
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
r = sr.Recognizer()
def say(text):
subprocess.call('say -v "Samantha" ' + text, shell=True)
quit = ["bye", "by", "goody bye", "buy", "quit", "seeya", "later", "shutdown"]
active = True
def initSpeak():
print("Speaking")
open_file('./audio/audio_initiate.wav')
with sr.Microphone() as source:
print("Say something")
audio = r.listen(source)
open_file('./audio/audio_end.wav')
command = ""
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
# print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
obj = engine.rootObjects()
myObj = obj[0].findChild(QObject, 'mainPage')
QMetaObject.invokeMethod(myObj, "updateQuestion", Qt.DirectConnection, Q_ARG("QVariant", r.recognize_google(audio)))
command = r.recognize_google(audio)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
if command in quit:
say("Bye")
active = False
else:
commander = Commander()
commander.discover(command)
if __name__ == "__main__":
# Create the application instance.
app = QGuiApplication(sys.argv)
# Create a QML engine.
engine = QQmlApplicationEngine()
engine.addImportPath("/usr/lib/qt/qml/")
engine.addImportPath("/Users/nickgermaine/Qt/5.7/clang_64/qml")
# Create a component factory and load the QML script.
engine.load(QUrl.fromLocalFile("main.qml"))
win = engine.rootObjects()[0]
speakButton = win.findChild(QObject, "speakButton")
speakButton.initSpeak.connect(initSpeak)
ctx = engine.rootContext()
ctx.setContextProperty("mainAppPy", engine)
win.show()
sys.exit(app.exec_())
'''QApplication,
open_file('./audio/audio_initiate.wav')
with sr.Microphone() as source:
print("Say something")
audio = r.listen(source)
open_file('./audio/audio_end.wav')
command = ""
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
# print("Google Speech Recognition thinks you said " + r.recognize_google(audio))
command = r.recognize_google(audio)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
if command in quit:
say("Bye")
active = False
else:
commander = Commander()
commander.discover(command)
active = False
'''