// onwork0009 - ver 181226 night - temporal ok 2 //var inputfile='https://recruit-lattice.jp/wp-content/uploads/2018/01/test2.json'; var inputfile='https://recruit-lattice.jp/wp-content/uploads/2018/01/ElectricalFan_test1.json'; //var inputfile='hotx.gltf'; //var inputfile='ElectricalFan_test1.json'; window.onload = function() { var divPage = document.getElementById("page"); if(divPage){ var currentStyle = divPage.getAttribute("style"); if (currentStyle) { divPage.setAttribute("style", currentStyle + ";z-index: -1"); } else { divPage.setAttribute("style", "z-index: -1"); } console.log(divPage); } var button_vr = document.getElementById("button_vr"); if(button_vr){ button_vr.onclick = function (event) { testVRhtml(); var ctrlVowel = document.getElementById("ctrlVowel"); ctrlVowel.style.visibility="visible"; } } } // test document.write("
" //+"" //+"" +""//+"" //+"" //+"" //+"" +""//+"" +"音量\" onclick=\"testRecogMovAlternate()\">" //+"" +"" +"" +"
" +"
"); function sleep(a){ var dt1 = new Date().getTime(); var dt2 = new Date().getTime(); while (dt2 < dt1 + a){ dt2 = new Date().getTime(); } return; } var fftsize = 1024; var trainsize = fftsize/2; var fftscope = fftsize/2; var Abridged_powcoef = {}; var n = 0; var freq = 0; var freqstep= 44100/fftsize; for(; freq < 20; n++, freq+=freqstep) { Abridged_powcoef[Math.round(freqstep*n)+'Hz']=0.1; } for(; freq < 50; n++, freq+=freqstep) { Abridged_powcoef[Math.round(freqstep*n)+'Hz']=1.0; } for(; freq < 4000; n++, freq+=freqstep) { Abridged_powcoef[Math.round(freqstep*n)+'Hz']=2.0; } for(; freq < 8000; n++, freq+=freqstep) { Abridged_powcoef[Math.round(freqstep*n)+'Hz']=1.0; } for(; freq < 12000; n++, freq+=freqstep) { Abridged_powcoef[Math.round(freqstep*n)+'Hz']=0.7; } for(; n 100 && sumAbsAmp < 10.0) {return;} if (sumAbsAmp - avedBRecog > 50) { sumdBRecog = 0.0; nFrameRecog = 0; } nFrameRecog += 1; sumdBRecog += sumAbsAmp; avedBRecog = sumdBRecog / nFrameRecog; if (avedBRecog < 50.0) {analyserNode.minDecibels -= 2; analyserNode.maxDecibels -= 2; nFrameRecog-=0.2; avedBRecog+=2;} if (avedBRecog > 150.0) {analyserNode.minDecibels += 2; analyserNode.maxDecibels += 2; nFrameRecog+=0.2; avedBRecog-=2;} maxdBRecog = Math.max(maxdBRecog,avedBRecog); thSumAbsAmpStop = Math.max(1.0, (maxdBRecog+avedBRecog+sumAbsAmp)/3 - thSumAbsAmpStopGap); console.log("analyserNode.minDecibels : " + analyserNode.minDecibels + " analyserNode.maxDecibels : " + analyserNode.maxDecibels); noticeBox.textContent += "\n" + "thSumAbsAmpStop : " + thSumAbsAmpStop + "\n"+"sumAbsAmp : " + sumAbsAmp; } return; } var iframe1 = document.getElementById("iframe1"); var winelem =iframe1.contentWindow; var element = iframe1.contentDocument.getElementById("div_a-scene"); var rotcamera = iframe1.contentDocument.getElementById('camera'); var curposition = rotcamera.getAttribute('position'); var rottarget = iframe1.contentDocument.getElementById('obj'); var rotation = rottarget.getAttribute('rotation'); var val_range01 = document.getElementById("range01").value * 1.0 / 255; var varOpe = val_range01; if(isEnableMic){ var sumAbsAmp = 0.0; var powcoef = Object.values(Abridged_powcoef); for (var i = 0; i < fftscope; i++) { sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= fftscope; noticeBox.textContent += "\n" + "thSumAbsAmpStop : " + thSumAbsAmpStop +"\n"+"sumAbsAmp : " + sumAbsAmp; if (sumAbsAmp < 0.1) {analyserNode.minDecibels -= 2; analyserNode.maxDecibels -= 2;} if (sumAbsAmp > 150.0) {analyserNode.minDecibels += 2; analyserNode.maxDecibels += 2;} if(thSumAbsAmpStopConst > sumAbsAmp) {// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! //if(thSumAbsAmpStop > sumAbsAmp) { console.log("thSumAbsAmpStop > sumAbsAmp thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); noticeBox.textContent += "\n"+"thSumAbsAmpStop > sumAbsAmp" if(elapsedtime < elapsedtimeMax){return;} recogCommand = null; elapsedtime = 0; r.stop(); return; } if(elapsedtime < elapsedtimeMax){ sumAbsAmp *= 1.0*elapsedtime/elapsedtimeMax; elapsedtime++; } console.log("thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); varOpe = Math.min(1.0 ,Math.max(Math.min(sumAbsAmp , 255)*1.0/255, 0))*coefScaleVol; } if(isMouseOnInputRange01) {varOpe = val_range01;} else { document.getElementById("range01").value = Math.round(varOpe * 255); } var scale = rottarget.getAttribute('scale'); if (scale == null) {scale = new THREE.Vector3();} scaleScaleVol = Math.log(1 + varOpe)+1; var curposition = rottarget.getAttribute('position'); if (curposition == null) {curposition = new THREE.Vector3();} if(recogCommand == '右') curposition.x += scaleScaleVol*scalePosSprt; if(recogCommand == '左') curposition.x -= scaleScaleVol*scalePosSprt; if(recogCommand == '上') curposition.y += scaleScaleVol*scalePosSprt; if(recogCommand == '下') curposition.y -= scaleScaleVol*scalePosSprt; if(recogCommand == '前') curposition.z += scaleScaleVol*scalePosSprt; if(recogCommand == '奥') curposition.z -= scaleScaleVol*scalePosSprt; rottarget.setAttribute('position', curposition); } if(userAgent.indexOf('android') == -1){ navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; } console.log(navigator.getUserMedia); navigator.getUserMedia({video : false, audio : true}, successCallback, errorCallback); } var funcAudioRecogCtrl = function() { function successCallback(stream) { console.log("successCallback"); noticeBox.textContent = 'successCallback'; setupAudioNodes(stream); isEnableMic = true; intervalId = setInterval(intervalfunc, 100); gumStream = stream; } function errorCallback() { console.log("errorCallback"); noticeBox.textContent = 'errorCallback'; isEnableMic = false; intervalId = setInterval(intervalfunc, 100); } // intervalfunc = function () { console.log('intervalfunc recogCommand : ' + recogCommand); noticeBox.textContent = "intervalfunc" + "\n" + "recogCommand : " + recogCommand; if(recogCommand == null){//if(!recogCommand){ // 180624 ... // calc average dB of during recog if (nFrameRecog != null) { if (nFrameRecog == 0) {sumdBRecog = 0.0;maxdBRecog=0.0;} var sumAbsAmp = 0.0; var powcoef = Object.values(Abridged_powcoef); for (var i = 0; i < fftscope; i++) { sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= fftscope; // refresh long low vol span 180625 if (nFrameRecog > 100 && sumAbsAmp < 10.0) {return;} if (sumAbsAmp - avedBRecog > 50) { sumdBRecog = 0.0; nFrameRecog = 0; } nFrameRecog += 1; sumdBRecog += sumAbsAmp; avedBRecog = sumdBRecog / nFrameRecog; if (avedBRecog < 50.0) {analyserNode.minDecibels -= 2; analyserNode.maxDecibels -= 2; nFrameRecog-=0.2; avedBRecog+=2;} if (avedBRecog > 150.0) {analyserNode.minDecibels += 2; analyserNode.maxDecibels += 2; nFrameRecog+=0.2; avedBRecog-=2;} maxdBRecog = Math.max(maxdBRecog,avedBRecog); thSumAbsAmpStop = Math.max(1.0, (maxdBRecog+avedBRecog+sumAbsAmp)/3 - thSumAbsAmpStopGap); console.log("analyserNode.minDecibels : " + analyserNode.minDecibels + " analyserNode.maxDecibels : " + analyserNode.maxDecibels); noticeBox.textContent += "\n" + "thSumAbsAmpStop : " + thSumAbsAmpStop + "\n"+"sumAbsAmp : " + sumAbsAmp; } return; } var iframe1 = document.getElementById("iframe1"); var winelem =iframe1.contentWindow; var element = iframe1.contentDocument.getElementById("div_a-scene"); var rotcamera = iframe1.contentDocument.getElementById('camera'); var curposition = rotcamera.getAttribute('position'); var rottarget = iframe1.contentDocument.getElementById('obj'); var rotation = rottarget.getAttribute('rotation'); var val_range01 = document.getElementById("range01").value * 1.0 / 255; var varOpe = val_range01; if(isEnableMic){ var sumAbsAmp = 0.0; var powcoef = Object.values(Abridged_powcoef); for (var i = 0; i < fftscope; i++) { sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= fftscope; noticeBox.textContent += "\n" + "thSumAbsAmpStop : " + thSumAbsAmpStop +"\n"+"sumAbsAmp : " + sumAbsAmp; if (sumAbsAmp < 0.1) {analyserNode.minDecibels -= 2; analyserNode.maxDecibels -= 2;} if (sumAbsAmp > 150.0) {analyserNode.minDecibels += 2; analyserNode.maxDecibels += 2;} elapsedtime++; if(thSumAbsAmpStop > sumAbsAmp) { console.log("thSumAbsAmpStop > sumAbsAmp thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); noticeBox.textContent += "\n"+"thSumAbsAmpStop > sumAbsAmp" if(elapsedtime < elapsedtimeMax){return;} recogCommand = null; elapsedtime = 0; r.stop(); return; } if(elapsedtime < elapsedtimeMax){ sumAbsAmp *= 1.0*elapsedtime/elapsedtimeMax; } console.log("thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); //varOpe = Math.min(1.0 ,Math.max(Math.min(avePow , 255)*1.0/255, 0))*coefScaleVol; varOpe = Math.min(1.0 ,Math.max(Math.min(sumAbsAmp , 255)*1.0/255, 0))*coefScaleVol; } if(isMouseOnInputRange01) {varOpe = val_range01;} else { document.getElementById("range01").value = Math.round(varOpe * 255); } var scale = rottarget.getAttribute('scale'); if (scale == null) {scale = new THREE.Vector3();} scaleScaleVol = Math.log(1 + varOpe)+1; /*var tmpscale = scaleScaleVol*scalePosSprt; scale.x = tmpscale; scale.y = tmpscale; scale.z = tmpscale; rottarget.setAttribute('scale', scale); */ var curposition = rottarget.getAttribute('position'); if (curposition == null) {curposition = new THREE.Vector3();} if(recogCommand == '右') curposition.x += scaleScaleVol*scalePosSprt; if(recogCommand == '左') curposition.x -= scaleScaleVol*scalePosSprt; if(recogCommand == '上') curposition.y += scaleScaleVol*scalePosSprt; if(recogCommand == '下') curposition.y -= scaleScaleVol*scalePosSprt; if(recogCommand == '前') curposition.z += scaleScaleVol*scalePosSprt; if(recogCommand == '奥') curposition.z -= scaleScaleVol*scalePosSprt; rottarget.setAttribute('position', curposition); } //audioContext = new AudioContext(); //window.alert("play"); if(userAgent.indexOf('android') == -1){ navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; } console.log(navigator.getUserMedia); navigator.getUserMedia({video : false, audio : true}, successCallback, errorCallback); } /* window.AudioContext = (function(){ //return window.webkitAudioContext || window.AudioContext || window.mozAudioContext; return window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext; })(); */ var audioContext; var sourceNode; var analyserNode; var javascriptNode; var sampleSize = 1024; // number of samples to collect before analyzing // decreasing this gives a faster sonogram, increasing it slows it down var amplitudeArray; // array to hold frequency data var audioStream; function setupAudioNodes(stream) { // create the media stream from the audio input source (microphone) sourceNode = audioContext.createMediaStreamSource(stream); audioStream = stream; analyserNode = audioContext.createAnalyser(); javascriptNode = audioContext.createScriptProcessor(sampleSize, 1, 1); // Create the array for the data values amplitudeArray = new Uint8Array(analyserNode.frequencyBinCount); // setup the event handler that is triggered every time enough samples have been collected // trigger the audio analysis and draw one column in the display based on the results javascriptNode.onaudioprocess = function () { amplitudeArray = new Uint8Array(analyserNode.frequencyBinCount); //analyserNode.getByteTimeDomainData(amplitudeArray); analyserNode.getByteFrequencyData(amplitudeArray); // // draw one column of the display // requestAnimFrame(drawTimeDomain); } // Now connect the nodes together // Do not connect source node to destination - to avoid feedback sourceNode.connect(analyserNode); analyserNode.connect(javascriptNode); javascriptNode.connect(audioContext.destination); } var noticeBox = document.createElement('pre'); document.body.appendChild(noticeBox); noticeBox.placeholder = '入力結果'; noticeBox.contentEditable = true; noticeBox.style.position = 'fixed'; noticeBox.style.top = '10px'; noticeBox.style.right = '10px'; noticeBox.style.width = '250px'; noticeBox.style.minHeight = '50px'; noticeBox.style.transition = '.2s'; noticeBox.style.zIndex = 999999; var r; var funcSpeechRecognition = function() { r = new webkitSpeechRecognition();console.log(r); r.addEventListener('start', function() {console.log('start'); noticeBox.textContent = 'start'; noticeBox.style.background = 'rgba(255,100,100,.8)'; recogStatus = 'start'; }); r.addEventListener('end', function() {console.log('end'); noticeBox.style.background = 'rgba(200,200,200,.8)'; noticeBox.textContent = 'end'; recogStatus = 'end'; if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'android';/*sleep(1000);*/r.start();return;} // for debug sleep funcSpeechRecognition(); }); /* log sample test_recog.html:60 audiostart test_recog.html:62 soundstart test_recog.html:64 speechstart test_recog.html:65 speechend test_recog.html:63 soundend test_recog.html:61 audioend test_recog.html:60 audiostart test_recog.html:61 audioend */ r.addEventListener('audiostart', function() {recogStatus = 'audiostart'; noticeBox.textContent = 'audiostart';}); r.addEventListener('audioend', function() {recogStatus = 'audioend'; noticeBox.textContent = 'audioend';}); r.addEventListener('soundstart', function() {recogStatus = 'soundstart'; noticeBox.textContent = 'soundstart';}); r.addEventListener('soundend', function() {recogStatus = 'soundend'; noticeBox.textContent = 'soundend';}); r.addEventListener('speechstart', function() {recogStatus = 'speechstart'; noticeBox.textContent = 'speechstart';nFrameRecog = 0;}); //r.addEventListener('speechend', function() {console.log('speechend');r.stop();}); r.addEventListener('speechend', function() {recogStatus = 'speechend'; noticeBox.textContent = 'speechend';nFrameRecog = null;recogCommand = null; if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'speechend';/*sleep(1000);*/return;} // for debug sleep r.stop();}); r.addEventListener('result', function(e) {noticeBox.textContent = 'result'; if(recogCommand != null)/*if(recogCommand)*/{return;} console.log(e); if(e.results.length) { var results = e.results[e.results.length-1]; /*if(results.isFinal) { noticeBox.textContent = 'fixed!!\n' + results[e.results.length-1].transcript; r.stop(); } else*/ { var possiblity = [].slice.call(results).map(function(result, i) { var strtrans = result.transcript;console.log(strtrans); var targetCommand; function checkIncludes(strtrans, targetCommand, targetIncludes){ if (strtrans.includes(targetIncludes)){console.log(targetIncludes);noticeBox.textContent = targetCommand;recogCommand=targetCommand;} } targetCommand='右'; checkIncludes(strtrans, targetCommand, '右'); checkIncludes(strtrans, targetCommand, 'み'); checkIncludes(strtrans, targetCommand, 'ミ'); checkIncludes(strtrans, targetCommand, '三'); checkIncludes(strtrans, targetCommand, 'ウィー'); checkIncludes(strtrans, targetCommand, 'イギー'); targetCommand='左'; checkIncludes(strtrans, targetCommand, '左'); checkIncludes(strtrans, targetCommand, 'ひ'); checkIncludes(strtrans, targetCommand, '飛騨'); checkIncludes(strtrans, targetCommand, '日田'); checkIncludes(strtrans, targetCommand, 'ヒーター'); targetCommand='上'; checkIncludes(strtrans, targetCommand, '上'); checkIncludes(strtrans, targetCommand, 'う'); checkIncludes(strtrans, targetCommand, 'うえ'); checkIncludes(strtrans, targetCommand, 'AAA'); checkIncludes(strtrans, targetCommand, 'ウェイ'); targetCommand='下'; checkIncludes(strtrans, targetCommand, '下'); checkIncludes(strtrans, targetCommand, 'し'); checkIncludes(strtrans, targetCommand, 'C'); checkIncludes(strtrans, targetCommand, 'シーサー'); checkIncludes(strtrans, targetCommand, '明日'); targetCommand='前'; checkIncludes(strtrans, targetCommand, '前'); checkIncludes(strtrans, targetCommand, 'ま'); targetCommand='奥'; checkIncludes(strtrans, targetCommand, '奥'); checkIncludes(strtrans, targetCommand, 'お'); checkIncludes(strtrans, targetCommand, 'OK'); checkIncludes(strtrans, targetCommand, 'オーク'); checkIncludes(strtrans, targetCommand, '後'); return i + ':' + result.transcript; }).join('\n\n'); noticeBox.textContent = 'progress('+results.length+')...\n' + possiblity; } } else { noticeBox.textContent = '結果が空です'; } }); r.lang = 'ja-JP'; r.continuous = true; r.interimResults = true; r.maxAlternatives = 10; r.start(); } // --------------------- test Android var hasReceivedResult = false; var funcSpeechRecognitionAndroid = function() { r = new webkitSpeechRecognition(); r.addEventListener('start', function() { noticeBox.textContent = 'start'; noticeBox.style.background = 'rgba(255,100,100,.8)'; recogStatus = 'start'; }); r.addEventListener('end', function() { noticeBox.style.background = 'rgba(200,200,200,.8)'; noticeBox.textContent = 'end'; recogStatus = 'end'; if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'android - sleep';/*sleep(100);*/ r.start();return;} // for debug sleep funcSpeechRecognitionAndroid(); }); /* log sample test_recog.html:60 audiostart test_recog.html:62 soundstart test_recog.html:64 speechstart test_recog.html:65 speechend test_recog.html:63 soundend test_recog.html:61 audioend test_recog.html:60 audiostart test_recog.html:61 audioend */ r.addEventListener('audiostart', function() {recogStatus = 'audiostart'; noticeBox.textContent = 'audiostart';}); r.addEventListener('audioend', function() {recogStatus = 'audioend'; noticeBox.textContent = 'audioend';}); r.addEventListener('soundstart', function() {recogStatus = 'soundstart'; noticeBox.textContent = 'soundstart';}); r.addEventListener('soundend', function() {recogStatus = 'soundend'; noticeBox.textContent = 'soundend';}); r.addEventListener('speechstart', function() {recogStatus = 'speechstart'; noticeBox.textContent = 'speechstart';nFrameRecog = 0;}); //r.addEventListener('speechend', function() {console.log('speechend');r.stop();}); r.addEventListener('speechend', function() {recogStatus = 'speechend'; noticeBox.textContent = 'speechend';nFrameRecog = null;recogCommand = null; if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'speechend';/*sleep(1000);*/return;} // for debug sleep r.stop();}); r.addEventListener('result', function(e) {noticeBox.textContent = 'result'; hasReceivedResult = true; //if(recogCommand){return;} console.log(e); if(e.results.length) { var results = e.results[e.results.length-1]; /*if(results.isFinal) { noticeBox.textContent = 'fixed!!\n' + results[e.results.length-1].transcript; r.stop(); } else*/ { var possiblity = [].slice.call(results).map(function(result, i) { var strtrans = result.transcript;console.log(strtrans); var targetCommand; function checkIncludes(strtrans, targetCommand, targetIncludes){ if (strtrans.includes(targetIncludes)){console.log(targetIncludes);noticeBox.textContent = targetCommand;recogCommand=targetCommand;} } targetCommand='右'; checkIncludes(strtrans, targetCommand, '右'); checkIncludes(strtrans, targetCommand, 'み'); checkIncludes(strtrans, targetCommand, 'ミ'); checkIncludes(strtrans, targetCommand, '三'); checkIncludes(strtrans, targetCommand, 'ウィー'); checkIncludes(strtrans, targetCommand, 'イギー'); targetCommand='左'; checkIncludes(strtrans, targetCommand, '左'); checkIncludes(strtrans, targetCommand, 'ひ'); checkIncludes(strtrans, targetCommand, '飛騨'); checkIncludes(strtrans, targetCommand, '日田'); checkIncludes(strtrans, targetCommand, 'ヒーター'); targetCommand='上'; checkIncludes(strtrans, targetCommand, '上'); checkIncludes(strtrans, targetCommand, 'う'); checkIncludes(strtrans, targetCommand, 'うえ'); checkIncludes(strtrans, targetCommand, 'AAA'); checkIncludes(strtrans, targetCommand, 'ウェイ'); targetCommand='下'; checkIncludes(strtrans, targetCommand, '下'); checkIncludes(strtrans, targetCommand, 'し'); checkIncludes(strtrans, targetCommand, 'C'); checkIncludes(strtrans, targetCommand, 'シーサー'); checkIncludes(strtrans, targetCommand, '明日'); targetCommand='前'; checkIncludes(strtrans, targetCommand, '前'); checkIncludes(strtrans, targetCommand, 'ま'); targetCommand='奥'; checkIncludes(strtrans, targetCommand, '奥'); checkIncludes(strtrans, targetCommand, 'お'); checkIncludes(strtrans, targetCommand, 'OK'); checkIncludes(strtrans, targetCommand, 'オーク'); checkIncludes(strtrans, targetCommand, '後'); return i + ':' + result.transcript; }).join('\n\n'); noticeBox.textContent = 'progress('+results.length+')...\n' + possiblity; } } else { noticeBox.textContent = '結果が空です'; } }); r.lang = 'ja-JP'; r.continuous = true; r.interimResults = true; r.maxAlternatives = 10; r.start(); } // --------------------- function testRecogAndroid() { noticeBox.textContent = 'testRecogAndroid'; if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcSpeechRecognitionAndroid(); } function testAudioAndroid() { noticeBox.textContent = 'ctrlAudioAndroid'; if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcAudioAndroid(); } function funcAudioAndroid() { function successCallback(stream) { console.log("successCallback"); noticeBox.textContent = 'successCallback'; isEnableMic = true; setupAudioNodes(stream); intervalId = setInterval(intervalfunc, 100); gumStream = stream; } function errorCallback() { console.log("errorCallback"); noticeBox.textContent = 'errorCallback'; isEnableMic = false; //intervalId = setInterval(intervalfunc, 100); } intervalfunc = function () { //console.log('intervalfunc'); if(recogCommand != null){return;} noticeBox.textContent = "intervalfunc" + "\n" + "recogCommand : " + recogCommand; if(isEnableMic){ var sumAbsAmp = 0.0; /* for (var i = 0; i < amplitudeArray.length; i++) { //sumAbsAmp += Math.abs(amplitudeArray[i]); sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= amplitudeArray.length; */ var powcoef = Object.values(Abridged_powcoef); for (var i = 0; i < fftscope; i++) { sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= fftscope; noticeBox.textContent += "\n"+"thSumAbsAmpStop : " + thSumAbsAmpStop +"\n"+"sumAbsAmp : " + sumAbsAmp; if(thSumAbsAmpStop > sumAbsAmp) { //console.log("thSumAbsAmpStop > sumAbsAmp thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); recogCommand = null; elapsedtime = 0; r.stop(); return; } if(elapsedtime < elapsedtimeMax){ sumAbsAmp *= 1.0*elapsedtime/elapsedtimeMax; elapsedtime++; } //console.log("thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); } } //audioContext = new AudioContext(); navigator.getUserMedia({video : false, audio : true}, successCallback, errorCallback); } // --------------------- test testRecogMovAndroid ---------------------- function testRecogMovAndroid() { noticeBox.textContent = 'testRecogMovAndroid'; if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcAudioAndroid(); funcSpeechRecognitionAndroid(); } // --------------------- test testRecogMovSimple ---------------------- function testRecogMovSimple() { noticeBox.textContent = 'testRecogMovSimple'; //if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcRecogMovSimpleCtrl(); funcSpeechRecognitionAndroid(); } var isSimpleCtrlPhase = false; function funcRecogMovSimpleCtrl() { // intervalfunc = function () { console.log('intervalfunc recogCommand : ' + recogCommand); noticeBox.textContent = "intervalfunc" + "\n" + "recogCommand : " + recogCommand; if(recogCommand == null){ return; } /* log sample test_recog.html:60 audiostart test_recog.html:62 soundstart test_recog.html:64 speechstart test_recog.html:65 speechend test_recog.html:63 soundend test_recog.html:61 audioend test_recog.html:60 audiostart test_recog.html:61 audioend */ if (recogStatus == 'end' || recogStatus == 'audioend' || recogStatus == 'soundend') {if (isSimpleCtrlPhase) {recogCommand = null; isSimpleCtrlPhase = false; return;}} if (recogStatus != 'audiostart' && recogStatus != 'soundstart' && recogStatus != 'speechstart' && recogStatus != 'speechend') { return; } isSimpleCtrlPhase = true; var iframe1 = document.getElementById("iframe1"); var winelem =iframe1.contentWindow; var element = iframe1.contentDocument.getElementById("div_a-scene"); var rotcamera = iframe1.contentDocument.getElementById('camera'); var curposition = rotcamera.getAttribute('position'); //console.log(curposition); // //var rotation = rotcamera.getAttribute('rotation'); // //console.log(rotation); var rottarget = iframe1.contentDocument.getElementById('obj'); //console.log(rottarget); var rotation = rottarget.getAttribute('rotation'); // console.log(rotation); var val_range01 = document.getElementById("range01").value * 1.0 / 255; var varOpe = val_range01; varOpe = 1.0; if (recogStatus == 'audiostart') {varOpe = 0.2;} if (recogStatus == 'soundstart') {} if (recogStatus == 'speechstart'){varOpe = 2.0;} if (recogStatus == 'speechend') {varOpe = 0.1;} if(isMouseOnInputRange01) {varOpe = val_range01;} else { document.getElementById("range01").value = Math.round(varOpe * 255); } var scale = rottarget.getAttribute('scale'); if (scale == null) {scale = new THREE.Vector3();} scaleScaleVol = Math.log(1 + varOpe)+1; var curposition = rottarget.getAttribute('position'); if (curposition == null) {curposition = new THREE.Vector3();} if(recogCommand == '右') curposition.x += scaleScaleVol*scalePosSprt; if(recogCommand == '左') curposition.x -= scaleScaleVol*scalePosSprt; if(recogCommand == '上') curposition.y += scaleScaleVol*scalePosSprt; if(recogCommand == '下') curposition.y -= scaleScaleVol*scalePosSprt; if(recogCommand == '前') curposition.z += scaleScaleVol*scalePosSprt; if(recogCommand == '奥') curposition.z -= scaleScaleVol*scalePosSprt; rottarget.setAttribute('position', curposition); } intervalId = setInterval(intervalfunc, 100); } // --------------------- test testRecogMovAlternate ---------------------- function testRecogMovAlternate() { noticeBox.textContent = 'testRecogMovAlternate'; //if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcSpeechRecognitionAlternate(); } function funcRecogMovAlternate() { function successCallback(stream) { console.log("successCallback"); noticeBox.textContent = 'successCallback'; isEnableMic = true; setupAudioNodes(stream); intervalId = setInterval(intervalfunc, 100); gumStream = stream; } function errorCallback() { console.log("errorCallback"); noticeBox.textContent = 'errorCallback'; isEnableMic = false; //intervalId = setInterval(intervalfunc, 100); } // intervalfunc = function () { console.log('intervalfunc recogCommand : ' + recogCommand); noticeBox.textContent = "intervalfunc" + "\n" + "recogCommand : " + recogCommand; if(recogCommand == null){ return; } var iframe1 = document.getElementById("iframe1"); var winelem =iframe1.contentWindow; var element = iframe1.contentDocument.getElementById("div_a-scene"); var rotcamera = iframe1.contentDocument.getElementById('camera'); var curposition = rotcamera.getAttribute('position'); var rottarget = iframe1.contentDocument.getElementById('obj'); var rotation = rottarget.getAttribute('rotation'); var val_range01 = document.getElementById("range01").value * 1.0 / 255; var varOpe = val_range01; if(isEnableMic){ var sumAbsAmp = 0.0; var powcoef = Object.values(Abridged_powcoef); for (var i = 0; i < fftscope; i++) { sumAbsAmp += amplitudeArray[i] * powcoef[i]; } sumAbsAmp /= fftscope; noticeBox.textContent += "\n" + "thSumAbsAmpStop : " + thSumAbsAmpStop +"\n"+"sumAbsAmp : " + sumAbsAmp; if (sumAbsAmp < 0.1) {analyserNode.minDecibels -= 2; analyserNode.maxDecibels -= 2;} if (sumAbsAmp > 150.0) {analyserNode.minDecibels += 2; analyserNode.maxDecibels += 2;} elapsedtime++; if(thSumAbsAmpStop > sumAbsAmp) { console.log("thSumAbsAmpStop > sumAbsAmp thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); noticeBox.textContent += "\n"+"thSumAbsAmpStop > sumAbsAmp" if(elapsedtime < elapsedtimeMax){return;} recogCommand = null; elapsedtime = 0; audioContext.close(); gumStream.getAudioTracks()[0].stop(); // https://www.fxsitecompat.com/ja/docs/2015/mediastream-stop-has-been-deprecated/ clearInterval(intervalId); intervalId = null; //noticeBox.textContent += "\n"+"180709";//if(userAgent.indexOf('android') != -1){r.start(); return;} //sleep(1000); funcSpeechRecognitionAlternate(); //r.start(); //r.stop(); return; } if(elapsedtime < elapsedtimeMax){ sumAbsAmp *= 1.0*elapsedtime/elapsedtimeMax; } console.log("thSumAbsAmpStop : " + thSumAbsAmpStop + " sumAbsAmp : " + sumAbsAmp); varOpe = Math.min(1.0 ,Math.max(Math.min(sumAbsAmp , 255)*1.0/255, 0))*coefScaleVol; } if(isMouseOnInputRange01) {varOpe = val_range01;} else { document.getElementById("range01").value = Math.round(varOpe * 255); } var scale = rottarget.getAttribute('scale'); if (scale == null) {scale = new THREE.Vector3();} scaleScaleVol = Math.log(1 + varOpe)+1; var curposition = rottarget.getAttribute('position'); if (curposition == null) {curposition = new THREE.Vector3();} if(recogCommand == '右') curposition.x += scaleScaleVol*scalePosSprt; if(recogCommand == '左') curposition.x -= scaleScaleVol*scalePosSprt; if(recogCommand == '上') curposition.y += scaleScaleVol*scalePosSprt; if(recogCommand == '下') curposition.y -= scaleScaleVol*scalePosSprt; if(recogCommand == '前') curposition.z += scaleScaleVol*scalePosSprt; if(recogCommand == '奥') curposition.z -= scaleScaleVol*scalePosSprt; rottarget.setAttribute('position', curposition); } //intervalId = setInterval(intervalfunc, 100); audioContext = new AudioContext(); navigator.getUserMedia({video : false, audio : true}, successCallback, errorCallback); } var funcSpeechRecognitionAlternate = function() { r = new webkitSpeechRecognition(); r.addEventListener('start', function() { noticeBox.textContent = 'start'; noticeBox.style.background = 'rgba(255,100,100,.8)'; recogStatus = 'start'; }); r.addEventListener('end', function() { noticeBox.style.background = 'rgba(200,200,200,.8)'; noticeBox.textContent = 'end'; recogStatus = 'end'; if(recogCommand) { funcRecogMovAlternate(); return; }//alternate if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'android - sleep';/*sleep(100);*/ r.start();return;} // for debug sleep funcSpeechRecognitionAlternate(); }); /* log sample test_recog.html:60 audiostart test_recog.html:62 soundstart test_recog.html:64 speechstart test_recog.html:65 speechend test_recog.html:63 soundend test_recog.html:61 audioend test_recog.html:60 audiostart test_recog.html:61 audioend */ r.addEventListener('audiostart', function() {recogStatus = 'audiostart'; noticeBox.textContent = 'audiostart';}); r.addEventListener('audioend', function() {recogStatus = 'audioend'; noticeBox.textContent = 'audioend';}); r.addEventListener('soundstart', function() {recogStatus = 'soundstart'; noticeBox.textContent = 'soundstart';}); r.addEventListener('soundend', function() {recogStatus = 'soundend'; noticeBox.textContent = 'soundend';}); r.addEventListener('speechstart', function() {recogStatus = 'speechstart'; noticeBox.textContent = 'speechstart';nFrameRecog = 0;}); //r.addEventListener('speechend', function() {console.log('speechend');r.stop();}); r.addEventListener('speechend', function() {recogStatus = 'speechend'; noticeBox.textContent = 'speechend';nFrameRecog = null; if(userAgent.indexOf('android') != -1){noticeBox.textContent = 'speechend';/*sleep(1000);*/return;} // for debug sleep r.stop();}); r.addEventListener('result', function(e) {noticeBox.textContent = 'result'; //if(recogCommand){return;} console.log(e); if(e.results.length) { var results = e.results[e.results.length-1]; /*if(results.isFinal) { noticeBox.textContent = 'fixed!!\n' + results[e.results.length-1].transcript; r.stop(); } else*/ { var possiblity = [].slice.call(results).map(function(result, i) { var strtrans = result.transcript;console.log(strtrans); var targetCommand; function checkIncludes(strtrans, targetCommand, targetIncludes){ if (strtrans.includes(targetIncludes)){console.log(targetIncludes);noticeBox.textContent = targetCommand;recogCommand=targetCommand;} } targetCommand='右'; checkIncludes(strtrans, targetCommand, '右'); checkIncludes(strtrans, targetCommand, 'み'); checkIncludes(strtrans, targetCommand, 'ミ'); checkIncludes(strtrans, targetCommand, '三'); checkIncludes(strtrans, targetCommand, 'ウィー'); targetCommand='左'; checkIncludes(strtrans, targetCommand, '左'); checkIncludes(strtrans, targetCommand, 'ひ'); checkIncludes(strtrans, targetCommand, '飛騨'); checkIncludes(strtrans, targetCommand, '日田'); checkIncludes(strtrans, targetCommand, 'ヒーター'); targetCommand='上'; checkIncludes(strtrans, targetCommand, '上'); checkIncludes(strtrans, targetCommand, 'う'); checkIncludes(strtrans, targetCommand, 'うえ'); checkIncludes(strtrans, targetCommand, 'AAA'); checkIncludes(strtrans, targetCommand, 'ウェイ'); targetCommand='下'; checkIncludes(strtrans, targetCommand, '下'); checkIncludes(strtrans, targetCommand, 'し'); checkIncludes(strtrans, targetCommand, 'C'); checkIncludes(strtrans, targetCommand, 'シーサー'); checkIncludes(strtrans, targetCommand, '明日'); targetCommand='前'; checkIncludes(strtrans, targetCommand, '前'); checkIncludes(strtrans, targetCommand, 'ま'); targetCommand='奥'; checkIncludes(strtrans, targetCommand, '奥'); checkIncludes(strtrans, targetCommand, 'お'); checkIncludes(strtrans, targetCommand, 'OK'); checkIncludes(strtrans, targetCommand, 'オーク'); checkIncludes(strtrans, targetCommand, '後'); return i + ':' + result.transcript; }).join('\n\n'); noticeBox.textContent = 'progress('+results.length+')...\n' + possiblity; // alternate if(recogCommand) {r.stop();} } } else { noticeBox.textContent = '結果が空です'; } }); r.lang = 'ja-JP'; r.continuous = true; r.interimResults = true; r.maxAlternatives = 10; r.start(); } // ------------------ testRecogMovResultMonitor ------------------ function testRecogMovResultMonitor() { noticeBox.textContent = 'testRecogMovResultMonitor'; //if(userAgent.indexOf('android') != -1){audioContext = new AudioContext();} funcRecogMovResultMonitor(); funcSpeechRecognitionAndroid(); } var isSimpleCtrlPhase = false; var countReceivedResultAfter = 0;var hasReceivedResult = false; var countReceivedResultAfterMax = 5; function funcRecogMovResultMonitor() { // intervalfunc = function () { console.log('intervalfunc recogCommand : ' + recogCommand); noticeBox.textContent = "intervalfunc" + "\n" + "recogCommand : " + recogCommand; if(recogCommand == null){ return; } /* log sample test_recog.html:60 audiostart test_recog.html:62 soundstart test_recog.html:64 speechstart test_recog.html:65 speechend test_recog.html:63 soundend test_recog.html:61 audioend test_recog.html:60 audiostart test_recog.html:61 audioend */ if (recogStatus == 'end' || recogStatus == 'audioend' || recogStatus == 'soundend') { if (isSimpleCtrlPhase) {recogCommand = null; isSimpleCtrlPhase = false; countReceivedResultAfter = 0; return;} } if (recogStatus != 'audiostart' && recogStatus != 'soundstart' && recogStatus != 'speechstart' && recogStatus != 'speechend') { return; } isSimpleCtrlPhase = true; if (recogStatus == 'speechstart') { countReceivedResultAfter += 1; noticeBox.textContent += "\n" + "countReceivedResultAfter : " + countReceivedResultAfter; if (hasReceivedResult) {countReceivedResultAfter = 0;} hasReceivedResult = false; if (countReceivedResultAfter >= countReceivedResultAfterMax) { recogCommand = null; isSimpleCtrlPhase = false; countReceivedResultAfter = 0; return; } } var iframe1 = document.getElementById("iframe1"); var winelem =iframe1.contentWindow; var element = iframe1.contentDocument.getElementById("div_a-scene"); var rotcamera = iframe1.contentDocument.getElementById('camera'); var curposition = rotcamera.getAttribute('position'); //console.log(curposition); // //var rotation = rotcamera.getAttribute('rotation'); // //console.log(rotation); var rottarget = iframe1.contentDocument.getElementById('obj'); //console.log(rottarget); var rotation = rottarget.getAttribute('rotation'); // console.log(rotation); var val_range01 = document.getElementById("range01").value * 1.0 / 255; var varOpe = val_range01; varOpe = 1.0; if (recogStatus == 'audiostart') {varOpe = 0.2;} if (recogStatus == 'soundstart') {} if (recogStatus == 'speechstart'){varOpe = 2.0;} if (recogStatus == 'speechend') {varOpe = 0.1;} if(isMouseOnInputRange01) {varOpe = val_range01;} else { document.getElementById("range01").value = Math.round(varOpe * 255); } var scale = rottarget.getAttribute('scale'); if (scale == null) {scale = new THREE.Vector3();} scaleScaleVol = Math.log(1 + varOpe)+1; var curposition = rottarget.getAttribute('position'); if (curposition == null) {curposition = new THREE.Vector3();} if(recogCommand == '右') curposition.x += scaleScaleVol*scalePosSprt; if(recogCommand == '左') curposition.x -= scaleScaleVol*scalePosSprt; if(recogCommand == '上') curposition.y += scaleScaleVol*scalePosSprt; if(recogCommand == '下') curposition.y -= scaleScaleVol*scalePosSprt; if(recogCommand == '前') curposition.z += scaleScaleVol*scalePosSprt; if(recogCommand == '奥') curposition.z -= scaleScaleVol*scalePosSprt; rottarget.setAttribute('position', curposition); } intervalId = setInterval(intervalfunc, 100); }