無名

大猫咪与小狮子

导航

H5采集pcm流转换采样率实时发送到服务端

 1     function startTalk(ws2, button) {
 2         var arrBuffer = new ArrayBuffer(320 * 2);
 3         var sendBuffer = new DataView(arrBuffer);
 4         var sendBufferindex  = 0;
 5         if (navigator.getUserMedia) {
 6             navigator.getUserMedia(
 7                 { audio: true },
 8                 function (stream) {
 9                     button.text("挂 断")
10                     button.attr("status","doing")
11                     var context = new AudioContext();
12                     var audioInput = context.createMediaStreamSource(stream);
13                     var recorder = context.createScriptProcessor(1024, 1, 1);
14                     audioInput.connect(recorder);
15                     recorder.connect(context.destination);
16                     recorder.onaudioprocess = function (e) {
17                         var buffer = e.inputBuffer.getChannelData(0);
18                         var arrayBuffer = context.createBuffer(1, 1024, context.sampleRate);
19                         var nowBuffering = arrayBuffer.getChannelData(0);
20                         for (var i = 0; i < arrayBuffer.length; i++) {
21                             nowBuffering[i] = buffer[i]
22                         }
23                         var offctx = new OfflineAudioContext(1, parseInt(1024*16000/context.sampleRate), 16000);
24                         var source = offctx.createBufferSource();
25                         source.buffer = arrayBuffer;
26                         source.connect(offctx.destination);
27                         source.start();
28                         offctx.startRendering().then(function(renderedBuffer) {
29                             var channetData = renderedBuffer.getChannelData(0)
30                             source.stop();
31                             var index = 0
32                             var length = channetData.length
33                             while (index < length) {
34                                 var selectBuffer = channetData[index]
35                                 var s = Math.max(-1, Math.min(1, selectBuffer));
36                                 var point = s < 0 ? s * 0x8000 : s * 0x7FFF;
37                                 if (sendBufferindex < 320 * 2) {
38                                     sendBuffer.setInt16(sendBufferindex, point, true);
39                                 } else {
40                                     var sendData = {
41                                         "cmd":"pcm",
42                                         "data":{
43                                             "buffer": Array.from(new Int16Array(arrBuffer)),
44                                             "timestamp": new Date().getTime(),
45                                         }
46                                     }
47                                     if(ws2.readyState == ws2.CLOSING || ws2.readyState == ws2.CLOSED) {
48                                         recorder.disconnect()
49                                         return
50                                     }
51                                     ws2.send(JSON.stringify(sendData))
52                                     
53                                     sendBuffer = new DataView(arrBuffer);
54                                     sendBufferindex = 0;
55                                     sendBuffer.setInt16(sendBufferindex, point, true);
56                                 }
57                                 index++;
58                                 sendBufferindex = sendBufferindex + 2;
59                             }
60                         })
61                         
62                     }
63                 },
64                 function() {
65                     ws2.close()
66                     button.attr("status","close")
67                     button.text("通 话")
68                     alert('请插入耳机');
69 

posted on 2019-01-26 16:35  xiezhengcai  阅读(2554)  评论(1编辑  收藏  举报