-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
149 lines (129 loc) · 4.22 KB
/
index.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
var TiSpeech = require('ti.speech');
TiSpeech.initialize();
var canRecordAudio = false;
var canUseSpeechRecognition = false;
var isRunning = false;
if (!TiSpeech.isAvailable()) {
alert('Speech recognition is not available on this device!');
} else {
TiSpeech.requestSpeechRecognizerAuthorization(function(e) {
canUseSpeechRecognition = !!e.success;
if (!e.success) {
alert("Speech recognition was not authorized!");
} else {
TiSpeech.requestMicrophoneAuthorization(function(e) {
canRecordAudio = !!e.success;
if (!e.success) {
alert("Permission to record audio was not authorized!");
}
enableButtons();
});
}
});
}
/**
* @function enableButtons
* @summary Enable buttons based on permissions granted by user
* @since 1.0.0
*/
function enableButtons() {
canUseSpeechRecognition && canRecordAudio && $.toggleLiveRecognitionButton.setEnabled(true);
canUseSpeechRecognition && $.toggleAudioRecognitionButton.setEnabled(true);
canUseSpeechRecognition && $.toggleVideoRecognitionButton.setEnabled(true);
}
/**
* @function stopRecognition
* @summary Stops voice recognition and resets buttons
* @since 1.0.0
*/
function stopRecognition() {
if (isRunning) {
TiSpeech.stopRecognition();
}
isRunning = false;
$.toggleLiveRecognitionButton.title = 'Start Listening to Live Audio';
$.toggleAudioRecognitionButton.title = 'Start Listening to Audio File';
$.toggleVideoRecognitionButton.title = 'Start Listening to Video File';
$.toggleLiveRecognitionButton.setEnabled(true);
$.toggleAudioRecognitionButton.setEnabled(true);
$.toggleVideoRecognitionButton.setEnabled(true);
}
/**
* @function progressCallback
* @summary Function used to report progress of speech recognition
* @param {object} result - Resulting progress object
* @param {boolean} result.finished - Indicates if the speech recognition has completed
* @param {object} result.error - If an error occurred, this parameter will contain the error information
* @param {string} result.value - Transcript of the recognized speech
* @since 1.0.0
*/
function progressCallback(result) {
if (result.error) {
console.error('An error occurred with speech recognition');
console.error(result.error);
if (result.error.toString().match(/Timeout/g)) {
alert('Time limit exceeded for speech recognition');
} else {
alert('An error occurred with speech recognition');
}
stopRecognition();
return;
} else {
$.results.setText(result.value);
}
if (result.finished) {
isRunning = false;
stopRecognition();
}
}
function toggleLiveRecognition(e) {
TiSpeech.initialize();
if (isRunning) {
stopRecognition();
} else {
$.results.setText('Listening...');
var success = TiSpeech.startRecognition({
progress: progressCallback,
});
if (success) {
isRunning = true;
$.toggleLiveRecognitionButton.title = 'Stop Listening';
}
}
}
function toggleAudioRecognition(e) {
// Initializing with locale 'en_US' because audio is in English
TiSpeech.initialize('en_US');
if (isRunning) {
stopRecognition();
} else {
$.results.setText('Loading Audio File...');
var success = TiSpeech.startRecognition({
type: TiSpeech.SOURCE_TYPE_URL,
url: 'one_more_thing.mp3',
progress: progressCallback,
});
if (success) {
isRunning = true;
$.toggleAudioRecognitionButton.title = 'Stop Listening';
}
}
}
function toggleVideoRecognition(e) {
TiSpeech.initialize('en_GB');
if (isRunning) {
stopRecognition();
} else {
$.results.setText('Loading Video File...');
var success = TiSpeech.startRecognition({
type: TiSpeech.SOURCE_TYPE_URL,
url: 'hyperloop.mp4',
progress: progressCallback,
});
if (success) {
isRunning = true;
$.toggleVideoRecognitionButton.title = 'Stop Listening';
}
}
}
$.index.open();