glsa/JS/recognition.js
Exequiel Aramburu d65a64d9df .
2024-12-15 19:28:47 -03:00

165 lines
6.2 KiB
JavaScript

// LIBRERIA PARA ESCUCHAR Y TRANSCRIBIR
function isMobile(){
return (
(navigator.userAgent.match(/Android/i)) ||
(navigator.userAgent.match(/webOS/i)) ||
(navigator.userAgent.match(/iPhone/i)) ||
(navigator.userAgent.match(/iPod/i)) ||
(navigator.userAgent.match(/iPad/i)) ||
(navigator.userAgent.match(/BlackBerry/i))
);
}
var recognition;
var output = document.getElementById("output");
var outputsenasAR = document.getElementById("outputsenasAR");
var outputsenasUY = document.getElementById("outputsenasUY");
var outputHistoria = document.getElementById("outputHistoria");
const removeAcentos = (str) => { return str.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); }
function cambiarSenasAR() { $("#outputHistoria").attr('class', 'form-control SLA_AR');}
function cambiarSenasUY() { $("#outputHistoria").attr('class', 'form-control SLA_UY');}
function cambiarTexto() {$("#outputHistoria").attr('class', 'form-control TEXTO_TRA');}
function stopSpeechRecognition() {
$.notify("DETENIDO.", "warn");
$('#boff').prop('disabled', true);
$('#bon').prop('disabled', false);
$('#mic').attr('src','IMG/mic_off.png');
recognition.stop();
}
function insertarSimbolo(simbolo)
{
if(simbolo== "NEW_LINE"){simbolo = String.fromCharCode(13, 10);}
if(simbolo== "NEW_LINE2"){simbolo = String.fromCharCode(13, 10)+String.fromCharCode(13, 10);}
output.innerHTML += simbolo;
outputsenasAR.innerHTML += simbolo;
outputsenasUY.innerHTML += simbolo;
outputHistoria.innerHTML += simbolo
}
function runSpeechRecognition(parametro = true)
{
$('#boff').prop('disabled', false);
$('#bon').prop('disabled', true);
$('#mic').attr('src','IMG/mic_on.png');
// get output div reference
var output = document.getElementById("output");
// new speech recognition object
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent
recognition = new SpeechRecognition();
// This runs when the speech recognition service starts
if ( isMobile() )
{
recognition.continuous = false;
}else{
recognition.continuous = true;
}
recognition.lang = 'es-ES';
recognition.interimResults = true;
recognition.maxAlternatives = 100;
recognition.onstart = function(){
if(parametro == true) $.notify("Escuchando..", "info");
};
recognition.onspeechend = function(){
if (parametro == true) $.notify("Sin sonido..", "info");
setTimeout('runSpeechRecognition(false)',1000);
}
recognition.onerror = function(event) {
$.notify(event.error, "warn");
}
recognition.onresult = function(event){
var interim_transcript = '';
var final_transcript = '';
for (var i = event.resultIndex; i < event.results.length; ++i) {
// Verify if the recognized text is the last with the isFinal property
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
var transcript = interim_transcript;
var newline = String.fromCharCode(13, 10);
transcript = transcript.replaceAll("punto y coma", ";").replaceAll("dos puntos", ":").replaceAll('punto','.').replaceAll("coma", ",").replaceAll("signo de exclamación", "!").replaceAll("signo de interrogación", "?").replaceAll("guión", "-").replaceAll("nueva línea", newline).replaceAll("abrir paréntesis", "(").replaceAll("cerrar paréntesis", ")").replaceAll("cara sonriente", ":-)").replaceAll("cara triste", ":-(").replaceAll("nuevo apartado", newline+newline);
output.innerHTML = removeAcentos(transcript);
outputsenasAR.innerHTML = removeAcentos(transcript);
outputsenasUY.innerHTML = removeAcentos(transcript);
final_transcript = final_transcript.replaceAll("punto y coma", ";").replaceAll("dos puntos", ":").replaceAll('punto','.').replaceAll("coma", ",").replaceAll("signo de exclamación", "!").replaceAll("signo de interrogación", "?").replaceAll("guión", "-").replaceAll("nueva línea", newline).replaceAll("abrir paréntesis", "(").replaceAll("cerrar paréntesis", ")").replaceAll("cara sonriente", ":-)").replaceAll("cara triste", ":-(").replaceAll("nuevo apartado", newline+newline);
outputHistoria.innerHTML += removeAcentos(final_transcript);
};
// start recognition
recognition.start();
}
// PARA DIBUJAR LA INTESIDAD DEL MICROFONO
function colorPids(vol) {
let all_pids = $('.pid');
let amout_of_pids = Math.round(vol/4);
let elem_range = all_pids.slice(0, amout_of_pids);
for (var i = 0; i < all_pids.length; i++) {
all_pids[i].style.backgroundColor="#e6e7e8";
}
for (var i = 0; i < elem_range.length; i++) {
elem_range[i].style.backgroundColor="#69ce2b";
}
var estado = $('#bon').prop('disabled');
if(amout_of_pids>8 && estado == false) {
var testData = !!document.getElementsByClassName("data-notify-text");
$("#barra").notify("Notamos que estas hablando, pero te falta realizar click en Comenzar.", "info");
}
}
navigator.mediaDevices.getUserMedia({ audio: true})
.then(function(stream) {
$('#mic').attr('src','IMG/mic_on.png');
audioContext = new AudioContext();
analyser = audioContext.createAnalyser();
microphone = audioContext.createMediaStreamSource(stream);
javascriptNode = audioContext.createScriptProcessor(2048, 1, 1);
analyser.smoothingTimeConstant = 0.8;
analyser.fftSize = 1024;
microphone.connect(analyser);
analyser.connect(javascriptNode);
javascriptNode.connect(audioContext.destination);
javascriptNode.onaudioprocess = function() {
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var values = 0;
var length = array.length;
for (var i = 0; i < length; i++) {
values += (array[i]);
}
var average = values / length;
colorPids(average);
}
})
.catch(function(err) {
});