From 012903e42ec890ab5c50137beb365c3d94e731d1 Mon Sep 17 00:00:00 2001
From: nichongjia-2007 <nichongjia@gmail.com>
Date: 星期五, 30 六月 2023 11:21:28 +0800
Subject: [PATCH] Merge branch 'main' of https://github.com/alibaba-damo-academy/FunASR

---
 funasr/runtime/html5/static/main.js |  261 +++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 229 insertions(+), 32 deletions(-)

diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
index 9317778..4a50801 100644
--- a/funasr/runtime/html5/static/main.js
+++ b/funasr/runtime/html5/static/main.js
@@ -23,22 +23,150 @@
 var sampleBuf=new Int16Array();
 // 瀹氫箟鎸夐挳鍝嶅簲浜嬩欢
 var btnStart = document.getElementById('btnStart');
-btnStart.onclick = start;
+btnStart.onclick = record;
 var btnStop = document.getElementById('btnStop');
 btnStop.onclick = stop;
 btnStop.disabled = true;
+btnStart.disabled = true;
  
+btnConnect= document.getElementById('btnConnect');
+btnConnect.onclick = start;
+
+var awsslink= document.getElementById('wsslink');
 
  
-var rec_text="";
-var offline_text="";
+var rec_text="";  // for online rec asr result
+var offline_text=""; // for offline rec asr result
 var info_div = document.getElementById('info_div');
 
-//var now_ipaddress=window.location.href;
-//now_ipaddress=now_ipaddress.replace("https://","wss://");
-//now_ipaddress=now_ipaddress.replace("static/index.html","");
-//document.getElementById('wssip').value=now_ipaddress;
+var upfile = document.getElementById('upfile');
 
+ 
+
+var isfilemode=false;  // if it is in file mode
+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;
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+	
+}
+upfile.onchange = function () {
+銆�銆�銆�銆�銆�銆�var len = this.files.length;  
+            for(let i = 0; i < len; i++) {
+                let fileAudio = new FileReader();
+                fileAudio.readAsArrayBuffer(this.files[i]);  
+                fileAudio.onload = function() {
+                 var audioblob= fileAudio.result;
+				 file_data_array=audioblob;
+				 console.log(audioblob);
+                  
+                 info_div.innerHTML='璇风偣鍑昏繛鎺ヨ繘琛岃瘑鍒�';
+               
+                }
+銆�銆�銆�銆�銆�銆�銆�銆�銆�銆�fileAudio.onerror = function(e) {
+銆�銆�銆�銆�銆�銆�銆�銆�銆�銆�銆�銆�console.log('error' + e);
+銆�銆�銆�銆�銆�銆�銆�銆�銆�銆�}
+            }
+        }
+
+function play_file()
+{
+		  var audioblob=new Blob( [ new Uint8Array(file_data_array)] , {type :"audio/wav"});
+		  var audio_record = document.getElementById('audio_record');
+		  audio_record.src =  (window.URL||webkitURL).createObjectURL(audioblob); 
+          audio_record.controls=true;
+		  //audio_record.play();  //not auto play
+}
+function start_file_send()
+{
+		sampleBuf=new Int16Array( file_data_array );
+ 
+		var chunk_size=960; // for asr chunk_size [5, 10, 5]
+ 
+
+ 
+		
+ 
+		while(sampleBuf.length>=chunk_size){
+			
+		    sendBuf=sampleBuf.slice(0,chunk_size);
+			totalsend=totalsend+sampleBuf.length;
+			sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
+			wsconnecter.wsSend(sendBuf);
+ 
+		 
+		}
+ 
+		stop();
+
+ 
+
+}
+ 
+	
+function on_recoder_mode_change()
+{
+            var item = null;
+            var obj = document.getElementsByName("recoder_mode");
+            for (var i = 0; i < obj.length; i++) { //閬嶅巻Radio 
+                if (obj[i].checked) {
+                    item = obj[i].value;  
+					break;
+                }
+		    
+
+           }
+		    if(item=="mic")
+			{
+				document.getElementById("mic_mode_div").style.display = 'block';
+				document.getElementById("rec_mode_div").style.display = 'none';
+ 
+ 
+		        btnStart.disabled = true;
+		        btnStop.disabled = true;
+		        btnConnect.disabled=false;
+				isfilemode=false;
+			}
+			else
+			{
+				document.getElementById("mic_mode_div").style.display = 'none';
+				document.getElementById("rec_mode_div").style.display = 'block';
+ 
+		        btnStart.disabled = true;
+		        btnStop.disabled = true;
+		        btnConnect.disabled=true;
+			    isfilemode=true;
+				info_div.innerHTML='璇风偣鍑婚�夋嫨鏂囦欢';
+			    
+	 
+			}
+}
 function getAsrMode(){
 
             var item = null;
@@ -51,7 +179,12 @@
 		    
 
            }
+            if(isfilemode)
+			{
+				item= "offline";
+			}
 		   console.log("asr mode"+item);
+		   
 		   return item;
 }
 		   
@@ -64,41 +197,80 @@
 	var asrmodel=JSON.parse(jsonMsg.data)['mode'];
 	if(asrmodel=="2pass-offline")
 	{
-		offline_text=offline_text+rectxt.replace(/ +/g,"");
+		offline_text=offline_text+rectxt; //.replace(/ +/g,"");
 		rec_text=offline_text;
 	}
 	else
 	{
-		rec_text=rec_text+rectxt.replace(/ +/g,"");
+		rec_text=rec_text+rectxt; //.replace(/ +/g,"");
 	}
 	var varArea=document.getElementById('varArea');
 	
 	varArea.value=rec_text;
