简体   繁体   中英

Low Latency Audio Playback?

Is it possible to achieve low latency audio playback using HTML5? I'm currently using AudioContext API. However, I am getting latency of about 4 seconds. Which is way to much for my use case.

    if (!window.audioContextInstance) {
      window.audioContextInstance = new webkitAudioContext();
    }
    var context = window.audioContextInstance;

    context.sampleRate = 48000;

    var buffers = [];

    var src = new Float32Array();
    var srcIdx = 0;
    var bufferSize = 2048;

    var sourceNode = context.createScriptProcessor(bufferSize, 1, 2);
    sourceNode.onaudioprocess = function(evt) {
      var c0 = evt.outputBuffer.getChannelData(0);
      var c1 = evt.outputBuffer.getChannelData(1);

      var sample = 0;

      while(sample < bufferSize) {
        if (srcIdx >= src.length) {
          if (!buffers.length) {
            console.log("Warning: Audio Buffer Underflow")
            return;
          }
          src = buffers.shift();
          srcIdx = 0;
        }

        while(sample < bufferSize && srcIdx < src.length) {
          c0[sample] = src[srcIdx++];
          c1[sample] = src[srcIdx++];
          sample++;
        }
      }
    };

    scope.$on('frame', function (event, frame) {          

      while (buffers.length > 1) {
        buffers.shift();
      }

        buffers.push(new Float32Array(frame.data));

      if (buffers.length > 0) {
        sourceNode.connect(context.destination);
      }
    });
  }

您可能对riffwave.js感兴趣,它看起来比4s的延迟要低得多。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM