-
Notifications
You must be signed in to change notification settings - Fork 81
/
voix.js
208 lines (171 loc) · 4.95 KB
/
voix.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
(function (window) {
'use strict';
var VoiceRecognition = window.webkitSpeechRecognition;
/**
* Voix Constructor.
* @constructor
* @params {String} lang - A given language.
* @returns {voix} Returns a new instance of Voix.
*/
function Voix(lang) {
this._lang = lang;
this.initialize();
}
/**
* Initializes a new instance of Voix.
* @function
* @returns {voix} Returns a new instance of Voix.
*/
Voix.prototype.initialize = function () {
this._collection = {};
this._createRecognition();
this._bindKeyEvents();
return this;
};
/**
* Creates a new instance of VoiceRecognition.
* @function
* @private
* @returns {voix}
*/
Voix.prototype._createRecognition = function () {
this._recognition = new VoiceRecognition();
this._recognition.continuous = true;
this._recognition.interimResults = false;
this._recognition.lang = this._lang || 'en-US';
this._recognition.maxAlternatives = 1;
this._recognition.onresult = undefined;
this._recognition.start();
return this;
};
/**
* Binds key events.
* @function
* @private
* @returns {voix}
*/
Voix.prototype._bindKeyEvents = function () {
var that = this,
keypress = false;
document.addEventListener('keydown', function (eve) {
if (!keypress && eve.keyCode === 86) {
that.start();
keypress = true;
}
});
document.addEventListener('keyup', function (eve) {
if (keypress && eve.keyCode === 86) {
keypress = false;
}
});
return this;
};
/**
* Transcripts commands.
* @function
* @private
* @returns {voix}
*/
Voix.prototype._result = function (eve) {
this.stop();
var that = this,
i = eve.resultIndex,
len = eve.results.length,
j = 0,
listeners,
lenListeners,
command;
for (i; i < len; i += 1) {
if (eve.results[i].isFinal) {
command = eve.results[i][0].transcript.replace(/^\s+|\s+$/g, '').toLowerCase();
if (that._collection[command]) {
listeners = that._collection[command].listeners;
lenListeners = listeners.length;
for (j; j < lenListeners; j += 1) {
listeners[j].call();
}
}
}
}
};
/**
* Sets a new command with a listener to the collection.
* @memberof! Voix.prototype
* @function
* @param {String} [command] - A given command.
* @param {Funtion} listener - A given listener.
* @returns {voix}
*/
Voix.prototype.setCommand = function (command, listener) {
if (this._collection[command] === undefined) {
this._collection[command] = {
'listeners': []
};
}
this._collection[command].listeners.push(listener);
return this;
};
/**
* Rmoves a given command or its listener from the collection.
* @memberof! Voix.prototype
* @function
* @param {String} [command] - A given command.
* @param {Funtion} listener - A given listener.
* @returns {voix}
*/
Voix.prototype.removeCommand = function (command, listener) {
var listeners = this._collection[command].listeners,
i = 0,
len = listeners.length;
if (len !== undefined) {
for (i; i < len; i += 1) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
break;
}
}
}
if (listeners.length === 0 || listener === undefined) {
delete this._collection[command];
}
return this;
};
/**
* Starts the recognition.
* @memberof! Voix.prototype
* @function
* @returns {voix}
*/
Voix.prototype.start = function () {
var that = this;
this._recognition.onresult = function (eve) {
that._result.call(that, eve);
};
return this;
};
/**
* Stops the recognition.
* @memberof! Voix.prototype
* @function
* @returns {voix}
*/
Voix.prototype.stop = function () {
this._recognition.onresult = undefined;
return this;
};
/**
* Expose Voix
*/
// AMD suppport
if (typeof window.define === 'function' && window.define.amd !== undefined) {
window.define('Voix', [], function () {
return Voix;
});
// CommonJS suppport
} else if (typeof module !== 'undefined' && module.exports !== undefined) {
module.exports = Voix;
// Default
} else {
window.Voix = Voix;
}
}(this));