+	console.log( "offline_text: " + asrmodel+","+offline_text);
+	console.log( "rec_text: " + rec_text);
+	if (isfilemode==true){
+		console.log("call stop ws!");
+		play_file();
+		wsconnecter.wsStop();
+        
+		info_div.innerHTML="璇风偣鍑昏繛鎺�";
+ 
+		btnStart.disabled = true;
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+	}
+	
 	 
  
 }
 
 // 杩炴帴鐘舵�佸搷搴�
 function getConnState( connState ) {
-	if ( connState === 0 ) {
+	if ( connState === 0 ) { //on open
  
-		rec.open( function(){
-			rec.start();
-			console.log("寮�濮嬪綍闊�");
  
-		});
+		info_div.innerHTML='杩炴帴鎴愬姛!璇风偣鍑诲紑濮�';
+		if (isfilemode==true){
+			info_div.innerHTML='璇疯�愬績绛夊緟,澶ф枃浠剁瓑寰呮椂闂存洿闀�';
+			start_file_send();
+		}
+		else
+		{
+			btnStart.disabled = false;
+			btnStop.disabled = true;
+			btnConnect.disabled=true;
+		}
 	} else if ( connState === 1 ) {
 		//stop();
 	} else if ( connState === 2 ) {
 		stop();
 		console.log( 'connecttion error' );
 		 
-		alert("杩炴帴鍦板潃"+document.getElementById('wssip').value+"澶辫触,璇锋鏌sr鍦板潃鍜岀鍙o紝骞剁‘淇漢5鏈嶅姟鍜宎sr鏈嶅姟鍦ㄥ悓涓�涓煙鍐呫�傛垨鎹釜娴忚鍣ㄨ瘯璇曘��");
+		alert("杩炴帴鍦板潃"+document.getElementById('wssip').value+"澶辫触,璇锋鏌sr鍦板潃鍜岀鍙c�傛垨璇曡瘯鐣岄潰涓婃墜鍔ㄦ巿鏉冿紝鍐嶈繛鎺ャ��");
 		btnStart.disabled = true;
-		info_div.innerHTML='璇风偣鍑诲紑濮�';
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+ 
+ 
+		info_div.innerHTML='璇风偣鍑昏繛鎺�';
 	}
 }
 
+function record()
+{
+ 
+		 rec.open( function(){
+		 rec.start();
+		 console.log("寮�濮�");
+			btnStart.disabled = true;
+			btnStop.disabled = false;
+			btnConnect.disabled=true;
+		 });
+ 
+}
+
+ 
 
 // 璇嗗埆鍚姩銆佸仠姝€�佹竻绌烘搷浣�
 function start() {
@@ -106,15 +278,28 @@
 	// 娓呴櫎鏄剧ず
 	clear();
 	//鎺т欢鐘舵�佹洿鏂�
- 	    
-
+ 	console.log("isfilemode"+isfilemode);
+    
 	//鍚姩杩炴帴
 	var ret=wsconnecter.wsStart();
+	// 1 is ok, 0 is error
 	if(ret==1){
+		info_div.innerHTML="姝e湪杩炴帴asr鏈嶅姟鍣紝璇风瓑寰�...";
 		isRec = true;
 		btnStart.disabled = true;
-		btnStop.disabled = false;
-	    info_div.innerHTML="姝e湪杩炴帴asr鏈嶅姟鍣紝璇风瓑寰�...";
+		btnStop.disabled = true;
+		btnConnect.disabled=true;
+ 
+        return 1;
+	}
+	else
+	{
+		info_div.innerHTML="璇风偣鍑诲紑濮�";
+		btnStart.disabled = true;
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+ 
+		return 0;
 	}
 }
 
@@ -130,24 +315,35 @@
 		};
 		console.log(request);
 		if(sampleBuf.length>0){
-		wsconnecter.wsSend(sampleBuf,false);
+		wsconnecter.wsSend(sampleBuf);
 		console.log("sampleBuf.length"+sampleBuf.length);
 		sampleBuf=new Int16Array();
 		}
-	   wsconnecter.wsSend( JSON.stringify(request) ,false);
+	   wsconnecter.wsSend( JSON.stringify(request) );
  
+	  
+	
 	 
-	
-	
 
-	
+ 
 	// 鎺т欢鐘舵�佹洿鏂�
+	
 	isRec = false;
-    info_div.innerHTML="璇风瓑鍊�...";
-	btnStop.disabled = true;
-	setTimeout(function(){
+    info_div.innerHTML="鍙戦�佸畬鏁版嵁,璇风瓑鍊�,姝e湪璇嗗埆...";
+
+   if(isfilemode==false){
+	    btnStop.disabled = true;
+		btnStart.disabled = true;
+		btnConnect.disabled=true;
+		//wait 3s for asr result
+	  setTimeout(function(){
 		console.log("call stop ws!");
-		wsconnecter.wsStop();btnStart.disabled = false;info_div.innerHTML="璇风偣鍑诲紑濮�";}, 3000 );
+		wsconnecter.wsStop();
+		btnConnect.disabled=false;
+		info_div.innerHTML="璇风偣鍑昏繛鎺�";}, 3000 );
+ 
+ 
+	   
 	rec.stop(function(blob,duration){
   
 		console.log(blob);
@@ -157,7 +353,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){
@@ -170,8 +366,9 @@
 	},function(errMsg){
 		console.log("errMsg: " + errMsg);
 	});
+   }
     // 鍋滄杩炴帴
-	
+ 
     
 
 }
@@ -200,7 +397,7 @@
 		while(sampleBuf.length>=chunk_size){
 		    sendBuf=sampleBuf.slice(0,chunk_size);
 			sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
-			wsconnecter.wsSend(sendBuf,false);
+			wsconnecter.wsSend(sendBuf);
 			
 			
 		 

--
Gitblit v1.9.1