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 

转载于:https://www.cnblogs.com/xiezhengcai/p/10324108.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值