From 6fa45b7fdbe850b9da2f8baf68fbc3f5d1785b88 Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期二, 16 五月 2023 09:37:23 +0800
Subject: [PATCH] Merge pull request #513 from alibaba-damo-academy/for-html5-demo

---
 funasr/runtime/html5/static/main.js |  167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 167 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..5f6bb10
--- /dev/null
+++ b/funasr/runtime/html5/static/main.js
@@ -0,0 +1,167 @@
+/**
+ * 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 info_div = document.getElementById('info_div');
+ 
+
+// 璇煶璇嗗埆缁撴灉; 瀵筳sonMsg鏁版嵁瑙f瀽,灏嗚瘑鍒粨鏋滈檮鍔犲埌缂栬緫妗嗕腑
+function getJsonMessage( jsonMsg ) {
+	console.log( "message: " + JSON.parse(jsonMsg.data)['text'] );
+	var rectxt=""+JSON.parse(jsonMsg.data)['text'];
+	var varArea=document.getElementById('varArea');
+	rec_text=rec_text+rectxt.replace(/ +/g,"");
+	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' );
+		setTimeout(function(){btnStart.disabled = true;info_div.innerHTML='connecttion error';}, 4000 );
+	}
+}
+
+
+// 璇嗗埆鍚姩銆佸仠姝€�佹竻绌烘搷浣�
+function start() {
+	info_div.innerHTML="姝e湪杩炴帴asr鏈嶅姟鍣紝璇风瓑寰�...";
+	// 娓呴櫎鏄剧ず
+	clear();
+	//鎺т欢鐘舵�佹洿鏂�
+ 	    
+	isRec = true;
+	btnStart.disabled = true;
+	btnStop.disabled = false;
+	//鍚姩杩炴帴
+	wsconnecter.wsStart();
+}
+
+ 
+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,
+		};
+		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="";
+ 
+}
+
+ 
+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