annyang 2.6.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +55 -31
- package/dist/annyang.cjs +354 -0
- package/dist/annyang.cjs.map +1 -0
- package/dist/annyang.d.cts +288 -0
- package/dist/annyang.d.ts +288 -0
- package/dist/annyang.iife.min.js +1 -0
- package/dist/annyang.js +296 -670
- package/dist/annyang.js.map +1 -1
- package/package.json +54 -36
- package/.editorconfig +0 -13
- package/.eslintrc.json +0 -34
- package/.gitattributes +0 -1
- package/.github/ISSUE_TEMPLATE.md +0 -32
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -29
- package/.jshintrc +0 -31
- package/.travis.yml +0 -20
- package/CONTRIBUTING.md +0 -75
- package/Gruntfile.js +0 -144
- package/bower.json +0 -25
- package/demo/css/main.css +0 -152
- package/demo/css/main.min.css +0 -1
- package/demo/images/flickr.png +0 -0
- package/demo/images/footer_background.jpg +0 -0
- package/demo/images/icon_js.png +0 -0
- package/demo/images/icon_speech.png +0 -0
- package/demo/images/icon_user.png +0 -0
- package/demo/images/mini_icon_say.png +0 -0
- package/demo/images/palette.png +0 -0
- package/demo/images/tpscover.jpg +0 -0
- package/demo/index.html +0 -225
- package/demo/vendor/css/default.css +0 -135
- package/demo/vendor/css/github.css +0 -127
- package/demo/vendor/css/tomorrow.css +0 -49
- package/demo/vendor/html/github-btn.html +0 -243
- package/demo/vendor/js/highlight.pack.js +0 -1
- package/dist/annyang.min.js +0 -12
- package/docs/FAQ.md +0 -223
- package/docs/README.md +0 -336
- package/require.js +0 -36
- package/sites/README.md +0 -6
- package/sites/facebook.js +0 -123
- package/sites/facebook.min.js +0 -17
- package/sites/geektime.js +0 -108
- package/sites/geektime.min.js +0 -17
- package/src/annyang.js +0 -680
- package/test/SpecRunner.html +0 -41
- package/test/init_corti.js +0 -1
- package/test/spec/.jshintrc +0 -40
- package/test/spec/BasicSpec.js +0 -1616
- package/test/spec/IssuesSpec.js +0 -41
- package/test/vendor/corti.js +0 -202
- package/yarn.lock +0 -4190
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
interface CallbackMap {
|
|
2
|
+
start: () => void;
|
|
3
|
+
end: () => void;
|
|
4
|
+
soundstart: () => void;
|
|
5
|
+
result: (phrases: string[]) => void;
|
|
6
|
+
resultMatch: (userSaid: string, commandText: string, phrases: string[]) => void;
|
|
7
|
+
resultNoMatch: (phrases: string[]) => void;
|
|
8
|
+
error: (event: SpeechRecognitionErrorEvent) => void;
|
|
9
|
+
errorNetwork: (event: SpeechRecognitionErrorEvent) => void;
|
|
10
|
+
errorPermissionBlocked: (event: SpeechRecognitionErrorEvent) => void;
|
|
11
|
+
errorPermissionDenied: (event: SpeechRecognitionErrorEvent) => void;
|
|
12
|
+
}
|
|
13
|
+
type CallbackType = keyof CallbackMap;
|
|
14
|
+
/**
|
|
15
|
+
* Is SpeechRecognition supported in this environment?
|
|
16
|
+
*
|
|
17
|
+
* @returns {boolean} true if SpeechRecognition is supported by the browser
|
|
18
|
+
*/
|
|
19
|
+
declare const isSpeechRecognitionSupported: () => boolean;
|
|
20
|
+
type CommandCallback = (...args: string[]) => void;
|
|
21
|
+
interface CommandsList {
|
|
22
|
+
[key: string]: CommandCallback | {
|
|
23
|
+
regexp: RegExp;
|
|
24
|
+
callback: CommandCallback;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Add commands that annyang will respond to.
|
|
29
|
+
* By default this will add to the existing commands. Pass `true` as the second parameter to remove all existing commands first.
|
|
30
|
+
*
|
|
31
|
+
* #### Examples:
|
|
32
|
+
* ````javascript
|
|
33
|
+
* const commands1 = {'hello :name': helloFunction, 'howdy': helloFunction};
|
|
34
|
+
* const commands2 = {'hi': helloFunction};
|
|
35
|
+
*
|
|
36
|
+
* annyang.addCommands(commands1);
|
|
37
|
+
* annyang.addCommands(commands2);
|
|
38
|
+
* // annyang will now listen for all three commands defined in commands1 and commands2
|
|
39
|
+
*
|
|
40
|
+
* annyang.addCommands(commands2, true);
|
|
41
|
+
* // annyang will now only listen for the command in commands2
|
|
42
|
+
* ````
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} commands - Commands that annyang should listen for
|
|
45
|
+
* @param {boolean} [resetCommands=false] - Remove all existing commands before adding new commands? * @see [Commands Object](#commands-object)
|
|
46
|
+
*/
|
|
47
|
+
declare const addCommands: (commands: CommandsList, resetCommands?: boolean) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Remove existing commands. Called with a single phrase, an array of phrases, or with no params to remove all commands.
|
|
50
|
+
*
|
|
51
|
+
* #### Examples:
|
|
52
|
+
* ````javascript
|
|
53
|
+
* const commands = {'hello': helloFunction, 'howdy': helloFunction, 'hi': helloFunction};
|
|
54
|
+
*
|
|
55
|
+
* // Remove all existing commands
|
|
56
|
+
* annyang.removeCommands();
|
|
57
|
+
*
|
|
58
|
+
* // Add some commands
|
|
59
|
+
* annyang.addCommands(commands);
|
|
60
|
+
*
|
|
61
|
+
* // Don't respond to hello
|
|
62
|
+
* annyang.removeCommands('hello');
|
|
63
|
+
*
|
|
64
|
+
* // Don't respond to howdy or hi
|
|
65
|
+
* annyang.removeCommands(['howdy', 'hi']);
|
|
66
|
+
* ````
|
|
67
|
+
* @param {string|string[]|undefined} [commandsToRemove] - Commands to remove
|
|
68
|
+
*/
|
|
69
|
+
declare const removeCommands: (commandsToRemove?: string | string[] | undefined) => void;
|
|
70
|
+
interface StartOptions {
|
|
71
|
+
autoRestart?: boolean;
|
|
72
|
+
continuous?: boolean;
|
|
73
|
+
paused?: boolean;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Start listening.
|
|
77
|
+
* It's a good idea to call this after adding some commands first (but not mandatory)
|
|
78
|
+
*
|
|
79
|
+
* Receives an optional options object which supports the following options:
|
|
80
|
+
*
|
|
81
|
+
* - `autoRestart` (boolean) Should annyang restart itself if it is closed indirectly (e.g. because of silence or window conflicts)?
|
|
82
|
+
* - `continuous` (boolean) Allow forcing continuous mode on or off. annyang is pretty smart about this, so only set this if you know what you're doing.
|
|
83
|
+
* - `paused` (boolean) Start annyang in paused mode.
|
|
84
|
+
*
|
|
85
|
+
* #### Examples:
|
|
86
|
+
* ````javascript
|
|
87
|
+
* // Start listening, don't restart automatically
|
|
88
|
+
* annyang.start({ autoRestart: false });
|
|
89
|
+
* // Start listening, don't restart automatically, stop recognition after first phrase recognized
|
|
90
|
+
* annyang.start({ autoRestart: false, continuous: false });
|
|
91
|
+
* ````
|
|
92
|
+
* @param {Object} [options] - Optional options.
|
|
93
|
+
*/
|
|
94
|
+
declare const start: (options?: StartOptions) => void;
|
|
95
|
+
/**
|
|
96
|
+
* Stop listening and turn off the mic.
|
|
97
|
+
*
|
|
98
|
+
* Alternatively, to only temporarily pause annyang responding to commands without stopping the SpeechRecognition engine or closing the mic, use pause() instead.
|
|
99
|
+
* @see [pause()](#pause)
|
|
100
|
+
*/
|
|
101
|
+
declare const abort: () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Pause listening. annyang will stop responding to commands (until the resume or start methods are called), without turning off the browser's SpeechRecognition engine or the mic.
|
|
104
|
+
*
|
|
105
|
+
* Alternatively, to stop the SpeechRecognition engine and close the mic, use abort() instead.
|
|
106
|
+
* @see [abort()](#abort)
|
|
107
|
+
*/
|
|
108
|
+
declare const pause: () => void;
|
|
109
|
+
/**
|
|
110
|
+
* Resumes listening and restore command callback execution when a command is matched.
|
|
111
|
+
* If SpeechRecognition was aborted (stopped), start it.
|
|
112
|
+
*/
|
|
113
|
+
declare const resume: () => void;
|
|
114
|
+
/**
|
|
115
|
+
* Add a callback function to be called in case one of the following events happens:
|
|
116
|
+
*
|
|
117
|
+
* * `start` - Fired as soon as the browser's Speech Recognition engine starts listening.
|
|
118
|
+
*
|
|
119
|
+
* * `soundstart` - Fired as soon as any sound (possibly speech) has been detected.
|
|
120
|
+
*
|
|
121
|
+
* This will fire once per Speech Recognition starting. See https://is.gd/annyang_sound_start.
|
|
122
|
+
*
|
|
123
|
+
* * `error` - Fired when the browser's Speech Recognition engine returns an error, this generic error callback will be followed by more accurate error callbacks (both will fire if both are defined).
|
|
124
|
+
*
|
|
125
|
+
* The Callback function will be called with the error event as the first argument.
|
|
126
|
+
*
|
|
127
|
+
* * `errorNetwork` - Fired when Speech Recognition fails because of a network error.
|
|
128
|
+
*
|
|
129
|
+
* The Callback function will be called with the error event as the first argument.
|
|
130
|
+
*
|
|
131
|
+
* * `errorPermissionBlocked` - Fired when the browser blocks the permission request to use Speech Recognition.
|
|
132
|
+
*
|
|
133
|
+
* The Callback function will be called with the error event as the first argument.
|
|
134
|
+
*
|
|
135
|
+
* * `errorPermissionDenied` - Fired when the user blocks the permission request to use Speech Recognition.
|
|
136
|
+
*
|
|
137
|
+
* The Callback function will be called with the error event as the first argument.
|
|
138
|
+
*
|
|
139
|
+
* * `end` - Fired when the browser's Speech Recognition engine stops.
|
|
140
|
+
*
|
|
141
|
+
* * `result` - Fired as soon as some speech was identified. This generic callback will be followed by either the `resultMatch` or `resultNoMatch` callbacks.
|
|
142
|
+
*
|
|
143
|
+
* The Callback functions for this event will be called with an array of possible phrases the user said as the first argument.
|
|
144
|
+
*
|
|
145
|
+
* * `resultMatch` - Fired when annyang was able to match between what the user said and a registered command.
|
|
146
|
+
*
|
|
147
|
+
* The Callback functions for this event will be called with three arguments in the following order:
|
|
148
|
+
*
|
|
149
|
+
* * The phrase the user said that matched a command.
|
|
150
|
+
* * The command that was matched.
|
|
151
|
+
* * An array of possible alternative phrases the user might have said.
|
|
152
|
+
*
|
|
153
|
+
* * `resultNoMatch` - Fired when what the user said didn't match any of the registered commands.
|
|
154
|
+
*
|
|
155
|
+
* Callback functions for this event will be called with an array of possible phrases the user might have said as the first argument.
|
|
156
|
+
*
|
|
157
|
+
* #### Examples:
|
|
158
|
+
* ````javascript
|
|
159
|
+
* annyang.addCallback('resultMatch', (userSaid, commandText, phrases) => {
|
|
160
|
+
* console.log(userSaid); // sample output: 'hello'
|
|
161
|
+
* console.log(commandText); // sample output: 'hello (there)'
|
|
162
|
+
* console.log(phrases); // sample output: ['hello', 'halo', 'yellow', 'polo', 'hello kitty']
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* // Returns an unsubscribe function
|
|
166
|
+
* const unsubscribe = annyang.addCallback('error', () => {
|
|
167
|
+
* console.log('There was an error!');
|
|
168
|
+
* });
|
|
169
|
+
* unsubscribe(); // removes the callback
|
|
170
|
+
* ````
|
|
171
|
+
* @param {string} type - Name of event that will trigger this callback
|
|
172
|
+
* @param {function} callback - The function to call when event is triggered
|
|
173
|
+
* @param {Object} [context] - Optional context for the callback function
|
|
174
|
+
* @returns {function} A function that removes this callback when called
|
|
175
|
+
*/
|
|
176
|
+
declare const addCallback: <T extends CallbackType>(type: T, callback: CallbackMap[T], context?: object | undefined) => (() => void);
|
|
177
|
+
/**
|
|
178
|
+
* Remove callbacks from events.
|
|
179
|
+
*
|
|
180
|
+
* - Pass an event name and a callback command to remove that callback command from that event type.
|
|
181
|
+
* - Pass just an event name to remove all callback commands from that event type.
|
|
182
|
+
* - Pass undefined as event name and a callback command to remove that callback command from all event types.
|
|
183
|
+
* - Pass no params to remove all callback commands from all event types.
|
|
184
|
+
*
|
|
185
|
+
* #### Examples:
|
|
186
|
+
* ````javascript
|
|
187
|
+
* annyang.addCallback('start', myFunction1);
|
|
188
|
+
* annyang.addCallback('start', myFunction2);
|
|
189
|
+
* annyang.addCallback('end', myFunction1);
|
|
190
|
+
* annyang.addCallback('end', myFunction2);
|
|
191
|
+
*
|
|
192
|
+
* // Remove all callbacks from all events:
|
|
193
|
+
* annyang.removeCallback();
|
|
194
|
+
*
|
|
195
|
+
* // Remove all callbacks attached to end event:
|
|
196
|
+
* annyang.removeCallback('end');
|
|
197
|
+
*
|
|
198
|
+
* // Remove myFunction2 from being called on start:
|
|
199
|
+
* annyang.removeCallback('start', myFunction2);
|
|
200
|
+
*
|
|
201
|
+
* // Remove myFunction1 from being called on all events:
|
|
202
|
+
* annyang.removeCallback(undefined, myFunction1);
|
|
203
|
+
* ````
|
|
204
|
+
*
|
|
205
|
+
* @param type Name of event type to remove callback from
|
|
206
|
+
* @param callback The callback function to remove
|
|
207
|
+
* @returns undefined
|
|
208
|
+
*/
|
|
209
|
+
declare const removeCallback: (type?: CallbackType, callback?: CallbackMap[CallbackType]) => void;
|
|
210
|
+
/**
|
|
211
|
+
* Returns true if speech recognition is currently on.
|
|
212
|
+
* Returns false if speech recognition is off or annyang is paused.
|
|
213
|
+
*
|
|
214
|
+
* @returns true if SpeechRecognition is on and annyang is not paused
|
|
215
|
+
*/
|
|
216
|
+
declare const isListening: () => boolean;
|
|
217
|
+
type AnnyangState = 'idle' | 'listening' | 'paused';
|
|
218
|
+
/**
|
|
219
|
+
* Returns the current state of annyang.
|
|
220
|
+
*
|
|
221
|
+
* @returns {'idle' | 'listening' | 'paused'} The current state
|
|
222
|
+
*/
|
|
223
|
+
declare const getState: () => AnnyangState;
|
|
224
|
+
/**
|
|
225
|
+
* Set the language the user will speak in. If this method is not called, annyang defaults to 'en-US'.
|
|
226
|
+
*
|
|
227
|
+
* @param {string} language - The language (locale)
|
|
228
|
+
* @see [Languages](https://github.com/TalAter/annyang/blob/master/docs/FAQ.md#what-languages-are-supported)
|
|
229
|
+
*/
|
|
230
|
+
declare const setLanguage: (language: string) => void;
|
|
231
|
+
/**
|
|
232
|
+
* Turn on the output of debug messages to the console.
|
|
233
|
+
*
|
|
234
|
+
* @param {boolean} [newState=true] - Turn debug messages on or off
|
|
235
|
+
*/
|
|
236
|
+
declare const debug: (newState?: boolean) => void;
|
|
237
|
+
/**
|
|
238
|
+
* Match text against registered commands and fire the corresponding callbacks.
|
|
239
|
+
* Works independently of the speech recognition engine — does not require `start()`, and works even in
|
|
240
|
+
* environments where SpeechRecognition is not supported.
|
|
241
|
+
*
|
|
242
|
+
* Can accept either a string containing a single sentence or an array containing multiple sentences to be checked
|
|
243
|
+
* in order until one of them matches a command (similar to the way Speech Recognition Alternatives are parsed)
|
|
244
|
+
*
|
|
245
|
+
* #### Examples:
|
|
246
|
+
* ````javascript
|
|
247
|
+
* annyang.trigger('Time for some thrilling heroics');
|
|
248
|
+
* annyang.trigger(
|
|
249
|
+
* ['Time for some thrilling heroics', 'Time for some thrilling aerobics']
|
|
250
|
+
* );
|
|
251
|
+
* ````
|
|
252
|
+
*
|
|
253
|
+
* @param sentences - A sentence as a string or an array of strings of possible sentences
|
|
254
|
+
*/
|
|
255
|
+
declare const trigger: (sentences?: string | string[]) => void;
|
|
256
|
+
/**
|
|
257
|
+
* Returns the instance of the browser's SpeechRecognition object used by annyang.
|
|
258
|
+
* Useful in case you want direct access to the browser's Speech Recognition engine.
|
|
259
|
+
*
|
|
260
|
+
* @returns SpeechRecognition The browser's Speech Recognizer instance currently used by annyang
|
|
261
|
+
*/
|
|
262
|
+
declare const getSpeechRecognizer: () => SpeechRecognition | undefined;
|
|
263
|
+
/**
|
|
264
|
+
* @deprecated annyang no longer requires manual initialization. It initializes automatically on `start()` or `addCommands()`. Remove any calls to `init()`.
|
|
265
|
+
*/
|
|
266
|
+
declare const initDeprecated: () => void;
|
|
267
|
+
|
|
268
|
+
declare const annyang: {
|
|
269
|
+
readonly isSpeechRecognitionSupported: () => boolean;
|
|
270
|
+
readonly addCommands: (commands: CommandsList, resetCommands?: boolean) => void;
|
|
271
|
+
readonly removeCommands: (commandsToRemove?: string | string[] | undefined) => void;
|
|
272
|
+
readonly start: (options?: StartOptions) => void;
|
|
273
|
+
readonly abort: () => void;
|
|
274
|
+
readonly pause: () => void;
|
|
275
|
+
readonly resume: () => void;
|
|
276
|
+
readonly addCallback: <T extends CallbackType>(type: T, callback: CallbackMap[T], context?: object | undefined) => (() => void);
|
|
277
|
+
readonly removeCallback: (type?: CallbackType, callback?: CallbackMap[CallbackType]) => void;
|
|
278
|
+
readonly isListening: () => boolean;
|
|
279
|
+
readonly setLanguage: (language: string) => void;
|
|
280
|
+
readonly trigger: (sentences?: string | string[]) => void;
|
|
281
|
+
readonly debug: (newState?: boolean) => void;
|
|
282
|
+
readonly getSpeechRecognizer: () => SpeechRecognition | undefined;
|
|
283
|
+
readonly getState: () => AnnyangState;
|
|
284
|
+
readonly init: () => void;
|
|
285
|
+
readonly state: AnnyangState;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
export { type AnnyangState, type CallbackMap, type CallbackType, type CommandCallback, type CommandsList, type StartOptions, abort, addCallback, addCommands, debug, annyang as default, getSpeechRecognizer, getState, initDeprecated as init, isListening, isSpeechRecognitionSupported, pause, removeCallback, removeCommands, resume, setLanguage, start, trigger };
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
interface CallbackMap {
|
|
2
|
+
start: () => void;
|
|
3
|
+
end: () => void;
|
|
4
|
+
soundstart: () => void;
|
|
5
|
+
result: (phrases: string[]) => void;
|
|
6
|
+
resultMatch: (userSaid: string, commandText: string, phrases: string[]) => void;
|
|
7
|
+
resultNoMatch: (phrases: string[]) => void;
|
|
8
|
+
error: (event: SpeechRecognitionErrorEvent) => void;
|
|
9
|
+
errorNetwork: (event: SpeechRecognitionErrorEvent) => void;
|
|
10
|
+
errorPermissionBlocked: (event: SpeechRecognitionErrorEvent) => void;
|
|
11
|
+
errorPermissionDenied: (event: SpeechRecognitionErrorEvent) => void;
|
|
12
|
+
}
|
|
13
|
+
type CallbackType = keyof CallbackMap;
|
|
14
|
+
/**
|
|
15
|
+
* Is SpeechRecognition supported in this environment?
|
|
16
|
+
*
|
|
17
|
+
* @returns {boolean} true if SpeechRecognition is supported by the browser
|
|
18
|
+
*/
|
|
19
|
+
declare const isSpeechRecognitionSupported: () => boolean;
|
|
20
|
+
type CommandCallback = (...args: string[]) => void;
|
|
21
|
+
interface CommandsList {
|
|
22
|
+
[key: string]: CommandCallback | {
|
|
23
|
+
regexp: RegExp;
|
|
24
|
+
callback: CommandCallback;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Add commands that annyang will respond to.
|
|
29
|
+
* By default this will add to the existing commands. Pass `true` as the second parameter to remove all existing commands first.
|
|
30
|
+
*
|
|
31
|
+
* #### Examples:
|
|
32
|
+
* ````javascript
|
|
33
|
+
* const commands1 = {'hello :name': helloFunction, 'howdy': helloFunction};
|
|
34
|
+
* const commands2 = {'hi': helloFunction};
|
|
35
|
+
*
|
|
36
|
+
* annyang.addCommands(commands1);
|
|
37
|
+
* annyang.addCommands(commands2);
|
|
38
|
+
* // annyang will now listen for all three commands defined in commands1 and commands2
|
|
39
|
+
*
|
|
40
|
+
* annyang.addCommands(commands2, true);
|
|
41
|
+
* // annyang will now only listen for the command in commands2
|
|
42
|
+
* ````
|
|
43
|
+
*
|
|
44
|
+
* @param {Object} commands - Commands that annyang should listen for
|
|
45
|
+
* @param {boolean} [resetCommands=false] - Remove all existing commands before adding new commands? * @see [Commands Object](#commands-object)
|
|
46
|
+
*/
|
|
47
|
+
declare const addCommands: (commands: CommandsList, resetCommands?: boolean) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Remove existing commands. Called with a single phrase, an array of phrases, or with no params to remove all commands.
|
|
50
|
+
*
|
|
51
|
+
* #### Examples:
|
|
52
|
+
* ````javascript
|
|
53
|
+
* const commands = {'hello': helloFunction, 'howdy': helloFunction, 'hi': helloFunction};
|
|
54
|
+
*
|
|
55
|
+
* // Remove all existing commands
|
|
56
|
+
* annyang.removeCommands();
|
|
57
|
+
*
|
|
58
|
+
* // Add some commands
|
|
59
|
+
* annyang.addCommands(commands);
|
|
60
|
+
*
|
|
61
|
+
* // Don't respond to hello
|
|
62
|
+
* annyang.removeCommands('hello');
|
|
63
|
+
*
|
|
64
|
+
* // Don't respond to howdy or hi
|
|
65
|
+
* annyang.removeCommands(['howdy', 'hi']);
|
|
66
|
+
* ````
|
|
67
|
+
* @param {string|string[]|undefined} [commandsToRemove] - Commands to remove
|
|
68
|
+
*/
|
|
69
|
+
declare const removeCommands: (commandsToRemove?: string | string[] | undefined) => void;
|
|
70
|
+
interface StartOptions {
|
|
71
|
+
autoRestart?: boolean;
|
|
72
|
+
continuous?: boolean;
|
|
73
|
+
paused?: boolean;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Start listening.
|
|
77
|
+
* It's a good idea to call this after adding some commands first (but not mandatory)
|
|
78
|
+
*
|
|
79
|
+
* Receives an optional options object which supports the following options:
|
|
80
|
+
*
|
|
81
|
+
* - `autoRestart` (boolean) Should annyang restart itself if it is closed indirectly (e.g. because of silence or window conflicts)?
|
|
82
|
+
* - `continuous` (boolean) Allow forcing continuous mode on or off. annyang is pretty smart about this, so only set this if you know what you're doing.
|
|
83
|
+
* - `paused` (boolean) Start annyang in paused mode.
|
|
84
|
+
*
|
|
85
|
+
* #### Examples:
|
|
86
|
+
* ````javascript
|
|
87
|
+
* // Start listening, don't restart automatically
|
|
88
|
+
* annyang.start({ autoRestart: false });
|
|
89
|
+
* // Start listening, don't restart automatically, stop recognition after first phrase recognized
|
|
90
|
+
* annyang.start({ autoRestart: false, continuous: false });
|
|
91
|
+
* ````
|
|
92
|
+
* @param {Object} [options] - Optional options.
|
|
93
|
+
*/
|
|
94
|
+
declare const start: (options?: StartOptions) => void;
|
|
95
|
+
/**
|
|
96
|
+
* Stop listening and turn off the mic.
|
|
97
|
+
*
|
|
98
|
+
* Alternatively, to only temporarily pause annyang responding to commands without stopping the SpeechRecognition engine or closing the mic, use pause() instead.
|
|
99
|
+
* @see [pause()](#pause)
|
|
100
|
+
*/
|
|
101
|
+
declare const abort: () => void;
|
|
102
|
+
/**
|
|
103
|
+
* Pause listening. annyang will stop responding to commands (until the resume or start methods are called), without turning off the browser's SpeechRecognition engine or the mic.
|
|
104
|
+
*
|
|
105
|
+
* Alternatively, to stop the SpeechRecognition engine and close the mic, use abort() instead.
|
|
106
|
+
* @see [abort()](#abort)
|
|
107
|
+
*/
|
|
108
|
+
declare const pause: () => void;
|
|
109
|
+
/**
|
|
110
|
+
* Resumes listening and restore command callback execution when a command is matched.
|
|
111
|
+
* If SpeechRecognition was aborted (stopped), start it.
|
|
112
|
+
*/
|
|
113
|
+
declare const resume: () => void;
|
|
114
|
+
/**
|
|
115
|
+
* Add a callback function to be called in case one of the following events happens:
|
|
116
|
+
*
|
|
117
|
+
* * `start` - Fired as soon as the browser's Speech Recognition engine starts listening.
|
|
118
|
+
*
|
|
119
|
+
* * `soundstart` - Fired as soon as any sound (possibly speech) has been detected.
|
|
120
|
+
*
|
|
121
|
+
* This will fire once per Speech Recognition starting. See https://is.gd/annyang_sound_start.
|
|
122
|
+
*
|
|
123
|
+
* * `error` - Fired when the browser's Speech Recognition engine returns an error, this generic error callback will be followed by more accurate error callbacks (both will fire if both are defined).
|
|
124
|
+
*
|
|
125
|
+
* The Callback function will be called with the error event as the first argument.
|
|
126
|
+
*
|
|
127
|
+
* * `errorNetwork` - Fired when Speech Recognition fails because of a network error.
|
|
128
|
+
*
|
|
129
|
+
* The Callback function will be called with the error event as the first argument.
|
|
130
|
+
*
|
|
131
|
+
* * `errorPermissionBlocked` - Fired when the browser blocks the permission request to use Speech Recognition.
|
|
132
|
+
*
|
|
133
|
+
* The Callback function will be called with the error event as the first argument.
|
|
134
|
+
*
|
|
135
|
+
* * `errorPermissionDenied` - Fired when the user blocks the permission request to use Speech Recognition.
|
|
136
|
+
*
|
|
137
|
+
* The Callback function will be called with the error event as the first argument.
|
|
138
|
+
*
|
|
139
|
+
* * `end` - Fired when the browser's Speech Recognition engine stops.
|
|
140
|
+
*
|
|
141
|
+
* * `result` - Fired as soon as some speech was identified. This generic callback will be followed by either the `resultMatch` or `resultNoMatch` callbacks.
|
|
142
|
+
*
|
|
143
|
+
* The Callback functions for this event will be called with an array of possible phrases the user said as the first argument.
|
|
144
|
+
*
|
|
145
|
+
* * `resultMatch` - Fired when annyang was able to match between what the user said and a registered command.
|
|
146
|
+
*
|
|
147
|
+
* The Callback functions for this event will be called with three arguments in the following order:
|
|
148
|
+
*
|
|
149
|
+
* * The phrase the user said that matched a command.
|
|
150
|
+
* * The command that was matched.
|
|
151
|
+
* * An array of possible alternative phrases the user might have said.
|
|
152
|
+
*
|
|
153
|
+
* * `resultNoMatch` - Fired when what the user said didn't match any of the registered commands.
|
|
154
|
+
*
|
|
155
|
+
* Callback functions for this event will be called with an array of possible phrases the user might have said as the first argument.
|
|
156
|
+
*
|
|
157
|
+
* #### Examples:
|
|
158
|
+
* ````javascript
|
|
159
|
+
* annyang.addCallback('resultMatch', (userSaid, commandText, phrases) => {
|
|
160
|
+
* console.log(userSaid); // sample output: 'hello'
|
|
161
|
+
* console.log(commandText); // sample output: 'hello (there)'
|
|
162
|
+
* console.log(phrases); // sample output: ['hello', 'halo', 'yellow', 'polo', 'hello kitty']
|
|
163
|
+
* });
|
|
164
|
+
*
|
|
165
|
+
* // Returns an unsubscribe function
|
|
166
|
+
* const unsubscribe = annyang.addCallback('error', () => {
|
|
167
|
+
* console.log('There was an error!');
|
|
168
|
+
* });
|
|
169
|
+
* unsubscribe(); // removes the callback
|
|
170
|
+
* ````
|
|
171
|
+
* @param {string} type - Name of event that will trigger this callback
|
|
172
|
+
* @param {function} callback - The function to call when event is triggered
|
|
173
|
+
* @param {Object} [context] - Optional context for the callback function
|
|
174
|
+
* @returns {function} A function that removes this callback when called
|
|
175
|
+
*/
|
|
176
|
+
declare const addCallback: <T extends CallbackType>(type: T, callback: CallbackMap[T], context?: object | undefined) => (() => void);
|
|
177
|
+
/**
|
|
178
|
+
* Remove callbacks from events.
|
|
179
|
+
*
|
|
180
|
+
* - Pass an event name and a callback command to remove that callback command from that event type.
|
|
181
|
+
* - Pass just an event name to remove all callback commands from that event type.
|
|
182
|
+
* - Pass undefined as event name and a callback command to remove that callback command from all event types.
|
|
183
|
+
* - Pass no params to remove all callback commands from all event types.
|
|
184
|
+
*
|
|
185
|
+
* #### Examples:
|
|
186
|
+
* ````javascript
|
|
187
|
+
* annyang.addCallback('start', myFunction1);
|
|
188
|
+
* annyang.addCallback('start', myFunction2);
|
|
189
|
+
* annyang.addCallback('end', myFunction1);
|
|
190
|
+
* annyang.addCallback('end', myFunction2);
|
|
191
|
+
*
|
|
192
|
+
* // Remove all callbacks from all events:
|
|
193
|
+
* annyang.removeCallback();
|
|
194
|
+
*
|
|
195
|
+
* // Remove all callbacks attached to end event:
|
|
196
|
+
* annyang.removeCallback('end');
|
|
197
|
+
*
|
|
198
|
+
* // Remove myFunction2 from being called on start:
|
|
199
|
+
* annyang.removeCallback('start', myFunction2);
|
|
200
|
+
*
|
|
201
|
+
* // Remove myFunction1 from being called on all events:
|
|
202
|
+
* annyang.removeCallback(undefined, myFunction1);
|
|
203
|
+
* ````
|
|
204
|
+
*
|
|
205
|
+
* @param type Name of event type to remove callback from
|
|
206
|
+
* @param callback The callback function to remove
|
|
207
|
+
* @returns undefined
|
|
208
|
+
*/
|
|
209
|
+
declare const removeCallback: (type?: CallbackType, callback?: CallbackMap[CallbackType]) => void;
|
|
210
|
+
/**
|
|
211
|
+
* Returns true if speech recognition is currently on.
|
|
212
|
+
* Returns false if speech recognition is off or annyang is paused.
|
|
213
|
+
*
|
|
214
|
+
* @returns true if SpeechRecognition is on and annyang is not paused
|
|
215
|
+
*/
|
|
216
|
+
declare const isListening: () => boolean;
|
|
217
|
+
type AnnyangState = 'idle' | 'listening' | 'paused';
|
|
218
|
+
/**
|
|
219
|
+
* Returns the current state of annyang.
|
|
220
|
+
*
|
|
221
|
+
* @returns {'idle' | 'listening' | 'paused'} The current state
|
|
222
|
+
*/
|
|
223
|
+
declare const getState: () => AnnyangState;
|
|
224
|
+
/**
|
|
225
|
+
* Set the language the user will speak in. If this method is not called, annyang defaults to 'en-US'.
|
|
226
|
+
*
|
|
227
|
+
* @param {string} language - The language (locale)
|
|
228
|
+
* @see [Languages](https://github.com/TalAter/annyang/blob/master/docs/FAQ.md#what-languages-are-supported)
|
|
229
|
+
*/
|
|
230
|
+
declare const setLanguage: (language: string) => void;
|
|
231
|
+
/**
|
|
232
|
+
* Turn on the output of debug messages to the console.
|
|
233
|
+
*
|
|
234
|
+
* @param {boolean} [newState=true] - Turn debug messages on or off
|
|
235
|
+
*/
|
|
236
|
+
declare const debug: (newState?: boolean) => void;
|
|
237
|
+
/**
|
|
238
|
+
* Match text against registered commands and fire the corresponding callbacks.
|
|
239
|
+
* Works independently of the speech recognition engine — does not require `start()`, and works even in
|
|
240
|
+
* environments where SpeechRecognition is not supported.
|
|
241
|
+
*
|
|
242
|
+
* Can accept either a string containing a single sentence or an array containing multiple sentences to be checked
|
|
243
|
+
* in order until one of them matches a command (similar to the way Speech Recognition Alternatives are parsed)
|
|
244
|
+
*
|
|
245
|
+
* #### Examples:
|
|
246
|
+
* ````javascript
|
|
247
|
+
* annyang.trigger('Time for some thrilling heroics');
|
|
248
|
+
* annyang.trigger(
|
|
249
|
+
* ['Time for some thrilling heroics', 'Time for some thrilling aerobics']
|
|
250
|
+
* );
|
|
251
|
+
* ````
|
|
252
|
+
*
|
|
253
|
+
* @param sentences - A sentence as a string or an array of strings of possible sentences
|
|
254
|
+
*/
|
|
255
|
+
declare const trigger: (sentences?: string | string[]) => void;
|
|
256
|
+
/**
|
|
257
|
+
* Returns the instance of the browser's SpeechRecognition object used by annyang.
|
|
258
|
+
* Useful in case you want direct access to the browser's Speech Recognition engine.
|
|
259
|
+
*
|
|
260
|
+
* @returns SpeechRecognition The browser's Speech Recognizer instance currently used by annyang
|
|
261
|
+
*/
|
|
262
|
+
declare const getSpeechRecognizer: () => SpeechRecognition | undefined;
|
|
263
|
+
/**
|
|
264
|
+
* @deprecated annyang no longer requires manual initialization. It initializes automatically on `start()` or `addCommands()`. Remove any calls to `init()`.
|
|
265
|
+
*/
|
|
266
|
+
declare const initDeprecated: () => void;
|
|
267
|
+
|
|
268
|
+
declare const annyang: {
|
|
269
|
+
readonly isSpeechRecognitionSupported: () => boolean;
|
|
270
|
+
readonly addCommands: (commands: CommandsList, resetCommands?: boolean) => void;
|
|
271
|
+
readonly removeCommands: (commandsToRemove?: string | string[] | undefined) => void;
|
|
272
|
+
readonly start: (options?: StartOptions) => void;
|
|
273
|
+
readonly abort: () => void;
|
|
274
|
+
readonly pause: () => void;
|
|
275
|
+
readonly resume: () => void;
|
|
276
|
+
readonly addCallback: <T extends CallbackType>(type: T, callback: CallbackMap[T], context?: object | undefined) => (() => void);
|
|
277
|
+
readonly removeCallback: (type?: CallbackType, callback?: CallbackMap[CallbackType]) => void;
|
|
278
|
+
readonly isListening: () => boolean;
|
|
279
|
+
readonly setLanguage: (language: string) => void;
|
|
280
|
+
readonly trigger: (sentences?: string | string[]) => void;
|
|
281
|
+
readonly debug: (newState?: boolean) => void;
|
|
282
|
+
readonly getSpeechRecognizer: () => SpeechRecognition | undefined;
|
|
283
|
+
readonly getState: () => AnnyangState;
|
|
284
|
+
readonly init: () => void;
|
|
285
|
+
readonly state: AnnyangState;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
export { type AnnyangState, type CallbackMap, type CallbackType, type CommandCallback, type CommandsList, type StartOptions, abort, addCallback, addCommands, debug, annyang as default, getSpeechRecognizer, getState, initDeprecated as init, isListening, isSpeechRecognitionSupported, pause, removeCallback, removeCommands, resume, setLanguage, start, trigger };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var annyang=(()=>{var R=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var W=(e,t)=>{for(var n in t)R(e,n,{get:t[n],enumerable:!0})},U=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of B(t))!G.call(e,s)&&s!==n&&R(e,s,{get:()=>t[s],enumerable:!(o=F(t,s))||o.enumerable});return e};var q=e=>U(R({},"__esModule",{value:!0}),e);var ne={};W(ne,{abort:()=>N,addCallback:()=>L,addCommands:()=>w,debug:()=>O,default:()=>te,getSpeechRecognizer:()=>j,getState:()=>C,init:()=>z,isListening:()=>P,isSpeechRecognitionSupported:()=>h,pause:()=>_,removeCallback:()=>$,removeCommands:()=>M,resume:()=>I,setLanguage:()=>D,start:()=>m,trigger:()=>V});var r,b=!1,u=!0,E=!1,p="font-weight: bold; color: #00f;",f=new Map,a=new Map([["start",[]],["error",[]],["end",[]],["soundstart",[]],["result",[]],["resultMatch",[]],["resultNoMatch",[]],["errorNetwork",[]],["errorPermissionBlocked",[]],["errorPermissionDenied",[]]]),S=0,k=0,l=!1,H=/\s*\((.*?)\)\s*/g,J=/(\(\?:[^)]+\))\?/g,K=/(\(\?)?:\w+/g,Q=/\*\w+/g,X=/[-{}[\]+?.,\\^$|#]/g,Y=e=>{let t=e.replace(X,"\\$&").replace(H,"(?:$1)?").replace(K,(n,o)=>o?n:"([^\\s]+)").replace(Q,"(.*?)").replace(J,"\\s*$1?\\s*");return new RegExp(`^${t}$`,"i")},y=()=>globalThis.SpeechRecognition||globalThis.webkitSpeechRecognition,x=()=>r!==void 0,c=(e,t)=>{E&&(e.indexOf("%c")===-1&&!t?console.log(e):console.log(e,t||p))},T=(e,t,n)=>{f.set(n,{command:e,callback:t}),c(`Command successfully loaded: %c${n}`,p)},i=(e=[],...t)=>{e.forEach(n=>{n.callback.apply(n.context,t)})},Z=()=>{y()&&(r&&r.abort&&r.abort(),r=new(y()),r.maxAlternatives=5,r.continuous=globalThis.location.protocol==="http:",r.lang="en-US",r.onstart=()=>{b=!0,i(a.get("start"))},r.onsoundstart=()=>{i(a.get("soundstart"))},r.onerror=e=>{switch(i(a.get("error"),e),e.error){case"network":i(a.get("errorNetwork"),e);break;case"not-allowed":case"service-not-allowed":u=!1,new Date().getTime()-S<200?i(a.get("errorPermissionBlocked"),e):i(a.get("errorPermissionDenied"),e);break;default:break}},r.onend=()=>{if(b=!1,i(a.get("end")),u){let e=new Date().getTime()-S;k+=1,k%10===0&&c("Speech Recognition is repeatedly stopping and starting. See http://is.gd/annyang_restarts for tips."),e<1e3?setTimeout(()=>{m({paused:l})},1e3-e):m({paused:l})}},r.onresult=e=>{if(l){c("Speech heard, but annyang is paused");return}let t=e.results[e.resultIndex],n=Array.from(t,o=>o.transcript);v(n)})},A=()=>{x()||Z()},v=e=>{i(a.get("result"),e);for(let t of e)c(`Speech recognized: %c${t.trim()}`,p);for(let t of e){let n=t.trim();for(let[o,s]of f){let d=s.command.exec(n);if(d){let g=d.slice(1);c(`command matched: %c${o}`,p),g.length&&c("with parameters",g),s.callback(...g),i(a.get("resultMatch"),n,o,e);return}}}i(a.get("resultNoMatch"),e)},h=()=>!!y(),w=(e,t=!1)=>{t&&f.clear();for(let n of Object.keys(e)){let o=e[n];typeof o=="function"?T(Y(n),o,n):typeof o=="object"&&o.regexp instanceof RegExp?T(new RegExp(o.regexp.source,"i"),o.callback,n):c(`Can not register command: %c${n}`,p)}},M=e=>{e===void 0?f.clear():(Array.isArray(e)?e:[e]).forEach(n=>f.delete(n))},m=(e={})=>{if(h()){A(),l=!!e.paused,e.autoRestart!==void 0?u=!!e.autoRestart:u=!0,e.continuous!==void 0&&(r.continuous=!!e.continuous),S=new Date().getTime();try{r.start()}catch(t){c(t instanceof Error?t.message:String(t))}}},N=()=>{u=!1,k=0,x()&&r.abort()},_=()=>{l=!0},I=()=>{m()},L=(e,t,n=void 0)=>{let o=a.get(e);if(typeof t=="function"&&o){let s={callback:t,context:n};return o.push(s),()=>{let d=a.get(e);if(d){let g=d.indexOf(s);g!==-1&&d.splice(g,1)}}}return()=>{}},$=(e,t)=>{a.forEach((n,o)=>{(e===void 0||e===o)&&(t===void 0?a.get(o).length=0:a.set(o,n.filter(s=>s.callback!==t)))})},P=()=>b&&!l,C=()=>b?l?"paused":"listening":"idle",D=e=>{h()&&(A(),r.lang=e)},O=(e=!0)=>{E=!!e},V=(e=[])=>{v(Array.isArray(e)?e:[e])},j=()=>r,z=()=>{console.warn("annyang.init() is deprecated and no longer needed. annyang initializes automatically on start() or addCommands(). Remove this call.")};var ee={isSpeechRecognitionSupported:h,addCommands:w,removeCommands:M,start:m,abort:N,pause:_,resume:I,addCallback:L,removeCallback:$,isListening:P,setLanguage:D,trigger:V,debug:O,getSpeechRecognizer:j,getState:C,init:z,get state(){return C()}},te=ee;return q(ne);})();
|