-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspeechRecognition.js
110 lines (85 loc) · 3.91 KB
/
speechRecognition.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
"use strict";
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function Speech() {
var _this = this;
if ('webkitSpeechRecognition' in window) {
console.assert(this.constructor.name, 'Speech', 'Error: this is not Speech');
console.dir(this);
this.recognition = new webkitSpeechRecognition();
console.log('webkitSpeechRecognition is available.'); // custom settings
this.recognizing = false;
this.finalTranscript = '';
this.interimTranscript = ''; // speech recognition settings
this.recognition.continuous = true;
this.language = config.speechTranslator.defaultSrcLang.code;
this.startSpeechCapture = function () {
console.assert(_this.constructor.name === 'Speech', 'Expected Speech object but "this" failed ');
console.log('startSpeechCapture');
if (_this.recognizing) {
_this.recognition.stop();
_this.recognizing = false;
}
console.log("initiate language change from - ".concat(_this.recognition.lang, " to ").concat(_this.language));
_this.recognition.lang = _this.language;
_this.recognition.start();
_this.recognizing = true;
};
this.stopSpeechCapture = function () {
console.assert(_this.constructor.name === 'Speech', 'Expected Speech object but "this" failed ');
console.dir(_this);
console.log('stopSpeechCapture');
_this.recognizing = false;
_this.recognition.stop();
};
this.recognition.onstart = function () {
console.assert(_this.constructor.name === 'Speech', 'Expected Speech object but "this" failed ');
console.dir(_this);
console.log('Started...');
};
this.recognition.onend = function (event) {
console.assert(_this.constructor.name === 'Speech', 'Expected Speech object but "this" failed ');
console.dir(_this); // if recognition is ended because of idle time, resume recognition. We want to end recognition only if it is explicitly stopped by user.
console.log('end...');
console.log("this.recognizing - ".concat(_this.recognizing));
if (_this.recognizing) {
_this.recognition.start();
}
};
this.recognition.onresult = function (event) {
console.assert(_this.constructor.name === 'Speech', 'Expected Speech object but "this" failed ');
console.log('result');
if (_typeof(event.results) === undefined) {
console.log('Undefined results'); // this.stopSpeechCapture();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
_this.finalTranscript = event.results[i][0].transcript;
}
}
_this.finalTranscript = function (transcript) {
if (transcript === undefined || transcript === '') {
return '';
}
var firstChar = /\S/;
return transcript.replace(firstChar, function capitalize(match) {
return match.toUpperCase();
});
}(_this.finalTranscript);
document.dispatchEvent(new CustomEvent('speechRecognitionResult', {
bubbles: true,
detail: {
finalTranscript: _this.finalTranscript
}
}));
};
this.recognition.onerror = function (event) {
console.log(event.error);
};
} else {
console.log('webkitSpeechRecognition is not available.');
document.querySelector('[data-section="translateSpeech"] article').remove();
document.querySelector('[data-section="translateSpeech"] .micContainer').remove();
document.querySelector('[data-section="translateSpeech"] .message').textContent = 'Please use latest Chrome browser to use speech recognition feature';
}
}