Audio ScriptProcessorNode outputBuffer在Chrome中仅包含0

时间:2017-06-15 12:07:37

标签: javascript google-chrome debugging audio html5-audio

我试图创建一个音频ScriptProcessor,用我的音频做一些事情,但我无法在Chrome中调试它。在Firefox上它工作得很好,但在Chrome中我的声音超负荷(很多,我的声卡基本上一直说全功率,虽然我的音量下降了40%)。我的主要问题是我无法在chrome中调试它,因为我的outputBuffer只包含0,即使在我复制了inputBuffer之后也是如此。但由于某种原因,仍然有声音。我的代码的重要部分:

function processAudio(evt) {
 var inputBuffer = evt.inputBuffer;
 var outputBuffer = evt.outputBuffer;


 for (var channel = 0; channel < 2; channel++) {
    var inputData = inputBuffer.getChannelData(channel);
    var outputData = outputBuffer.getChannelData(channel);

    for (var i = 0; i < inputBuffer.length; i++) {
      outputData[i] = inputData[i];
      // At this point outputData[i] = 0, but inputData[i] is not 0
      // The audio plays normally, so there obviously is something in the outputBuffer
    }
  }
}

当我在外部循环的末尾设置断点时,我可以看到inputData充满了数字,但outputData只包含0。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

好的,我想我知道发生了什么事(也许有人可以证实这一点......)。正如@Kaiido指出的那样,Audio API似乎在一个并行线程上运行,这意味着,outputBuffer只是在一段时间后从音频线程中被拉出,只留下0。如果我这样做(不漂亮,但符合我的目的):

function processAudio(audioProcessingEvent) {
  var inputBuffer = audioProcessingEvent.inputBuffer;
  var outputBuffer = audioProcessingEvent.outputBuffer;

  var inputData = inputBuffer.getChannelData(channel);
  var outputData = outputBuffer.getChannelData(channel);

  for (var sample = 0; sample < outputBuffer.length; sample++) {
    outputData[sample] = ((Math.random() * 2) - 1) * 0.2;         
  }

  console.log("first = " + outputData[0]);

  // Delay 200ms until the outputBuffer gets pulled
  var date = new Date();
  var curDate = null;
  do { curDate = new Date(); }
  while(curDate-date < 200);

  console.log("first_delay = " + outputData[0]);
}

我得到了这个结果:

first = 0.060469988733530045
first_delay = 0
first = -0.026373833417892456
first_delay = 0
first = -0.037974122911691666
first_delay = 0

猜猜我将不得不尝试用firefox或其他东西进行调试。