Xian Shi
2023-10-17 a05e753d11d9c36983ec4e58c421dbcf86d1dcd4
funasr/runtime/html5/static/main.js
@@ -44,6 +44,8 @@
 
var isfilemode=false;  // if it is in file mode
var file_ext="";
var file_sample_rate=16000; //for wav file sample rate
var file_data_array;  // array to save file data
 
var totalsend=0;
@@ -69,6 +71,7 @@
      }
   
}
upfile.onclick=function()
{
      btnStart.disabled = true;
@@ -76,23 +79,108 @@
      btnConnect.disabled=false;
   
}
// from https://github.com/xiangyuecn/Recorder/tree/master
var readWavInfo=function(bytes){
   //读取wav文件头,统一成44字节的头
   if(bytes.byteLength<44){
      return null;
   };
   var wavView=bytes;
   var eq=function(p,s){
      for(var i=0;i<s.length;i++){
         if(wavView[p+i]!=s.charCodeAt(i)){
            return false;
         };
      };
      return true;
   };
   if(eq(0,"RIFF")&&eq(8,"WAVEfmt ")){
      var numCh=wavView[22];
      if(wavView[20]==1 && (numCh==1||numCh==2)){//raw pcm 单或双声道
         var sampleRate=wavView[24]+(wavView[25]<<8)+(wavView[26]<<16)+(wavView[27]<<24);
         var bitRate=wavView[34]+(wavView[35]<<8);
         var heads=[wavView.subarray(0,12)],headSize=12;//head只保留必要的块
         //搜索data块的位置
         var dataPos=0; // 44 或有更多块
         for(var i=12,iL=wavView.length-8;i<iL;){
            if(wavView[i]==100&&wavView[i+1]==97&&wavView[i+2]==116&&wavView[i+3]==97){//eq(i,"data")
               heads.push(wavView.subarray(i,i+8));
               headSize+=8;
               dataPos=i+8;break;
            }
            var i0=i;
            i+=4;
            i+=4+wavView[i]+(wavView[i+1]<<8)+(wavView[i+2]<<16)+(wavView[i+3]<<24);
            if(i0==12){//fmt
               heads.push(wavView.subarray(i0,i));
               headSize+=i-i0;
            }
         }
         if(dataPos){
            var wavHead=new Uint8Array(headSize);
            for(var i=0,n=0;i<heads.length;i++){
               wavHead.set(heads[i],n);n+=heads[i].length;
            }
            return {
               sampleRate:sampleRate
               ,bitRate:bitRate
               ,numChannels:numCh
               ,wavHead44:wavHead
               ,dataPos:dataPos
            };
         };
      };
   };
   return null;
};
upfile.onchange = function () {
      var len = this.files.length;  
            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();
                var audioblob;
                fileAudio.onload = function() {
                 var audioblob= fileAudio.result;
                audioblob = fileAudio.result;
             file_data_array=audioblob;
             console.log(audioblob);
                  
                 info_div.innerHTML='请点击连接进行识别';
                }
          fileAudio.onerror = function(e) {
            console.log('error' + e);
          }
            }
         // for wav file, we  get the sample rate
         if(file_ext=="wav")
            for(let i = 0; i < len; i++) {
                let fileAudio = new FileReader();
                fileAudio.readAsArrayBuffer(this.files[i]);
                fileAudio.onload = function() {
                audioblob = new Uint8Array(fileAudio.result);
            // for wav file, we can get the sample rate
            var info=readWavInfo(audioblob);
               console.log(info);
               file_sample_rate=info.sampleRate;
                }
      
            }
        }
function play_file()
@@ -105,7 +193,7 @@
}
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]
 
@@ -167,6 +255,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;
@@ -188,7 +287,34 @@
         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);
@@ -196,9 +322,11 @@
   var rectxt=""+JSON.parse(jsonMsg.data)['text'];
   var asrmodel=JSON.parse(jsonMsg.data)['mode'];
   var is_final=JSON.parse(jsonMsg.data)['is_final'];
   if(asrmodel=="2pass-offline")
   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