AudioProcessingEvent

弃用
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

Web 音频 API AudioProcessingEvent represents events that occur when a ScriptProcessorNode input buffer is ready to be processed.

注意 : As of the August 29 2014 Web Audio API spec publication, this feature has been marked as deprecated, and is soon to be replaced by AudioWorklet .

特性

The list below includes the properties inherited from its parent, Event .

Property Type 描述
target 只读 EventTarget The event target (the topmost target in the DOM tree).
type 只读 DOMString The type of event.
bubbles 只读 boolean Does the event normally bubble?
cancelable 只读 boolean Is it possible to cancel the event?
playbackTime 只读 double The time when the audio will be played, as defined by the time of AudioContext.currentTime
inputBuffer 只读 AudioBuffer The buffer containing the input audio data to be processed. The number of channels is defined as a parameter, numberOfInputChannels , of the factory method AudioContext.createScriptProcessor() . Note the the returned AudioBuffer is only valid in the scope of the onaudioprocess 函数。
outputBuffer 只读 AudioBuffer

The buffer where the output audio data should be written. The number of channels is defined as a parameter, numberOfOutputChannels , of the factory method AudioContext.createScriptProcessor() . Note the the returned AudioBuffer is only valid in the scope of the onaudioprocess 函数。

范例

The following example shows basic usage of a ScriptProcessorNode to take a track loaded via AudioContext.decodeAudioData() , process it, adding a bit of white noise to each audio sample of the input track (buffer) and play it through the AudioDestinationNode . For each channel and each sample frame, the scriptNode.onaudioprocess function takes the associated audioProcessingEvent and uses it to loop through each channel of the input buffer, and each sample in each channel, and add a small amount of white noise, before setting that result to be the output sample in each case.

注意 : For a full working example, see our script-processor-node github repo (also view the source code .)

var myScript = document.querySelector('script');
var myPre = document.querySelector('pre');
var playButton = document.querySelector('button');
// Create AudioContext and buffer source
var audioCtx = new AudioContext();
source = audioCtx.createBufferSource();
// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channel
var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
console.log(scriptNode.bufferSize);
// load in an audio track via XHR and decodeAudioData
function getData() {
  request = new XMLHttpRequest();
  request.open('GET', 'viper.ogg', true);
  request.responseType = 'arraybuffer';
  request.onload = function() {
    var audioData = request.response;
    audioCtx.decodeAudioData(audioData, function(buffer) {
    myBuffer = buffer;
    source.buffer = myBuffer;
  },
    function(e){"Error with decoding audio data" + e.err});
  }
  request.send();
}
// Give the node a function to process audio events
scriptNode.onaudioprocess = function(audioProcessingEvent) {
  // The input buffer is the song we loaded earlier
  var inputBuffer = audioProcessingEvent.inputBuffer;
  // The output buffer contains the samples that will be modified and played
  var outputBuffer = audioProcessingEvent.outputBuffer;
  // Loop through the output channels (in this case there is only one)
  for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
    var inputData = inputBuffer.getChannelData(channel);
    var outputData = outputBuffer.getChannelData(channel);
    // Loop through the 4096 samples
    for (var sample = 0; sample < inputBuffer.length; sample++) {
      // make output equal to the same as the input
      outputData[sample] = inputData[sample];
      // add noise to each output sample
      outputData[sample] += ((Math.random() * 2) - 1) * 0.2;
    }
  }
}
getData();
// wire up play button
playButton.onclick = function() {
  source.connect(scriptNode);
  scriptNode.connect(audioCtx.destination);
  source.start();
}
// When the buffer source stops playing, disconnect everything
source.onended = function() {
  source.disconnect(scriptNode);
  scriptNode.disconnect(audioCtx.destination);
}
					

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioProcessingEvent
弃用
Chrome 14 Edge ≤18 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
AudioProcessingEvent() 构造函数
弃用
Chrome 57 Edge ≤79 Firefox ? IE 不支持 No Opera ? Safari ? WebView Android 57 Chrome Android 57 Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android 7.0
inputBuffer
弃用
Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
outputBuffer
弃用
Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0
playbackTime
弃用
Chrome 14 Edge 12 Firefox 25 IE 不支持 No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知
弃用。不要用于新网站。
弃用。不要用于新网站。

另请参阅