forked from NoxWings/FreePie-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoiceToKeyboard.py
181 lines (141 loc) · 4.53 KB
/
VoiceToKeyboard.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import time;
# ***********************************
# Key Actions Classes
# ***********************************
class KeyAction:
def __init__(self, key):
self.keys = []
# append or extend (both are valid)
if isinstance(key, list):
self.keys.extend(key)
else:
self.keys.append(key)
def setKeyDown(self):
for key in self.keys:
keyboard.setKeyDown(key)
def setKeyUp(self):
for key in self.keys:
keyboard.setKeyUp(key)
def setKey(self, down):
for key in self.keys:
keyboard.setKey(key, down)
def setKeyPressed(self):
for key in self.keys:
#keyboard.setPressed(key)
keyboard.setKeyDown(key)
keyboard.setKeyUp(key)
def execute(self):
raise NotImplementedError("Subclass must implement execute method")
def update(self, curentTime):
pass
class KeyPress(KeyAction):
def __init__(self, key, duration = 0.07):
KeyAction.__init__(self, key)
self.duration = duration
self.time = time.time()
self.needUpdate = False
def execute(self):
# set the keys down
self.setKeyDown()
# start the update timer
self.time = time.time()
self.needUpdate = True
def update(self, currentTime):
if self.needUpdate:
# determine if we should stop pressing the keys
if (currentTime - self.time) >= self.duration:
self.setKeyUp()
self.needUpdate = False
class KeyRepeat(KeyAction):
def __init__(self, key, times, timeInterval = 0.14, duration = 0.07):
KeyAction.__init__(self, key)
self.times = times-1
self.timeInterval = timeInterval
# Duration cannot be larger than time interval
if (duration > timeInterval):
self.duration = timeInterval
else:
self.duration = duration
#internal use variables
self.time = time.time()
self.timesLeft = 0
self.needUpdate = False
def execute(self):
# set the keys down
self.setKeyDown()
# start the update timer
self.time = time.time()
self.timesLeft = self.times
self.needUpdate = True
def update(self, currentTime):
if self.needUpdate:
elapsedTime = currentTime - self.time
# Release Current Key
if (elapsedTime >= self.duration):
self.setKeyUp()
# End the update if the last key has been released
if (self.timesLeft == 0):
self.needUpdate = False
# Press Next KeyDown
if (elapsedTime >= self.timeInterval):
self.setKeyDown()
self.time = time.time()
self.timesLeft = self.timesLeft - 1
# ***********************************
# VoiceCommand Class
# ***********************************
class VoiceCommand:
def __init__(self, cmd, response, action = None):
self.cmd = cmd
self.response = response
self.action = action
def said(self, confidence, response=False):
return ((self.cmd != "") and speech.said(self.cmd, confidence))
def playResponse(self):
if self.response:
speech.say(self.response)
def execute(self):
if self.action:
self.action.execute()
def update(self, currentTime):
if self.action:
self.action.update(currentTime)
# ***********************************
# VoiceToKeyboard
# ***********************************
class VoiceToKeyboard:
def __init__(self, confidenceLevel, commands = None):
self.confidenceLevel = confidenceLevel
self.commands = []
self.setCommands(commands)
def setCommands(self, commands):
if isinstance(commands, list):
self.commands = commands
def addCommand(self, cmd, response, action = None):
self.commands.append( VoiceCommand(cmd, response, action) )
def executeLoop(self):
currentTime = time.time()
for command in self.commands:
# if said execute action
if command.said(self.confidenceLevel):
command.playResponse()
command.execute()
command.update(currentTime)
# ***********************************
# Config and commands
# ***********************************
if starting:
confidenceLevel = 0.7
v2k = VoiceToKeyboard( confidenceLevel )
# Voice response only
v2k.addCommand("Hello", "!Welcome! Initialising system.")
# Key Press
v2k.addCommand("Test single press", "Single key press", KeyPress( Key.A ))
v2k.addCommand("Test multiple press", "Multiple keys press", KeyPress( [ Key.LeftShift, Key.A ] ))
# Key Hold
v2k.addCommand("Test single hold", "Single key hold. 2 seconds", KeyPress( Key.B, 2 ))
v2k.addCommand("Test multiple hold", "Multiple keys hold. 2 seconds", KeyPress( [ Key.LeftShift, Key.B ], 2 ))
# Key Repeat
v2k.addCommand("Test single repeat", "Pressing C key 5 times", KeyRepeat( Key.C, 5 ))
v2k.addCommand("Test multiple repeat", "Pressing Shift and C keys 5 times", KeyRepeat( [ Key.LeftShift, Key.C ], 5 , 0.1, 0.07 ))
v2k.executeLoop()