-
Notifications
You must be signed in to change notification settings - Fork 1
/
speech.py
81 lines (71 loc) · 2.7 KB
/
speech.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
from audio import TranscribeWorker, Parser
from queue import Queue
from StateEnum import StateEnum
from visual import Color, Led
init_color = Color(0, 255, 0)
pins = (14, 15, 18)
# led = Led(init_color, pins)
# This function is called from within the Parser
def action(actionType: StateEnum, color: Color = Color(0, 0, 0)):
# The states are seperated into 2 as 'actions' and `set color`
actions = [StateEnum.ON, StateEnum.OFF, StateEnum.BLINK]
# We might want to do additional stuff if the state is an action
if actionType == StateEnum.ON:
led.blink()
elif actionType in actions:
print("State:", actionType.name)
elif actionType == StateEnum.ON or actionType == StateEnum.OFF:
# Toggle led on and off
led.toggleOn()
elif actionType == StateEnum.NOT_UNDERSTOOD:
print("Vocab:", list(Parser().vocab.keys()))
elif actionType == StateEnum.CHANGE_COLOR:
# Change the color of the LEDs
led.setColor(color)
# This function runs on the main thread, which handles the machine state
# Such as the colors of the LEDs and indicator lights.
def main():
# Create and store the event queue in the main thread
# The queue keeps the events that happen during listening to the audio
# These events are:
# SENDING, LISTENING, ERROR, NOT_UNDERSTOOD and OK
q = Queue()
# Pass the reference of the queue to the Audio object so that it can add
# items to it
a = Audio(q)
# The parser checks for meaning in the text and runs corresponding functions
p = Parser()
# Start the audio recognition thread
a.start()
# Run the main thread
while a.running:
try:
# The execution of the main thread will stop here and wait for
# a new item in the queue
result = q.get()
# The results are the states listed above
if result == StateEnum.SENDING:
print("Sending...")
elif result == StateEnum.LISTENING:
print("Listening...")
elif result == StateEnum.ERROR:
print("Error!")
elif result == StateEnum.NOT_UNDERSTOOD:
print("Not understood!")
elif result == StateEnum.END:
a.stop()
else:
# Not explicitly checking for the OK state because if the speech is understood
# the value of result will be a string
print("You said:", result)
p.parse(result, action)
q.task_done()
except:
# (KeyboardInterrupt, SystemExit)
a.stop()
quit()
# Stop the threads
a.stop()
# Run the program
if __name__ == "__main__":
main()