zhaomingwork
2023-06-12 d7e3ae17c3ac056752ecf5c41ff9a96645fcad49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
 * 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 = record;
var btnStop = document.getElementById('btnStop');
btnStop.onclick = stop;
btnStop.disabled = true;
btnStart.disabled = true;
 
btnConnect= document.getElementById('btnConnect');
btnConnect.onclick = start;
 
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;
}
           
 
// 语音识别结果; 对jsonMsg数据解析,将识别结果附加到编辑框中
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("开始录音");
 
        //});
        btnStart.disabled = false;
        btnConnect.disabled = true;
        info_div.innerHTML='连接成功!请点击开始';
    } else if ( connState === 1 ) {
        //stop();
    } else if ( connState === 2 ) {
        stop();
        console.log( 'connecttion error' );
         
        alert("连接地址"+document.getElementById('wssip').value+"失败,请检查asr地址和端口,并确保h5服务和asr服务在同一个域内。或换个浏览器试试。");
        btnStart.disabled = true;
 
        info_div.innerHTML='请点击连接';
    }
}
 
function record()
{
         rec.open( function(){
         rec.start();
         console.log("开始");
         btnStart.disabled = true;
         });
}
// 识别启动、停止、清空操作
function start() {
    
    // 清除显示
    clear();
    //控件状态更新
         
    info_div.innerHTML="正在连接asr服务器,请等待...";
    //启动连接
    var ret=wsconnecter.wsStart();
    if(ret==1){
        isRec = true;
        btnStart.disabled = true;
        btnStop.disabled = false;
        btnConnect.disabled=true;
 
    }
}
 
 
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(){
        console.log("call stop ws!");
        wsconnecter.wsStop();
        btnStart.disabled = true;
        btnConnect.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);
            
            
         
        }
        
 
        
    }
}