-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_cmdmemory.py.txt
More file actions
193 lines (154 loc) · 7.24 KB
/
Copy path_cmdmemory.py.txt
File metadata and controls
193 lines (154 loc) · 7.24 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# This file is a command-module for Dragonfly.
# (c) Copyright 2008 by Christo Butcher
# Licensed under the LGPL, see <http://www.gnu.org/licenses/>
#
"""
Command-module for **command memory**
============================================================================
This module offers voice-commands for *remembering* and *repeating*
previous recognitions. This works just as well for *text* recognitions
(such as "hello world!") and *command*
Commands
----------------------------------------------------------------------------
The following voice commands are available:
Command: **"repeat [last] [<n>] (commands | command) [<count> times]"**
Repeats the *<n>* most recent recognitions. If *<count>* is also
spoken, the series of recognitions is repeated that many times.
Command: **"remember [last] [<n>] (commands | command) as <name>"**
Remembers the *<n>* most recent recognitions under the name *<name>*.
The remember of recognitions can be repeated by simply saying
**"<name>"**.
Command: **"start recording <name>"**
Starts remembering recognitions and stores them under the name *<name>*.
The user must speak **"stop recording"** later. After that all the
recognitions heard between start and stop commands can be repeated by
simply saying **"<name>"**.
Command: **"stop recording"**
Stops remembering recognitions.
Command: **"<memory> [<count> times]"**
Plays back the series of recognitions previously remembered under
the spoken *<name>*. If *<count>* is also spoken, the series of
recognitions is repeated that many times.
"""
try:
import pkg_resources
pkg_resources.require("dragonfly >= 0.6.5beta1.dev-r97")
except ImportError:
pass
from dragonfly import *
#---------------------------------------------------------------------------
# Set up this module's configuration.
config = Config("command memory")
config.lang = Section("Language section")
config.lang.playback_last = Item("(playback | repeat) [last] [<n>] (commands | command | recognitions) [<count> times]")
config.lang.remember = Item("remember [last] [<n>] (commands | command | recognitions) as <name>")
config.lang.start_recording = Item("start recording <name>")
config.lang.stop_recording = Item("stop recording")
config.lang.playback_memory = Item("<memory> [<count> times]")
config.lang.recall = Item("recall everything")
config.load()
#---------------------------------------------------------------------------
# Create global dicts for storing command memories.
# Dictionary for storing memories.
memories = DictList("memories")
memories_ref = DictListRef("memory", memories)
# Recognition observer for retrieving recently heard recognitions.
playback_history = PlaybackHistory()
try:
playback_history.register()
except Exception, e:
print "Warning, failed to register playback_history: %s" % e
record_history = PlaybackHistory()
record_name = None
#---------------------------------------------------------------------------
# Define this module's main functionality and rule.
def print_history():
o = playback_history
print ("(%s) %s: %s\n" % (id(o), "playback_history", "recall") + "\n".join((" - %r" % (item,)) for item in o))
o =record_history
print ("(%s) %s: %s\n" % (id(o), "record_history", "recall") + "\n".join((" - %r" % (item,)) for item in o))
def playback_last(n, count):
# Retrieve playback-action from recognition observer.
if playback_history and playback_history.complete:
playback_history.pop() # Remove playback recognition itself.
action = playback_history[-n:] # Retrieve last *n* recognitions.
action.speed = 10 # Playback at 10x original speed.
# Playback action.
import time; time.sleep(1)
playback_history.unregister() # Don't record playback.
try:
for index in range(count): # Playback *count* times.
action.execute() # Perform playback.
finally:
playback_history.register() # Continue recording after playback.
def remember(n, name):
# Retrieve playback-action from recognition observer and store it.
if playback_history and playback_history.complete:
playback_history.pop() # Remove playback recognition itself.
action = playback_history[-n:] # Retrieve last *n* recognitions.
action.speed = 10 # Playback at 10x original speed.
memories[str(name)] = action # Store playback action.
def start_recording(name):
global record_name
record_name = str(name) # Remember memory name for later.
record_history.register() # Start recording with *record_history*.
del record_history[:] # Clear record history.
def stop_recording():
global record_name
record_history.unregister() # Stop recording with *record_history*.
if record_history and record_history.complete:
record_history.pop() # Remove playback recognition itself.
if not record_name:
return
action = record_history[:]
action.speed = 10 # Playback at 10x original speed.
memories[record_name] = action # Store playback action.
record_name = None
def playback_memory(count, memory):
if playback_history and playback_history.complete:
playback_history.pop() # Remove playback recognition itself.
# Playback remembered action.
playback_history.unregister() # Don't record playback.
try:
for index in range(count): # Playback *count* times.
memory.execute() # Perform playback.
finally:
playback_history.register() # Continue recording after playback.
class PlaybackRule(MappingRule):
mapping = { # Spoken form -> -> -> action
config.lang.playback_last: Function(playback_last),
config.lang.start_recording: Function(start_recording),
config.lang.stop_recording: Function(stop_recording),
config.lang.remember: Function(remember),
config.lang.playback_memory: Function(playback_memory),
config.lang.recall: Function(print_history),
}
extras = [
IntegerRef("n", 1, 100), # *n* designates the number
# of recent recognitions.
IntegerRef("count", 1, 100), # *count* designates how
# many times to repeat
# playback.
Dictation("name"), # *name* is used when
# Naming new memories.
memories_ref, # This is the list of
# already-remembered
# memories; its name is
# *memory*.
]
defaults = {
"n": 1,
"count": 1,
}
#---------------------------------------------------------------------------
# Create and load this module's grammar.
grammar = Grammar("command memory") # Create this module's grammar.
grammar.add_rule(PlaybackRule()) # Add the top-level rule.
grammar.load() # Load the grammar.
# Unload function which will be called at unload time.
def unload():
playback_history.unregister()
record_history.unregister()
global grammar
if grammar: grammar.unload()
grammar = None