From cc2c1d1d53dea5d2c45f858d1baa5bd279f47987 Mon Sep 17 00:00:00 2001
From: nichongjia-2007 <nichongjia@gmail.com>
Date: 星期三, 31 五月 2023 14:39:25 +0800
Subject: [PATCH] Merge branch 'main' of https://github.com/alibaba-damo-academy/FunASR

---
 funasr/runtime/html5/static/main.js |  210 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 210 insertions(+), 0 deletions(-)

diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
new file mode 100644
index 0000000..be57df1
--- /dev/null
+++ b/funasr/runtime/html5/static/main.js
@@ -0,0 +1,210 @@
+/**
+ * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
+ * Reserved. MIT License  (https://opensource.org/licenses/MIT)
+ */
+/* 2022-2023 by zhaoming,mali aihealthx.com */
+
+
+// 杩炴帴; 瀹氫箟socket杩炴帴绫诲璞′笌璇煶瀵硅薄
+var wsconnecter = new WebSocketConnectMethod({msgHandle:getJsonMessage,stateHandle:getConnState});
+var audioBlob;
+
+// 褰曢煶; 瀹氫箟褰曢煶瀵硅薄,wav鏍煎紡
+var rec = Recorder({
+	type:"pcm",
+	bitRate:16,
+	sampleRate:16000,
+	onProcess:recProcess
+});
+
+ 
+ 
+ 
+var sampleBuf=new Int16Array();
+// 瀹氫箟鎸夐挳鍝嶅簲浜嬩欢
+var btnStart = document.getElementById('btnStart');
+btnStart.onclick = start;
+var btnStop = document.getElementById('btnStop');
+btnStop.onclick = stop;
+btnStop.disabled = true;
+ 
+
+ 
+var rec_text="";
+var offline_text="";
+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;
+
+function getAsrMode(){
+
+            var item = null;
+            var obj = document.getElementsByName("asr_mode");
+            for (var i = 0; i < obj.length; i++) { //閬嶅巻Radio 
+                if (obj[i].checked) {
+                    item = obj[i].value;  
+					break;
+                }
+		    
+
+           }
+		   console.log("asr mode"+item);
+		   return item;
+}
+		   
+
+// 璇煶璇嗗埆缁撴灉; 瀵筳sonMsg鏁版嵁瑙f瀽,灏嗚瘑鍒粨鏋滈檮鍔犲埌缂栬緫妗嗕腑
+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")
+	{
+		offline_text=offline_text+rectxt.replace(/ +/g,"");
+		rec_text=offline_text;
+	}
+	else
+	{
+		rec_text=rec_text+rectxt.replace(/ +/g,"");
+	}
+	var varArea=document.getElementById('varArea');
+	
+	varArea.value=rec_text;
+	 
+ 
+}
+
+// 杩炴帴鐘舵�佸搷搴�
+function getConnState( connState ) {
+	if ( connState === 0 ) {
+ 
+		rec.open( function(){
+			rec.start();
+			console.log("寮�濮嬪綍闊�");
+ 
+		});
+	} else if ( connState === 1 ) {
+		//stop();
+	} else if ( connState === 2 ) {
+		stop();
+		console.log( 'connecttion error' );
+		 
+		alert("杩炴帴鍦板潃"+document.getElementById('wssip').value+"澶辫触,璇锋鏌sr鍦板潃鍜岀鍙o紝骞剁‘淇漢5鏈嶅姟鍜宎sr鏈嶅姟鍦ㄥ悓涓�涓煙鍐呫�傛垨鎹釜娴忚鍣ㄨ瘯璇曘��");
+		btnStart.disabled = true;
+		info_div.innerHTML='璇风偣鍑诲紑濮�';
+	}
+}
+
+
+// 璇嗗埆鍚姩銆佸仠姝€�佹竻绌烘搷浣�
+function start() {
+	
+	// 娓呴櫎鏄剧ず
+	clear();
+	//鎺т欢鐘舵�佹洿鏂�
+ 	    
+
+	//鍚姩杩炴帴
+	var ret=wsconnecter.wsStart();
+	if(ret==1){
+		isRec = true;
+		btnStart.disabled = true;
+		btnStop.disabled = false;
+	    info_div.innerHTML="姝e湪杩炴帴asr鏈嶅姟鍣紝璇风瓑寰�...";
+	}
+}
+
+ 
+function stop() {
+		var chunk_size = new Array( 5, 10, 5 );
+		var request = {
+			"chunk_size": chunk_size,
+			"wav_name":  "h5",
+			"is_speaking":  false,
+			"chunk_interval":10,
+			"mode":getAsrMode(),
+		};
+		console.log(request);
+		if(sampleBuf.length>0){
+		wsconnecter.wsSend(sampleBuf,false);
+		console.log("sampleBuf.length"+sampleBuf.length);
+		sampleBuf=new Int16Array();
+		}
+	   wsconnecter.wsSend( JSON.stringify(request) ,false);
+ 
+	 
+	
+	
+
+	
+	// 鎺т欢鐘舵�佹洿鏂�
+	isRec = false;
+    info_div.innerHTML="璇风瓑鍊�...";
+	btnStop.disabled = true;
+	setTimeout(function(){btnStart.disabled = false;info_div.innerHTML="璇风偣鍑诲紑濮�";}, 3000 );
+	rec.stop(function(blob,duration){
+  
+		console.log(blob);
+		var audioBlob = Recorder.pcm2wav(data = {sampleRate:16000, bitRate:16, blob:blob},
+		function(theblob,duration){
+				console.log(theblob);
+		var audio_record = document.getElementById('audio_record');
+		audio_record.src =  (window.URL||webkitURL).createObjectURL(theblob); 
+        audio_record.controls=true;
+		audio_record.play(); 
+         	
+
+	}   ,function(msg){
+		 console.log(msg);
+	}
+		);
+ 
+
+ 
+	},function(errMsg){
+		console.log("errMsg: " + errMsg);
+	});
+    // 鍋滄杩炴帴
+	
+    
+
+}
+
+function clear() {
+ 
+    var varArea=document.getElementById('varArea');
+ 
+	varArea.value="";
+    rec_text="";
+	offline_text="";
+ 
+}
+
+ 
+function recProcess( buffer, powerLevel, bufferDuration, bufferSampleRate,newBufferIdx,asyncEnd ) {
+	if ( isRec === true ) {
+		var data_48k = buffer[buffer.length-1];  
+ 
+		var  array_48k = new Array(data_48k);
+		var data_16k=Recorder.SampleData(array_48k,bufferSampleRate,16000).data;
+ 
+		sampleBuf = Int16Array.from([...sampleBuf, ...data_16k]);
+		var chunk_size=960; // for asr chunk_size [5, 10, 5]
+		info_div.innerHTML=""+bufferDuration/1000+"s";
+		while(sampleBuf.length>=chunk_size){
+		    sendBuf=sampleBuf.slice(0,chunk_size);
+			sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
+			wsconnecter.wsSend(sendBuf,false);
+			
+			
+		 
+		}
+		
+ 
+		
+	}
+}
\ No newline at end of file

--
Gitblit v1.9.1