main.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaoming,mali aihealthx.com */
  6. // 连接; 定义socket连接类对象与语音对象
  7. var wsconnecter = new WebSocketConnectMethod({msgHandle:getJsonMessage,stateHandle:getConnState});
  8. var audioBlob;
  9. // 录音; 定义录音对象,wav格式
  10. var rec = Recorder({
  11. type:"pcm",
  12. bitRate:16,
  13. sampleRate:16000,
  14. onProcess:recProcess
  15. });
  16. var sampleBuf=new Int16Array();
  17. // 定义按钮响应事件
  18. var btnStart = document.getElementById('btnStart');
  19. btnStart.onclick = record;
  20. var btnStop = document.getElementById('btnStop');
  21. btnStop.onclick = stop;
  22. btnStop.disabled = true;
  23. btnStart.disabled = true;
  24. btnConnect= document.getElementById('btnConnect');
  25. btnConnect.onclick = start;
  26. var rec_text="";
  27. var offline_text="";
  28. var info_div = document.getElementById('info_div');
  29. //var now_ipaddress=window.location.href;
  30. //now_ipaddress=now_ipaddress.replace("https://","wss://");
  31. //now_ipaddress=now_ipaddress.replace("static/index.html","");
  32. //document.getElementById('wssip').value=now_ipaddress;
  33. function getAsrMode(){
  34. var item = null;
  35. var obj = document.getElementsByName("asr_mode");
  36. for (var i = 0; i < obj.length; i++) { //遍历Radio
  37. if (obj[i].checked) {
  38. item = obj[i].value;
  39. break;
  40. }
  41. }
  42. console.log("asr mode"+item);
  43. return item;
  44. }
  45. // 语音识别结果; 对jsonMsg数据解析,将识别结果附加到编辑框中
  46. function getJsonMessage( jsonMsg ) {
  47. //console.log(jsonMsg);
  48. console.log( "message: " + JSON.parse(jsonMsg.data)['text'] );
  49. var rectxt=""+JSON.parse(jsonMsg.data)['text'];
  50. var asrmodel=JSON.parse(jsonMsg.data)['mode'];
  51. if(asrmodel=="2pass-offline")
  52. {
  53. offline_text=offline_text+rectxt; //.replace(/ +/g,"");
  54. rec_text=offline_text;
  55. }
  56. else
  57. {
  58. rec_text=rec_text+rectxt; //.replace(/ +/g,"");
  59. }
  60. var varArea=document.getElementById('varArea');
  61. varArea.value=rec_text;
  62. console.log( "offline_text: " + asrmodel+","+offline_text);
  63. console.log( "rec_text: " + rec_text);
  64. }
  65. // 连接状态响应
  66. function getConnState( connState ) {
  67. if ( connState === 0 ) {
  68. //rec.open( function(){
  69. // rec.start();
  70. // console.log("开始录音");
  71. //});
  72. btnStart.disabled = false;
  73. btnConnect.disabled = true;
  74. info_div.innerHTML='连接成功!请点击开始';
  75. } else if ( connState === 1 ) {
  76. //stop();
  77. } else if ( connState === 2 ) {
  78. stop();
  79. console.log( 'connecttion error' );
  80. alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口,并确保h5服务和asr服务在同一个域内。或换个浏览器试试。");
  81. btnStart.disabled = true;
  82. info_div.innerHTML='请点击连接';
  83. }
  84. }
  85. function record()
  86. {
  87. rec.open( function(){
  88. rec.start();
  89. console.log("开始");
  90. btnStart.disabled = true;
  91. });
  92. }
  93. // 识别启动、停止、清空操作
  94. function start() {
  95. // 清除显示
  96. clear();
  97. //控件状态更新
  98. info_div.innerHTML="正在连接asr服务器,请等待...";
  99. //启动连接
  100. var ret=wsconnecter.wsStart();
  101. if(ret==1){
  102. isRec = true;
  103. btnStart.disabled = true;
  104. btnStop.disabled = false;
  105. btnConnect.disabled=true;
  106. }
  107. }
  108. function stop() {
  109. var chunk_size = new Array( 5, 10, 5 );
  110. var request = {
  111. "chunk_size": chunk_size,
  112. "wav_name": "h5",
  113. "is_speaking": false,
  114. "chunk_interval":10,
  115. "mode":getAsrMode(),
  116. };
  117. console.log(request);
  118. if(sampleBuf.length>0){
  119. wsconnecter.wsSend(sampleBuf,false);
  120. console.log("sampleBuf.length"+sampleBuf.length);
  121. sampleBuf=new Int16Array();
  122. }
  123. wsconnecter.wsSend( JSON.stringify(request) ,false);
  124. // 控件状态更新
  125. isRec = false;
  126. info_div.innerHTML="请等候...";
  127. btnStop.disabled = true;
  128. setTimeout(function(){
  129. console.log("call stop ws!");
  130. wsconnecter.wsStop();
  131. btnStart.disabled = true;
  132. btnConnect.disabled=false;
  133. info_div.innerHTML="请点击连接";}, 3000 );
  134. rec.stop(function(blob,duration){
  135. console.log(blob);
  136. var audioBlob = Recorder.pcm2wav(data = {sampleRate:16000, bitRate:16, blob:blob},
  137. function(theblob,duration){
  138. console.log(theblob);
  139. var audio_record = document.getElementById('audio_record');
  140. audio_record.src = (window.URL||webkitURL).createObjectURL(theblob);
  141. audio_record.controls=true;
  142. audio_record.play();
  143. } ,function(msg){
  144. console.log(msg);
  145. }
  146. );
  147. },function(errMsg){
  148. console.log("errMsg: " + errMsg);
  149. });
  150. // 停止连接
  151. }
  152. function clear() {
  153. var varArea=document.getElementById('varArea');
  154. varArea.value="";
  155. rec_text="";
  156. offline_text="";
  157. }
  158. function recProcess( buffer, powerLevel, bufferDuration, bufferSampleRate,newBufferIdx,asyncEnd ) {
  159. if ( isRec === true ) {
  160. var data_48k = buffer[buffer.length-1];
  161. var array_48k = new Array(data_48k);
  162. var data_16k=Recorder.SampleData(array_48k,bufferSampleRate,16000).data;
  163. sampleBuf = Int16Array.from([...sampleBuf, ...data_16k]);
  164. var chunk_size=960; // for asr chunk_size [5, 10, 5]
  165. info_div.innerHTML=""+bufferDuration/1000+"s";
  166. while(sampleBuf.length>=chunk_size){
  167. sendBuf=sampleBuf.slice(0,chunk_size);
  168. sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
  169. wsconnecter.wsSend(sendBuf,false);
  170. }
  171. }
  172. }