流和播放通过JavaScript / HTML5中的TCP数据包接收的音频

时间:2020-02-25 06:55:47

标签: javascript tcp html5-audio audio-streaming audiobuffer

我有某些TCP数据包中包含音频数据。
我是Js和html5的新手。
我写了这样的东西,它发出嗡嗡声。我想知道音频样本在TCP数据包中的样子。

我已经使用Wire-shark捕获了TCP个数据包,数据段显示了一些字符及其 ASCII 值。
我相信这些字符是音频数据。假设是这样,如何将它们传递给myArrayBuffer,如下所示?



    var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
    var source = audioCtx.createBufferSource();
       function Set1()
       {
    try {
    
    
    // Create an empty three-second stereo buffer at the sample rate of the AudioContext
    var myArrayBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 30, audioCtx.sampleRate);
    
    // Fill the buffer with white noise;
    // just random values between -1.0 and 1.0
    for (var channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {
      // This gives us the actual array that contains the data
      var nowBuffering = myArrayBuffer.getChannelData(channel);
      for (var i = 0; i < myArrayBuffer.length; i++) {
        // Math.random() is in [0; 1.0]
        // audio needs to be in [-1.0; 1.0]
        nowBuffering[i] = Math.random() * 2 - 1;
      }
    }
    
    // set the buffer in the AudioBufferSourceNode
    source.buffer = myArrayBuffer;
    
    // connect the AudioBufferSourceNode to the
    // destination so we can hear the sound
    source.connect(audioCtx.destination);
    
    // start the source playing
    source.start();
      }
      catch(e) {
        alert('Web Audio API is not supported in this browser');
      }
    }
    
    function stop()
    {
    try {
    source.stop();
    
    }
      catch(e) {
        alert('Unable to stop file');
      }
    }

0 个答案:

没有答案