游雁
2023-08-30 c2e4e3c2e9be855277d9f4fa9cd0544892ff829a
funasr/runtime/html5/static/main.js
@@ -31,6 +31,9 @@
 
btnConnect= document.getElementById('btnConnect');
btnConnect.onclick = start;
var awsslink= document.getElementById('wsslink');
 
var rec_text="";  // for online rec asr result
var offline_text=""; // for offline rec asr result
@@ -41,10 +44,32 @@
 
var isfilemode=false;  // if it is in file mode
var file_ext="";
var file_data_array;  // array to save file data
 
var totalsend=0;
var now_ipaddress=window.location.href;
now_ipaddress=now_ipaddress.replace("https://","wss://");
now_ipaddress=now_ipaddress.replace("static/index.html","");
var localport=window.location.port;
now_ipaddress=now_ipaddress.replace(localport,"10095");
document.getElementById('wssip').value=now_ipaddress;
addresschange();
function addresschange()
{
    var Uri = document.getElementById('wssip').value;
   document.getElementById('info_wslink').innerHTML="点此处手工授权(IOS手机)";
   Uri=Uri.replace(/wss/g,"https");
   console.log("addresschange uri=",Uri);
   awsslink.onclick=function(){
      window.open(Uri, '_blank');
      }
}
upfile.onclick=function()
{
      btnStart.disabled = true;
@@ -57,6 +82,8 @@
            for(let i = 0; i < len; i++) {
                let fileAudio = new FileReader();
                fileAudio.readAsArrayBuffer(this.files[i]);  
            file_ext=this.files[i].name.split('.').pop().toLowerCase();
                fileAudio.onload = function() {
                 var audioblob= fileAudio.result;
             file_data_array=audioblob;
@@ -77,11 +104,11 @@
        var audio_record = document.getElementById('audio_record');
        audio_record.src =  (window.URL||webkitURL).createObjectURL(audioblob); 
          audio_record.controls=true;
        audio_record.play();
        //audio_record.play();  //not auto play
}
function start_file_send()
{
      sampleBuf=new Int16Array( file_data_array );
      sampleBuf=new Uint8Array( file_data_array );
 
      var chunk_size=960; // for asr chunk_size [5, 10, 5]
 
@@ -143,6 +170,17 @@
    
         }
}
function getHotwords(){
  var obj = document.getElementById("varHot");
  if(typeof(obj) == 'undefined' || obj==null || obj.value.length<=0){
   return "";
  }
  let val = obj.value.toString();
  console.log("hotwords="+val);
  return val;
}
function getAsrMode(){
            var item = null;
@@ -164,16 +202,46 @@
         return item;
}
         
function handleWithTimestamp(tmptext,tmptime)
{
   console.log( "tmptext: " + tmptext);
   console.log( "tmptime: " + tmptime);
    if(tmptime==null || tmptime=="undefined" || tmptext.length<=0)
   {
      return tmptext;
   }
   tmptext=tmptext.replace(/。/g, ","); // in case there are a lot of "。"
   var words=tmptext.split(",");
   var jsontime=JSON.parse(tmptime); //JSON.parse(tmptime.replace(/\]\]\[\[/g, "],[")); // in case there are a lot segments by VAD
   var char_index=0;
   var text_withtime="";
   for(var i=0;i<words.length;i++)
   {
   if(words[i]=="undefined"  || words[i].length<=0)
   {
      continue;
   }
        console.log("words===",words[i]);
      console.log( "words: " + words[i]+",time="+jsontime[char_index][0]/1000);
      text_withtime=text_withtime+jsontime[char_index][0]/1000+":"+words[i]+"\n";
      char_index=char_index+words[i].length;
   }
   return text_withtime;
}
// 语音识别结果; 对jsonMsg数据解析,将识别结果附加到编辑框中
function getJsonMessage( jsonMsg ) {
   //console.log(jsonMsg);
   console.log( "message: " + JSON.parse(jsonMsg.data)['text'] );
   var rectxt=""+JSON.parse(jsonMsg.data)['text'];
   var asrmodel=JSON.parse(jsonMsg.data)['mode'];
   if(asrmodel=="2pass-offline")
   var is_final=JSON.parse(jsonMsg.data)['is_final'];
   var timestamp=JSON.parse(jsonMsg.data)['timestamp'];
   if(asrmodel=="2pass-offline" || asrmodel=="offline")
   {
      offline_text=offline_text+rectxt; //.replace(/ +/g,"");
      offline_text=offline_text+handleWithTimestamp(rectxt,timestamp); //rectxt; //.replace(/ +/g,"");
      rec_text=offline_text;
   }
   else
@@ -185,7 +253,7 @@
   varArea.value=rec_text;
   console.log( "offline_text: " + asrmodel+","+offline_text);
   console.log( "rec_text: " + rec_text);
   if (isfilemode==true){
   if (isfilemode==true && is_final==false){
      console.log("call stop ws!");
      play_file();
      wsconnecter.wsStop();
@@ -223,7 +291,7 @@
      stop();
      console.log( 'connecttion error' );
       
      alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口,并确保h5服务和asr服务在同一个域内。或换个浏览器试试。");
      alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口。或试试界面上手动授权,再连接。");
      btnStart.disabled = true;
      btnStop.disabled = true;
      btnConnect.disabled=false;
@@ -329,7 +397,7 @@
      var audio_record = document.getElementById('audio_record');
      audio_record.src =  (window.URL||webkitURL).createObjectURL(theblob); 
        audio_record.controls=true;
      audio_record.play();
      //audio_record.play();
            
   }   ,function(msg){