-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.py
executable file
·66 lines (59 loc) · 1.65 KB
/
stt.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
#!/usr/bin/env python3
# Standard packages
import logging
import threading
import requests
import json
import shlex
import subprocess
# Local packages
import recording
import playwav
running = True
skip = False
def main():
say("ready")
global skip
while running:
if skip or recording.passive_listen():
threading.Thread(None, playwav.playwav, "readySound", ["data/ding.wav"]).start()
message = recording.active_listen()
if message:
interpret_command(message)
skip=False
else:
say("Excuse me, what did you say?")
skip=True
def interpret_command(fullcommand):
global running
global skip
if fullcommand == "quit":
running = False
say("Bye bye")
return
command = fullcommand.split(' ')
if command[0] == "lights" or command[0] == "light" or command[0] == "lamp" or command[0] == "lamps":
data = {"id": "lightbtn1",
"type": "button",
"data":{"action": "pressed"}}
send_message(data)
elif command[0] == "abort":
pass
else:
say("Sorry, couldn't interpret your request")
def send_message(msg):
try:
jsn = json.dumps(msg)
requests.request("POST", "http://127.0.0.1:5600/api/v0/event", json=jsn)
except Exception as e:
print(e)
say("Couldn't connect to homebrain server")
def say(msg, wait=True):
print("Saying: " + msg)
command = shlex.split("espeak \"{}\"".format(msg))
if wait:
subprocess.call(command)
else:
subprocess.Popen(command)
if __name__ == "__main__":
main()