-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice-rec.js
97 lines (93 loc) · 3.38 KB
/
voice-rec.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
var VoiceRec = (function(){
var startBtn = document.getElementById('start'),
stopBtn = document.getElementById('stop'),
final_span = document.getElementById('transcription'),
interim_span = document.getElementById('intern'),
recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = true;
recognition.lang = 'es-AR';
//recognition.lang = 'en-GB';
recognition.start();
stopBtn.onclick = function(){
recognition.stop();
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') {
console.log('info_no_speech');
ignore_onend = false;
}
if (event.error == 'audio-capture') {
console.log('info_no_microphone');
ignore_onend = false;
}
if (event.error == 'not-allowed') {
if (event.timeStamp - start_timestamp < 100) {
console.log('info_blocked');
} else {
console.log('info_denied');
}
ignore_onend = false;
}
};
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}
var first_char = /\S/;
function capitalize(s) {
return s.replace(first_char, function(m) { return m.toUpperCase(); });
}
var final_transcript = '';
var currentWord = "hola";
recognition.onresult = function(event) {
/*console.log("exxxx");
var interim_transcript = '';*/
for (var i = event.resultIndex; i < event.results.length; ++i) {
var thisword = event.results[i][0].transcript.toLowerCase();
if(thisword.indexOf("hola") >= 0 && currentWord === "hola"){
main.addClass('container--active');
Materialize.toast('Hoooooola! :)', 3000);
Speech.talk("Hola");
$('.clockycontainer').css('background-image', "url('svg/despiertonegro.png')");
currentWord = "agenda";
} else if(thisword.indexOf("agenda") >= 0 && currentWord === "agenda"){
Materialize.toast("¿Cuándo?", 3000);
Speech.talk("¿Cuándo?");
$('.clockycontainer').css('background-image', "url('svg/asombro.png')");
currentWord = "para";
} else if(thisword.indexOf("para") >= 0 && currentWord === "para"){
Materialize.toast("¿Qué debo recordar?", 3000);
Speech.talk("¿Qué debo recordar?");
$('.clockycontainer').css('background-image', "url('svg/todobiennegro.png')");
currentWord = "limpiar";
} else if(thisword.indexOf("limpiar") >= 0 && currentWord === "limpiar"){
Materialize.toast("¡Hecho! ;)", 3000);
Speech.talk("¡Hecho!");
$('.clockycontainer').css('background-image', "url('svg/CheckN.png')");
currentWord = "agenda";
var fecha = new Date();
fecha.setSeconds(fecha.getSeconds()+5);
comm.agendar("Limpiar la pieza", fecha);
}
/*} else {
//console.log(event.results[i][0].transcript.toLowerCase());
}*/
/*if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}*/
}
/*final_transcript = capitalize(final_transcript);
final_span.innerHTML = linebreak(final_transcript);
interim_span.innerHTML = linebreak(interim_transcript);
console.log(event.results);*/
};
recognition.onend = function(event){
final_transcript = '';
console.log("end");
recognition.start();
}
});