1 2$(document).ready(function(){ 3 var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor); 4 5 if(!isChrome){ 6 return; 7 } 8 9 try{ 10 var SpeechRecognition = (typeof SpeechRecognition !== 'undefined' ? SpeechRecognition : null) || 11 (typeof webkitSpeechRecognition !== 'undefined' ? webkitSpeechRecognition : null); 12 var SpeechRecognitionEvent = (typeof SpeechRecognitionEvent !== 'undefined' ? SpeechRecognitionEvent : null) || 13 (typeof webkitSpeechRecognitionEvent !== 'undefined' ? webkitSpeechRecognitionEvent : null); 14 } 15 catch(e){ 16 var SpeechRecognition = false; 17 } 18 try{ 19 var SpeechGrammarList = (typeof SpeechGrammarList !== 'undefined' ? SpeechGrammarList : null) || 20 (typeof webkitSpeechGrammarList !== 'undefined' ? webkitSpeechGrammarList : null); 21 } 22 catch(e){ 23 var SpeechGrammarList = null; 24 } 25 26 if(!SpeechRecognition){ 27 return; 28 } 29 else{ 30 $('.microphone').removeClass('occulta'); 31 } 32 33 var getLang = (!lang || lang == '') ? 'it' : lang.replace(/\//g,''); 34 var language = getLang + '-' + (getLang == 'en' ? 'US' : getLang.toUpperCase()); 35 36 var nations = []; 37 $('#worldNations li').each(function(){ 38 nations.push($(this).attr('data-value')); 39 }); 40 var grammar = '#JSGF V1.0; grammar nations; public <nation> = ' + nations.join(' | ') + ' ;' 41 42 var recognition = new SpeechRecognition(); 43 try{ 44 var speechRecognitionList = new SpeechGrammarList(); 45 speechRecognitionList.addFromString(grammar, 1); 46 recognition.grammars = speechRecognitionList; 47 } 48 catch(e){ 49 console.log(e.message); 50 } 51 52 recognition.continuous = false; 53 recognition.lang = language;//'en-US'; 54 recognition.interimResults = false; 55 recognition.maxAlternatives = 1; 56 57 58 59 60 function setVoice(lang){ 61 var voice; 62 switch(lang){ 63 case 'it-IT': 64 voice = speechSynthesis.getVoices().filter(item => item.lang === 'it-IT' && item.voiceURI === 'Google italiano')[0]; 65 break; 66 67 case 'en-US': 68 voice = speechSynthesis.getVoices().filter(item => item.lang === 'en-US' && item.voiceURI === 'Google US English')[0]; 69 break; 70 71 case 'fr-FR': 72 voice = speechSynthesis.getVoices().filter(item => item.lang === 'fr-FR' && item.voiceURI === 'Google français')[0]; 73 break; 74 75 case 'es-ES': 76 voice = speechSynthesis.getVoices().filter(item => item.lang === 'es-ES' && item.voiceURI === 'Google español')[0]; 77 break; 78 79 case 'de-DE': 80 voice = speechSynthesis.getVoices().filter(item => item.lang === 'de-DE' && item.voiceURI === 'Google Deutsch')[0]; 81 break; 82 83 default: 84 voice = speechSynthesis.getVoices().filter(item => item.lang === lang)[0]; 85 } 86 87 if(voice == undefined){ 88 voice = speechSynthesis.getVoices().filter(item => item.lang === lang)[0]; 89 } 90 91 return voice; 92 } 93 94 function speak(inputTxt, recon = false){ 95 96 if ('speechSynthesis' in window) { 97 98 99 if (inputTxt !== '') { 100 var utterThis = new SpeechSynthesisUtterance(); 101 utterThis.text = inputTxt; 102 utterThis.onend = function (event) { 103 if(recon){ 104 $('.microphone').addClass('active'); 105 recognition.start(); 106 } 107 } 108 utterThis.onerror = function (event) { 109 console.error('SpeechSynthesisUtterance.onerror'); 110 if(recon){ 111 recognition.start(); 112 } 113 } 114 var selectedVoice = setVoice(language); 115 if(selectedVoice){ 116 utterThis.voiceURI = selectedVoice.voiceURI; 117 utterThis.lang = selectedVoice.lang; 118 } 119 else{ 120 utterThis.lang = language; 121 } 122 utterThis.pitch = 1; 123 utterThis.rate = 1; 124 utterThis.volume = parseFloat(1); 125 window.speechSynthesis.speak(utterThis); 126 } 127 } 128 else{ 129 console.error('speechSynthesis.speaking'); 130 recognition.start(); 131 return; 132 } 133 } 134 135 136 var mic_isActive = false;
137 138 $('.microphone').on('click', function(){ 139 if(mic_isActive){ 140 recognition.stop(); 141 mic_isActive = false; 142 $('.microphone').removeClass('active'); 143 $('.microphone i').removeClass('text-blue'); 144 $('#set-macro').parent().removeClass('input-warning'); 145 $('#set-nation').parent().removeClass('input-warning'); 146 return; 147 } 148 if($('#set-macro').val() == ''){ 149 speak(speach_api1, true); 150 return; 151 } 152 else if($('#set-nation').val() == ''){ 153 speak(speach_api2, true); 154 return; 155 } 156 if($('#set-macro').val() != '' && $('#set-nation').val() != ''){ 157 $('#set-macro').val(''); 158 $('#set-nation').val(''); 159 $('input[name="selected-category"]').val(''); 160 $('input[name="selected-tag"]').val(''); 161 $('input[name="selected-region"]').val(''); 162 $('input[name="selected-nation"]').val(''); 163 speak(speach_api1, true); 164 return; 165 } 166 }); 167 168 // Start event is fired when user accept 169 recognition.onstart = function() { 170 mic_isActive = true; 171 $('.microphone').addClass('active'); 172 $('.microphone i').addClass('text-blue'); 173 console.log('Ready to receive a vocal command.'); 174 if($('#set-macro').val() == ''){ 175 $('#set-macro').parent().addClass('input-warning'); 176 } 177 else{ 178 $('#set-macro').parent().removeClass('input-warning'); 179 $('#set-nation').parent().addClass('input-warning'); 180 } 181 } 182 183 recognition.onresult = function(event) { 184 // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object 185 // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects. 186 // It has a getter so it can be accessed like an array 187 // The first [0] returns the SpeechRecognitionResult at the last position. 188 // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results. 189 // These also have getters so they can be accessed like arrays. 190 // The second [0] returns the SpeechRecognitionAlternative at position 0. 191 // We then return the transcript property of the SpeechRecognitionAlternative object 192 var result = event.results[0][0].transcript; 193 // console.log('Command received: ' + result + '.'); 194 // console.log('Confidence: ' + event.results[0][0].confidence); 195 if($('#set-macro').val() == ''){ 196 $('#set-macro').val(result); 197 $('#set-macro').parent().removeClass('input-warning'); 198 $('#set-nation').parent().addClass('input-warning'); 199 recognition.stop(); 200 mic_isActive = false; 201 $('.microphone').removeClass('active'); 202 speak(speach_api2, true); 203 204 } 205 else{ 206 var getNation = result; //event.results[1][0].transcript.trim(); 207 var formatNation = getNation.replace(/ /g, '-'); 208 var getIsoCode = $('li[data-macro="'+formatNation.toLowerCase()+'"]'); 209 mic_isActive = false; 210 if(getIsoCode.length > 0){ 211 $('input[name="selected-nation"]').val(getIsoCode.attr('data-mid')); 212 } 213 $('#set-nation').val(getNation); 214 $('#set-nation').parent().removeClass('input-warning'); 215 sessionStorage.setItem("speach", "yes"); 216 $('#searchBtn').prop('disabled', false); 217 $('#searchBtn').trigger('click'); 218 } 219 220 } 221 222 recognition.onspeechend = function() { 223 recognition.stop(); 224 mic_isActive = false; 225 $('.microphone').removeClass('active'); 226 $('.microphone i').removeClass('text-blue'); 227 $('#set-macro').parent().removeClass('input-warning'); 228 $('#set-nation').parent().removeClass('input-warning'); 229 if($('#set-macro').val() != '' && $('#set-nation').val() != ''){ 230 sessionStorage.setItem("speach", "yes"); 231 $('#searchBtn').prop('disabled', false); 232 $('#searchBtn').trigger('click'); 233 } 234 } 235 236 recognition.onnomatch = function(event) { 237 recognition.stop(); 238 mic_isActive = false;
239 $('.microphone').removeClass('active'); 240 $('.microphone i').removeClass('text-blue'); 241 $('#set-macro').parent().removeClass('input-warning'); 242 $('#set-nation').parent().removeClass('input-warning'); 243 //console.log("I didn't recognise that word."); 244 if (/Mobi/.test(navigator.userAgent)) { 245 // mobile! 246 return; 247 } 248 else{ 249 speak(speach_api3, true); 250 } 251 } 252 253 recognition.onerror = function(event) { 254 recognition.stop(); 255 mic_isActive = false; 256 $('.microphone').removeClass('active'); 257 $('.microphone i').removeClass('text-blue'); 258 $('#set-macro').parent().removeClass('input-warning'); 259 $('#set-nation').parent().removeClass('input-warning'); 260 //console.log('Error occurred in recognition: ' + event.error); 261 if(event.error == 'not-allowed'){ 262 return; 263 } 264 speak(speach_api3, false); 265 } 266 267 if($('.database-search-container').length > 0){ 268 var readTxt = ''; 269 if($('.region-label h3').length > 0){ 270 readTxt = $('.region-label h3').html().replace('<strong>','').replace('</strong>','').replace('|',''); 271 } 272 else if($('.svg-small').parent().find('h2').length > 0){ 273 readTxt = $('.svg-small').parent().find('h2').html().replace('<strong>','').replace('</strong>','').replace(/<br>/g,''); 274 } 275 276 if (sessionStorage.speach && readTxt.length > 0) { 277 setTimeout(function(){ 278 sessionStorage.removeItem("speach"); 279 speak(readTxt, false); 280 }, 500); 281 } 282 } 283 284 285 286 287});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.