-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
274 lines (219 loc) · 7.38 KB
/
script.js
File metadata and controls
274 lines (219 loc) · 7.38 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
let SpeechRecognition;
let recognition;
try {
SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SpeechRecognition();
} catch (e) {
console.error(e);
$('.no-browser-support').show();
$('.app').hide();
}
let noteTextarea = $('#note-textarea');
let instructions = $('#recording-instructions');
let notesList = $('ul#notes');
let logDiv = $('#log');
let noteContent = '';
// Get all notes from previous sessions and display them.
let notes = getAllNotes();
renderNotes(notes);
const MessageType = {
PRIMARY: "primary",
WARNING: "warning",
SUCCESS: "success",
DANGER: "danger",
INFO: "info"
};
function log(level, msg) {
logDiv.removeClass().addClass("alert alert-" + level);
instructions.text(msg)
}
/*-----------------------------
Voice Recognition
------------------------------*/
// If false, the recording will stop after a few seconds of silence.
// When true, the silence period is longer (about 15 seconds),
// allowing us to keep recording even when the user pauses.
recognition.continuous = false;
// This block is called every time the Speech APi captures a line.
recognition.onresult = function (event) {
// event is a SpeechRecognitionEvent object.
// It holds all the lines we have captured so far.
// We only need the current one.
const current = event.resultIndex;
// Get a transcript of what was said.
const transcript = event.results[current][0].transcript;
// Add the current transcript to the contents of our Note.
// There is a weird bug on mobile, where everything is repeated twice.
// There is no official solution so far so we have to handle an edge case.
const mobileRepeatBug = (current === 1 && transcript === event.results[0][0].transcript);
if (!mobileRepeatBug) {
noteContent += transcript;
noteTextarea.val(noteContent);
}
setState(states.STOPPED);
};
recognition.onstart = function () {
console.log('onstart');
log(MessageType.SUCCESS, 'Voice recognition activated. Try speaking into the microphone.');
};
recognition.onsoundstart = function () {
console.log('onsoundstart');
log(MessageType.SUCCESS, 'Voice recognition activated. Try speaking into the microphone.');
};
recognition.onaudiostart = function () {
console.log('onaudiostart');
log(MessageType.SUCCESS, 'Voice recognition activated. Try speaking into the microphone.');
};
recognition.onspeechstart = function () {
console.log('onspeechstart');
log(MessageType.SUCCESS, "Speech Detected! Keep speaking!");
};
recognition.onend = function () {
console.log('onend');
log(MessageType.WARNING, 'Press the button to start again');
};
recognition.onsoundend = function () {
console.log('onsoundend');
log(MessageType.SUCCESS, 'Voice recognition activated. Try speaking into the microphone.');
};
recognition.onaudioend = function () {
console.log('onaudioend');
log(MessageType.SUCCESS, 'Voice recognition activated. Try speaking into the microphone.');
};
recognition.onspeechend = function () {
console.log('onspeechend');
onRecEnd();
log(MessageType.INFO, 'Speech Ended');
};
recognition.onerror = function (event) {
console.log('onerror');
if (event.error === 'no-speech') {
log(MessageType.DANGER, 'No speech was detected. Try again.');
}
onRecEnd()
};
recognition.onnomatch = function (event) {
console.log('onnomatch');
if (event.error === 'no-speech') {
log(MessageType.DANGER, 'No speech was detected. Try again.');
}
onRecEnd()
};
/*-----------------------------
App buttons and input
------------------------------*/
const mainBtn = $('#main-btn');
let isRecOn = false;
function onRecStart() {
isRecOn = true;
mainBtn.removeClass('btn-warning btn-success').addClass('btn-danger');
mainBtn.html('Stop Recording');
if (noteContent.length) {
noteContent += ' ';
}
recognition.start();
}
function onRecEnd() {
recognition.stop();
isRecOn = false;
mainBtn.removeClass('btn-danger btn-warning').addClass('btn-success');
mainBtn.html('Start Recording');
}
mainBtn.on('click', function (e) {
if (!isRecOn) {
onRecStart();
} else {
onRecEnd();
log(MessageType.WARNING, 'Voice recognition paused.');
}
});
// Sync the text inside the text area with the noteContent variable.
noteTextarea.on('input', function () {
noteContent = $(this).val();
});
$('#save-note-btn').on('click', function (e) {
recognition.stop();
if (!noteContent.length) {
log(MessageType.DANGER, 'Could not save non-numeric data');
} else {
// Save note to localStorage.
// The key is the dateTime with seconds, the value is the content of the note.
saveNote(new Date().toLocaleString(), noteContent);
// Reset variables and update UI.
noteContent = '';
renderNotes(getAllNotes());
noteTextarea.val('');
log(MessageType.SUCCESS, 'Note saved successfully.');
}
});
notesList.on('click', function (e) {
e.preventDefault();
var target = $(e.target);
// Listen to the selected note.
if (target.hasClass('listen-note')) {
var content = target.closest('.note').find('.content').text();
readOutLoud(content);
}
// Delete note.
if (target.hasClass('delete-note')) {
var dateTime = target.siblings('.date').text();
deleteNote(dateTime);
target.closest('.note').remove();
}
});
/*-----------------------------
Speech Synthesis
------------------------------*/
function readOutLoud(message) {
let speech = new SpeechSynthesisUtterance();
// Set the text and voice attributes.
speech.text = message;
speech.volume = 1;
speech.rate = 1;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
}
/*-----------------------------
Helper Functions
------------------------------*/
function renderNotes(notes) {
let html = '';
if (notes.length) {
notes.forEach(function (note) {
html +=
`<li class="note d-flex justify-content-between align-items-center">
<p style="display:none" class="date">${note.date}</p>
<p class="content">${note.content}</p>
<div>
<!-- <i class="fas fa-play listen-note"></i>-->
<!-- <i class="fas fa-times delete-note"></i> -->
<button class="btn btn-success fas fa-play listen-note"></button>
<button class="btn btn-danger fas fa-times delete-note"></button>
</div>
</li>`;
});
} else {
html = '<li><p class="content">You don\'t have any notes yet.</p></li>';
}
notesList.html(html);
}
function saveNote(dateTime, content) {
localStorage.setItem('note-' + dateTime, content);
}
function getAllNotes() {
var notes = [];
var key;
for (var i = 0; i < localStorage.length; i++) {
key = localStorage.key(i);
if (key.substring(0, 5) === 'note-') {
notes.push({
date: key.replace('note-', ''),
content: localStorage.getItem(localStorage.key(i))
});
}
}
return notes;
}
function deleteNote(dateTime) {
localStorage.removeItem('note-' + dateTime);
}