From 5d15727b1e545a17793a5776bbe5c35b87654398 Mon Sep 17 00:00:00 2001
From: zhaomingwork <61895407+zhaomingwork@users.noreply.github.com>
Date: 星期一, 04 九月 2023 17:16:04 +0800
Subject: [PATCH] set wav sample rate and PCM for h5 (#919)

---
 funasr/runtime/html5/static/wsconnecter.js |    5 ++
 funasr/runtime/html5/static/main.js        |   93 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 94 insertions(+), 4 deletions(-)

diff --git a/funasr/runtime/html5/static/main.js b/funasr/runtime/html5/static/main.js
index 89f6e40..38811cd 100644
--- a/funasr/runtime/html5/static/main.js
+++ b/funasr/runtime/html5/static/main.js
@@ -45,6 +45,7 @@
 
 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;
@@ -70,6 +71,7 @@
 		}
 	
 }
+
 upfile.onclick=function()
 {
 		btnStart.disabled = true;
@@ -77,25 +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 鍗曟垨鍙屽0閬�
+			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()
diff --git a/funasr/runtime/html5/static/wsconnecter.js b/funasr/runtime/html5/static/wsconnecter.js
index cc6129f..ee0bcca 100644
--- a/funasr/runtime/html5/static/wsconnecter.js
+++ b/funasr/runtime/html5/static/wsconnecter.js
@@ -77,6 +77,11 @@
 		if(isfilemode)
 		{
 			request.wav_format=file_ext;
+			if(file_ext=="wav")
+			{
+				request.wav_format="PCM";
+				request.audio_fs=file_sample_rate;
+			}
 		}
 		var hotwords=getHotwords();
 		if(hotwords.length>0)

--
Gitblit v1.9.1