main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = start;
  20. var btnStop = document.getElementById('btnStop');
  21. btnStop.onclick = stop;
  22. btnStop.disabled = true;
  23. var rec_text=""
  24. var info_div = document.getElementById('info_div');
  25. var now_ipaddress=window.location.href;
  26. now_ipaddress=now_ipaddress.replace("https://","wss://");
  27. now_ipaddress=now_ipaddress.replace("static/index.html","");
  28. document.getElementById('wssip').value=now_ipaddress;
  29. // 语音识别结果; 对jsonMsg数据解析,将识别结果附加到编辑框中
  30. function getJsonMessage( jsonMsg ) {
  31. console.log( "message: " + JSON.parse(jsonMsg.data)['text'] );
  32. var rectxt=""+JSON.parse(jsonMsg.data)['text'];
  33. var varArea=document.getElementById('varArea');
  34. rec_text=rec_text+rectxt.replace(/ +/g,"");
  35. varArea.value=rec_text;
  36. }
  37. // 连接状态响应
  38. function getConnState( connState ) {
  39. if ( connState === 0 ) {
  40. rec.open( function(){
  41. rec.start();
  42. console.log("开始录音");
  43. });
  44. } else if ( connState === 1 ) {
  45. //stop();
  46. } else if ( connState === 2 ) {
  47. stop();
  48. console.log( 'connecttion error' );
  49. alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口,并确保h5服务和asr服务在同一个域内。或换个浏览器试试。");
  50. btnStart.disabled = true;
  51. info_div.innerHTML='请点击开始';
  52. }
  53. }
  54. // 识别启动、停止、清空操作
  55. function start() {
  56. // 清除显示
  57. clear();
  58. //控件状态更新
  59. //启动连接
  60. var ret=wsconnecter.wsStart();
  61. if(ret==1){
  62. isRec = true;
  63. btnStart.disabled = true;
  64. btnStop.disabled = false;
  65. info_div.innerHTML="正在连接asr服务器,请等待...";
  66. }
  67. }
  68. function stop() {
  69. var chunk_size = new Array( 5, 10, 5 );
  70. var request = {
  71. "chunk_size": chunk_size,
  72. "wav_name": "h5",
  73. "is_speaking": false,
  74. "chunk_interval":10,
  75. };
  76. if(sampleBuf.length>0){
  77. wsconnecter.wsSend(sampleBuf,false);
  78. console.log("sampleBuf.length"+sampleBuf.length);
  79. sampleBuf=new Int16Array();
  80. }
  81. wsconnecter.wsSend( JSON.stringify(request) ,false);
  82. // 控件状态更新
  83. isRec = false;
  84. info_div.innerHTML="请等候...";
  85. btnStop.disabled = true;
  86. setTimeout(function(){btnStart.disabled = false;info_div.innerHTML="请点击开始";}, 3000 );
  87. rec.stop(function(blob,duration){
  88. console.log(blob);
  89. var audioBlob = Recorder.pcm2wav(data = {sampleRate:16000, bitRate:16, blob:blob},
  90. function(theblob,duration){
  91. console.log(theblob);
  92. var audio_record = document.getElementById('audio_record');
  93. audio_record.src = (window.URL||webkitURL).createObjectURL(theblob);
  94. audio_record.controls=true;
  95. audio_record.play();
  96. } ,function(msg){
  97. console.log(msg);
  98. }
  99. );
  100. },function(errMsg){
  101. console.log("errMsg: " + errMsg);
  102. });
  103. // 停止连接
  104. }
  105. function clear() {
  106. var varArea=document.getElementById('varArea');
  107. varArea.value="";
  108. rec_text="";
  109. }
  110. function recProcess( buffer, powerLevel, bufferDuration, bufferSampleRate,newBufferIdx,asyncEnd ) {
  111. if ( isRec === true ) {
  112. var data_48k = buffer[buffer.length-1];
  113. var array_48k = new Array(data_48k);
  114. var data_16k=Recorder.SampleData(array_48k,bufferSampleRate,16000).data;
  115. sampleBuf = Int16Array.from([...sampleBuf, ...data_16k]);
  116. var chunk_size=960; // for asr chunk_size [5, 10, 5]
  117. info_div.innerHTML=""+bufferDuration/1000+"s";
  118. while(sampleBuf.length>=chunk_size){
  119. sendBuf=sampleBuf.slice(0,chunk_size);
  120. sampleBuf=sampleBuf.slice(chunk_size,sampleBuf.length);
  121. wsconnecter.wsSend(sendBuf,false);
  122. }
  123. }
  124. }