为什么这个简单的ScriptProcessorNode有这么多延迟?

时间:2016-11-30 08:33:08

标签: javascript javascript-audio-api

我遇到ScriptProcessorNode和滞后问题。即使做了一个非常简单的过程,只需要取样并做x %= 0.35。为什么这个简单的ScriptProcessorNode(包含在下面)有如此多的延迟?

MDN没有提及延迟:https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode

另外,我知道ScriptProcessorNode很快就会被弃用,但是audioWorklet还没有实现,所以这是最好的。



var baseFreq = 220;

var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var osc = audioCtx.createOscillator();
osc.type = 'sine';
osc.frequency.value = baseFreq;
osc.start();

var intervals = [1, 1.125, 1.25, 1.5, 1.6666666, 1.875, 2, 4]

var index = 0;
function newTone() {
  index++;
  index %= intervals.length;
  osc.frequency.value = intervals[index] * baseFreq;
}

var modulus = audioCtx.createScriptProcessor();
modulus.onaudioprocess = function(audioProcessingEvent) {
  var inputBuffer = audioProcessingEvent.inputBuffer;
  var outputBuffer = audioProcessingEvent.outputBuffer;

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

    for (var sample = 0; sample < inputBuffer.length; sample++) {
      outputData[sample] = inputData[sample] % moduAmount;
    }
  }
}

var moduLow = 0.35;
var moduHigh = 2;
var moduAmount = moduLow;

function turnModulusOnOff() {
  if (moduAmount == moduLow)
    moduAmount = moduHigh;
  else
    moduAmount = moduLow;

}
 
var gain = audioCtx.createGain();
gain.gain.value = 0.05;

gain.connect(audioCtx.destination);
modulus.connect(gain)
osc.connect(modulus);
osc.connect(gain);

document.body.addEventListener("keydown", newTone);
&#13;
html,
body {
  height: 100%;
  width: 100%;
  font-family: arial;
}
&#13;
<button onclick="newTone()">Next Tone</button>
<button onclick="turnModulusOnOff()">Mute/Unmute Modulus</button>

<br>
To test, click anywhere and then press any key to change the tone.  Notice when Modulus is muted how responsive the tone change is.  Notice the lag between the sine frequency change and the modulus effect.
<br>
<b>For Extra Fun: Go crazy on your keyboard</b>
&#13;
&#13;
&#13;

0 个答案:

没有答案