Audio 通过websocket从麦克风传输音频。我可以看到正在发送的数据,但在接收客户端听不到

Audio 通过websocket从麦克风传输音频。我可以看到正在发送的数据,但在接收客户端听不到,audio,audiocontext,Audio,Audiocontext,我正在尝试通过websocket广播捕获的麦克风音频。我可以看到正在发送缓冲区数组,该数组具有实际的有效数据,但接收客户端无法听到。我很确定我的回放功能是正确的,因为我可以通过用随机数填充数组并使用回放功能来听到白噪声。我在想,可能它播放的音频太安静了,听不见,因为阵列中生成的数字似乎大多在.000范围内。有什么想法吗?捕获麦克风音频和广播似乎过于复杂…:/ //broadcasting side navigator.mediaDevices.getUserMedia({audio: t

我正在尝试通过websocket广播捕获的麦克风音频。我可以看到正在发送缓冲区数组,该数组具有实际的有效数据,但接收客户端无法听到。我很确定我的回放功能是正确的,因为我可以通过用随机数填充数组并使用回放功能来听到白噪声。我在想,可能它播放的音频太安静了,听不见,因为阵列中生成的数字似乎大多在.000范围内。有什么想法吗?捕获麦克风音频和广播似乎过于复杂…:/

 //broadcasting side 
  navigator.mediaDevices.getUserMedia({audio: true,video: false}) // request cam
        .then(stream => {
         vid.srcObject = stream;
         context = new AudioContext();
         var source = context.createMediaStreamSource(stream);
         var processor = context.createScriptProcessor(1024, 2, 2);
         source.connect(processor);
         processor.connect(context.destination);

         processor.onaudioprocess = function(e) {
           audiodata = e.inputBuffer.getChannelData(1);
          socket.send(JSON.stringify({sound: audiodata, to: to, from: '$username', text:''}));
         };
         return vid.play(); // returns a Promise
       });



//receiving side object to array 

       if(typeof (message.sound) != "undefined"){
         //$('#video_stream_btn').trigger('click');
          var json_sound = message.sound;
          var array_sound = [];
          for(var i in json_sound){
            array_sound.push([i, json_sound [i]]);
          }
         if(typeof(context) == 'undefined'){
           context = new AudioContext();
         }
         play_sound(array_sound, context);
         return;
       }


// receiving side play sound function 

    function play_sound(raw,context){

        //alert(raw.length);
        var audioBuffer = context.createBuffer(1, raw.length, context.sampleRate);
        audioBuffer.getChannelData(0).set(raw);
        var source = context.createBufferSource();
        source.buffer = audioBuffer;
        source.connect(context.destination);
        source.start(0);

    }

对于任何想弄明白这一点的人来说。最后,我将其编码为int16array,然后通过套接字发送,客户端将其编码回float32数组,并将其传递给play_sound函数。基本上,我只是从stackoverflow上偷了一堆东西,然后伪造,直到我成功,因为我没那么聪明:)

捕获麦克风并转换为int16array,然后通过套接字发送

 navigator.mediaDevices.getUserMedia({audio: {sampleSize: 16, channelCount: 2},video: true}) // request cam
        .then(stream => {
         vid.srcObject = stream; // don't use createObjectURL(MediaStream)
         context = new AudioContext();
         var source = context.createMediaStreamSource(stream);
         var processor = context.createScriptProcessor(1024, 2, 2);
         source.connect(processor);
         processor.connect(context.destination);

         processor.onaudioprocess = function(e) {
          // Do something with the data, i.e Convert this to WAV
          audiodata = new Int16Array(convertFloat32ToInt16(e.inputBuffer.getChannelData(0)));
          console.log(audiodata);
          socket.send(JSON.stringify({sound: audiodata, to: to, from: '$username', text:''}));

         };
         return vid.play(); // returns a Promise
       });
将捕获的麦克风转换为int16array的相关功能:

function convertFloat32ToInt16(buffer){
          l = buffer.length;
          buf = new Int16Array(l);

      while (l--)
      {
         buf[l] = Math.min(1, buffer[l])*0x7FFF;
      }

      return buf.buffer;

  }
将客户端json对象接收到int16array,然后将int16array返回到float32array:

if(typeof (message.sound) != "undefined"){
         //$('#video_stream_btn').trigger('click');
          //var json_sound = message.sound;


         if(typeof(context) == 'undefined'){
           context = new AudioContext();
         }
         sound_array = [];
         for (i in message.sound)
         {
            sound_array[i] = (message.sound [i]);
         }

        //sound_array16 = new Int16Array(sound_array);
        sound_array32 = int16ToFloat32(sound_array);
         play_sound(sound_array32, context);
         return;
       }
相关接收端INT16阵列到浮点32阵列功能:


    function int16ToFloat32(inputArray) {

        let int16arr = new Int16Array(inputArray)
        var output = new Float32Array(int16arr.length);
        for (var i = 0; i < int16arr.length; i++) {
            var int = int16arr[i];
            var float = (int >= 0x8000) ? -(0x10000 - int) / 0x8000 : int / 0x7FFF;
            output[i] = float;
        }
        return output;
    }

函数int16ToFloat32(输入阵列){
设int16arr=新的Int16Array(inputArray)
var输出=新的Float32Array(int16arr.length);
对于(变量i=0;i=0x8000)?-(0x10000-int)/0x8000:int/0x7FFF;
输出[i]=浮点数;
}
返回输出;
}

Uncaught SyntaxError:非法返回语句