From bc723ea200144bd6fa8a5dff4b9a780feda144fc Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 29 六月 2023 18:55:01 +0800
Subject: [PATCH] dcos

---
 funasr/runtime/html5/static/main.js |  134 ++++++++++++++++++++++++++++----------------
 1 files changed, 85 insertions(+), 49 deletions(-)

diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
index 35e533a..4a50801 100644
--- a/funasr/runtime/html5/static/main.js
+++ b/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
@@ -42,9 +45,37 @@
 
 var isfilemode=false;  // if it is in file mode
 var file_data_array;  // array to save file data
-var isconnected=0;    // for file rec, 0 is not begin, 1 is connected, -1 is error
-var totalsend=0;
  
+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++) {
@@ -54,7 +85,7 @@
                  var audioblob= fileAudio.result;
 				 file_data_array=audioblob;
 				 console.log(audioblob);
-                 btnConnect.disabled = false;
+                  
                  info_div.innerHTML='璇风偣鍑昏繛鎺ヨ繘琛岃瘑鍒�';
                
                 }
@@ -70,7 +101,7 @@
 		  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()
 {
@@ -87,7 +118,7 @@
 		    sendBuf=sampleBuf.slice(0,chunk_size);
 			totalsend=totalsend+sampleBuf.length;
 			sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
-			wsconnecter.wsSend(sendBuf,false);
+			wsconnecter.wsSend(sendBuf);
  
 		 
 		}
@@ -97,24 +128,7 @@
  
 
 }
-function start_file_offline()
-{             
-           	  console.log("start_file_offline",isconnected);  
-              if(isconnected==-1)
-			  {
-				  return;
-			  }
-		      if(isconnected==0){
-			   
-		        setTimeout(start_file_offline, 1000);
-				return;
-		      }
-			start_file_send();
  
-	         
-
-		 
-}
 	
 function on_recoder_mode_change()
 {
@@ -133,14 +147,20 @@
 				document.getElementById("mic_mode_div").style.display = 'block';
 				document.getElementById("rec_mode_div").style.display = 'none';
  
-				btnConnect.disabled=false;
+ 
+		        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';
-                btnConnect.disabled = true;
+ 
+		        btnStart.disabled = true;
+		        btnStop.disabled = true;
+		        btnConnect.disabled=true;
 			    isfilemode=true;
 				info_div.innerHTML='璇风偣鍑婚�夋嫨鏂囦欢';
 			    
@@ -195,7 +215,7 @@
 		wsconnecter.wsStop();
         
 		info_div.innerHTML="璇风偣鍑昏繛鎺�";
-		isconnected=0;
+ 
 		btnStart.disabled = true;
 		btnStop.disabled = true;
 		btnConnect.disabled=false;
@@ -207,12 +227,19 @@
 
 // 杩炴帴鐘舵�佸搷搴�
 function getConnState( connState ) {
-	if ( connState === 0 ) {
+	if ( connState === 0 ) { //on open
  
  
 		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();
@@ -220,9 +247,11 @@
 		stop();
 		console.log( 'connecttion error' );
 		 
-		alert("杩炴帴鍦板潃"+document.getElementById('wssip').value+"澶辫触,璇锋鏌sr鍦板潃鍜岀鍙o紝骞剁‘淇漢5鏈嶅姟鍜宎sr鏈嶅姟鍦ㄥ悓涓�涓煙鍐呫�傛垨鎹釜娴忚鍣ㄨ瘯璇曘��");
+		alert("杩炴帴鍦板潃"+document.getElementById('wssip').value+"澶辫触,璇锋鏌sr鍦板潃鍜岀鍙c�傛垨璇曡瘯鐣岄潰涓婃墜鍔ㄦ巿鏉冿紝鍐嶈繛鎺ャ��");
 		btnStart.disabled = true;
-		isconnected=0;
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+ 
  
 		info_div.innerHTML='璇风偣鍑昏繛鎺�';
 	}
@@ -234,7 +263,9 @@
 		 rec.open( function(){
 		 rec.start();
 		 console.log("寮�濮�");
-		 btnStart.disabled = true;
+			btnStart.disabled = true;
+			btnStop.disabled = false;
+			btnConnect.disabled=true;
 		 });
  
 }
@@ -247,27 +278,29 @@
 	// 娓呴櫎鏄剧ず
 	clear();
 	//鎺т欢鐘舵�佹洿鏂�
- 	console.log("isfilemode"+isfilemode+","+isconnected);
-    info_div.innerHTML="姝e湪杩炴帴asr鏈嶅姟鍣紝璇风瓑寰�...";
+ 	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 = false;
-		btnStop.disabled = false;
+		btnStart.disabled = true;
+		btnStop.disabled = true;
 		btnConnect.disabled=true;
-		if (isfilemode)
-		{
-                 console.log("start file now");
-			     start_file_offline();
  
-				 btnStart.disabled = true;
-		         btnStop.disabled = true;
-		         btnConnect.disabled = true;
-		}
         return 1;
 	}
-	return 0;
+	else
+	{
+		info_div.innerHTML="璇风偣鍑诲紑濮�";
+		btnStart.disabled = true;
+		btnStop.disabled = true;
+		btnConnect.disabled=false;
+ 
+		return 0;
+	}
 }
 
  
@@ -282,17 +315,17 @@
 		};
 		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) );
  
 	  
 	
 	 
 
-	 //isconnected=0;
+ 
 	// 鎺т欢鐘舵�佹洿鏂�
 	
 	isRec = false;
@@ -301,12 +334,15 @@
    if(isfilemode==false){
 	    btnStop.disabled = true;
 		btnStart.disabled = true;
-		btnConnect.disabled=false;
+		btnConnect.disabled=true;
+		//wait 3s for asr result
 	  setTimeout(function(){
 		console.log("call stop ws!");
 		wsconnecter.wsStop();
-        isconnected=0;
+		btnConnect.disabled=false;
 		info_div.innerHTML="璇风偣鍑昏繛鎺�";}, 3000 );
+ 
+ 
 	   
 	rec.stop(function(blob,duration){
   
@@ -317,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){
@@ -361,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