From 58d8f70eabff0cbb62d3b8ee64613b703c233233 Mon Sep 17 00:00:00 2001 From: Lucio Zambon <lucio.zambon@elettra.eu> Date: Mon, 1 Jul 2024 02:50:36 +0000 Subject: [PATCH] Update talk.js --- speech/talk.js | 122 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 41 deletions(-) diff --git a/speech/talk.js b/speech/talk.js index c6b5766..e103b01 100644 --- a/speech/talk.js +++ b/speech/talk.js @@ -4,10 +4,7 @@ window.openData = []; const t0 = + new Date(); let mode = ''; - if (document.location.search.indexOf('background=')>-1) { - const a = document.location.search.split('background=')[1].split('&')[0]; - $('body').css('background-color', +a > 0? '#'+a: a); - } + const launcher = 'https://puma-01.elettra.eu/knob/launcher.php'; const lang = document.location.search.indexOf('lang=')>-1 && document.location.search.split('lang=')[1].split('&')[0]=='en'? 'en-US': 'it-IT'; // BCP 47 language const host = document.location.search.indexOf('talk=')>-1? document.location.search.split('talk=')[1].split('&')[0].replace('ee', 'pcl-elettra-cre-0').replace('ef','pcl-elettra-crf-0'): ''; function switchLocale(newlang) {document.location = './talk.php?lang='+newlang+(host.length>0? '&talk='+host: '');} @@ -18,6 +15,12 @@ $('#title').html(rlocale[lang].talkto + host); $('#micna').attr('title', rlocale[lang].micna); $('#miclabel').html(rlocale[lang].miclabel + ' '); + if (document.location.search.indexOf('background=')>-1) { + const a = document.location.search.split('background=')[1].split('&')[0]; + console.log(a); + $('body').css('background-color', +a > 0? '#'+a: a); + $('#search').hide(); $('#open').hide(); + } }); } // https://stackoverflow.com/questions/64405532/why-speechsynthesisutterance-is-not-working-on-chrome @@ -53,12 +56,23 @@ function startRec() { keepAlive = true; aliveTimer = -1; - // showLog('start'); + showLog('start'); recognition.start(); + showLog('started'); document.getElementById('micstart').style.display = 'inline'; document.getElementById('micstop').style.display = 'none'; + $('#search').hide(); + $('#open').hide(); + console.log($('#search')); + } + function naClick() { + $("#micstart").show(); + $("#micna").hide(); + $("#miclabel").hide(); + if (window.parent && window.parent.document.getElementById("talk")) window.parent.document.getElementById("talk").style.height = "250px"; } function stopRec() { + if (window.parent && window.parent.document.getElementById("talk")) window.parent.document.getElementById("talk").style.height = "30px"; if (typeof window.SpeechRecognition == 'undefined') {$("#micstart").hide();$("#micna").show();} keepAlive = false; recognition.stop(); @@ -70,34 +84,22 @@ let speechStat = ''; let firstRun = true; function showLog(currentToken, p) { - console.log(p); + if (typeof p != 'undefined') console.log(p); console.trace(currentToken); // Show log in console and UI. - const logElement = document.querySelector('#log'); - logElement.textContent = logElement.textContent+'\n'+JSON.stringify(currentToken); + $('#log').html(currentToken); + // const logElement = document.querySelector('#log'); + // logElement.textContent = logElement.textContent+'\n'+JSON.stringify(currentToken); } let finalTranscript = ''; - let recognition = new window.SpeechRecognition(); + const SpeechRecognition = window.speechRecognition || window.webkitSpeechRecognition; + const recognition = new SpeechRecognition(); recognition.maxAlternatives = 10; recognition.continuous = true; recognition.interimResults = true; recognition.lang = lang; recognition.maxAlternatives = 3; showLog('starting...'+'\n'); - /* recognition.onresult = (event) => { - // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object - // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects. - // It has a getter so it can be accessed like an array - // The first [0] returns the SpeechRecognitionResult at position 0. - // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects - // that contain individual results. - // These also have getters so they can be accessed like arrays. - // The second [0] returns the SpeechRecognitionAlternative at position 0. - // We then return the transcript property of the SpeechRecognitionAlternative object - const color = event.results[0][0].transcript; - diagnostic.textContent = `Result received: ${color}.`; - bg.style.backgroundColor = color; - };*/ recognition.onerror = function(event) { showLog(locale[lang].errorname + event.error+', '+event.message+', '+event.lineno+'\n'); showLog(locale[lang].errormessage + JSON.stringify(event.error)); @@ -132,7 +134,7 @@ } return -1; } - function quick_add(transcript) { + function detectTranscript(transcript) { const t = + new Date(); if ((transcript==oldText || (transcript=='te' && oldText.indexOf('te')>-1)) && t-oldTime<1500) return; oldTime = t; @@ -143,10 +145,13 @@ const tok = txt.split(' ')[0]; if (locale[lang].open.indexOf(tok)>-1) { mode = 'open'; + $('#search').hide(); + $('#open').show(); txt = txt.replace(tok, '').replace(' ', ''); } if (mode=='open' && txt.length>0) { - const snd = './talk.php?open='+txt.toLowerCase().replace('.', '')+'&token='+document.location.search.split('?d=')[1].split('&')[0]+"&lang="+lang+"&host="+document.location.search.split('host=')[1].split('&')[0]; + const token = document.location.search.indexOf('?d=')>-1? '&token='+document.location.search.split('?d=')[1].split('&')[0]: ''; + const snd = './talk.php?open='+txt.toLowerCase().replace('.', '')+token+"&lang="+lang+"&host="+document.location.search.split('host=')[1].split('&')[0]; showLog(snd); showLog(' host: '+host); fetch(snd).then((response) => {return response.json();}).then((rlocale) => { @@ -157,10 +162,14 @@ window.openData.push(rlocale[i]); } } + $('#openTable').html('<tr><td>'+txt+'</td><td><input id="oname" class="oname"></input></td><td><button class="btn btn-primary" onClick="findPanel($(\"#oname\").val)">'+locale[lang].searchSubmit+'</button></td></tr>'); + $(function() {$(".oname").autocomplete({source: launcher, select: function(event, ui) {findPanel(ui.item.value, txt); return false;}});}); window.openData.push(txt); showLog('window.openData1'); showLog(window.openData); mode = 'openTable'; + $('#search').hide(); + $('#open').show(); txt = ''; }); } @@ -176,42 +185,50 @@ if (txt.indexOf(locale[lang].three)>-1) num = 2; if (!isNaN(txt)) num = txt - 1; showLog(num); - const exe = (window.openData[num].path? window.openData[num].path: '/runtime/bin/')+ window.openData[num].exename + '&host=' + document.location.search.split('host=')[1].split('&')[0]; - showLog(exe); if (num>-1) { - showLog("https://puma-01.elettra.eu/knob/launcher.php?open="+exe); - fetch("https://puma-01.elettra.eu/knob/launcher.php?open="+exe) + const exe = (window.openData[num].path? window.openData[num].path: '/runtime/bin/')+ window.openData[num].exename + '&host=' + document.location.search.split('host=')[1].split('&')[0]; + showLog(exe); + showLog(launcher+"?open="+exe+"&speech="+window.openData[3]+"&lang="+lang+"&id="+window.openData[num].id); + fetch(launcher+"?open="+exe+"&speech="+window.openData[3]+"&lang="+lang+"&id="+window.openData[num].id) .then((response) => {return response.json();}) .then((rlocale) => {showLog(rlocale);}); - showLog("./talk.php?speech="+window.openData[3]+"&lang="+lang+"&id="+window.openData[num].id); + /*showLog("./talk.php?speech="+window.openData[3]+"&lang="+lang+"&id="+window.openData[num].id); fetch("./talk.php?speech="+window.openData[3]+"&lang="+lang+"&id="+window.openData[num].id) .then((response) => {return response.json();}) - .then((rlocale) => {showLog(rlocale);}); + .then((rlocale) => {showLog(rlocale);});*/ } mode = ''; + $('#search').hide(); + $('#open').hide(); $('#openTable').html(''); } if (locale[lang].search.indexOf(tok)>-1) { mode = 'search'; + $('#search').show(); $('#open').hide(); // txt = txt.replace(tok+' ', '').replace('ing', 'inj').replace('inch', 'inj').split(' ').join('_').split('.').join('_').toUpperCase(); txt = txt.replace(tok+' ', '').split(' ').join('_').split('.').join('_').toUpperCase(); const token = document.location.search.indexOf('?d=')>-1? '&token='+document.location.search.split('?d=')[1].split('&')[0]: ''; - const snd = './talk.php?search='+txt+token+"&lang="+lang; - $('#openTable').html('<tr><td colspan="5">' + locale[lang].searchCorrect + '</td></tr>'); - $('#openTable').html('<tr><td>'+txt+'</td><td><input id="sname" class="sname"></input></td><td><button class="btn btn-primary" onClick="findComponent($(\"#sname\").val)">'+locale[lang].searchSubmit+'</button></td></tr>'); + const snd = './talk.php?search='+txt+token+"&lang="+lang+"&speech="+txt; + $('#openTable').html('<tr><td colspan="5">' + locale[lang].searchCorrect + '</td></tr><tr><td>'+txt+'</td><td><input id="sname" class="sname"></input></td><td><button class="btn btn-primary" onClick="findComponent($(\"#sname\").val)">'+locale[lang].searchSubmit+'</button></td></tr>'); $(function() {$(".sname").autocomplete({source: names, select: function(event, ui) {findComponent(ui.item.value, txt); return false;}});}); showLog(snd); - fetch(snd).then((response) => {return response.text();}).then((rlocale) => {showLog(rlocale); /*alert(rlocale);*/}); - mode = ''; + fetch(snd).then((response) => {return response.text();}).then((rlocale) => { + showLog(rlocale); /*alert(rlocale);*/ + if (rlocale=='OK') $('#openTable').html(''); + }); + // mode = ''; + // $('#search').hide(); $('#open').hide(); } if (locale[lang].list.indexOf(tok)>-1) { mode = 'list'; txt = txt.replace(tok, '').replace(' ', ''); } if (mode=='list') { - const snd = './talk.php?list='+tok+'&token='+document.location.search.split('?d=')[1].split('&')[0]; + const token = document.location.search.indexOf('?d=')>-1? '&token='+document.location.search.split('?d=')[1].split('&')[0]: ''; + const snd = './talk.php?list='+tok+token; fetch(snd).then((response) => {return response.json();}).then((rlocale) => {showLog(rlocale); alert(rlocale);}); mode = ''; + $('#search').hide(); $('#open').hide(); } showLog('mode: '+mode+', tok: '+tok); } @@ -219,13 +236,22 @@ // transcript.replace('sky ',''); } - function findComponent(txt, speech) { + function findPanel(txt, speech) { console.log(txt); const token = document.location.search.indexOf('?d=')>-1? '&token='+document.location.search.split('?d=')[1].split('&')[0]: ''; - const snd = './talk.php?search='+txt+token+"&lang="+lang+"&speech="+speech; + const snd = './talk.php?open='+txt+token+"&lang="+lang+"&speech="+speech; showLog(snd); fetch(snd).then((response) => {return response.text();}).then((rlocale) => {showLog(rlocale); /*alert(rlocale);*/}); } + function findComponent(txt, speech) { + showLog(txt); + showLog(speech); + const token = document.location.search.indexOf('?d=')>-1? '&token='+document.location.search.split('?d=')[1].split('&')[0]: ''; + const snd = './talk.php?search='+txt+token+"&lang="+lang+"&speech="+speech; + showLog(snd); + fetch(snd).then((response) => {return response.text();}).then((rlocale) => {showLog(rlocale); alert(rlocale);$('#openTable').html('');}); + $(".sname").val(txt); + } if (!String.prototype.replaceAll) { String.prototype.replaceAll = function(search, replace) { return this.split(search).join(replace); @@ -261,6 +287,20 @@ }); } + /* recognition.onresult = (event) => { + // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object + // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects. + // It has a getter so it can be accessed like an array + // The first [0] returns the SpeechRecognitionResult at position 0. + // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects + // that contain individual results. + // These also have getters so they can be accessed like arrays. + // The second [0] returns the SpeechRecognitionAlternative at position 0. + // We then return the transcript property of the SpeechRecognitionAlternative object + const color = event.results[0][0].transcript; + diagnostic.textContent = `Result received: ${color}.`; + bg.style.backgroundColor = color; + };*/ recognition.onresult = (event) => { let interimTranscript = ''; for (let i = event.resultIndex, len = event.results.length; i < len; i++) { @@ -268,7 +308,7 @@ // const dt = + new Time() - t0; if (event.results[i][0].transcript.length) showLog(' mode '+mode+', i: '+i+', transcripts: '+event.results[i][0].transcript+(event.results[i][1]? ', transcripts: '+event.results[i][1].transcript:'')+'\n'); if (event.results[i].isFinal) { - quick_add(transcript.toLowerCase()); + detectTranscript(transcript.toLowerCase()); var meno = transcript.toLowerCase().indexOf('meno '); console.log(meno, transcript.substring(meno+5,meno+6), transcript.substring(meno+5,meno+6) % 1); if (meno>-1 && transcript.substring(meno+5,meno+6) % 1 === 0) { @@ -285,5 +325,5 @@ // document.getElementById('transcriptDiv').innerHTML = finalTranscript + '<i style=\"color:#00d;\">' + interimTranscript + '</i>'; document.getElementById('transcriptDiv').innerHTML = '<i style=\"color:#00d;\">' + finalTranscript + '</i>'; }; - showLog('start'+'\n'); recognition.start(); + -- GitLab