AudioProcessingEvent

已弃用:此特性不再推荐。虽然某些浏览器可能仍然支持它,但它可能已经从相关的网络标准中删除,可能正在删除过程中,或者可能仅为兼容性目的而保留。请避免使用它,如果可能,请更新现有代码;请参阅本页底部的兼容性表格以指导您的决策。请注意,此特性可能随时停止工作。

Web Audio APIAudioProcessingEvent 接口表示当 ScriptProcessorNode 的输入缓冲区准备好进行处理时发生的事件。

当需要进行音频处理时,会在 ScriptProcessorNode 上触发一个具有此接口的 audioprocess 事件。在音频处理过程中,会读取并处理输入缓冲区以生成输出音频数据,然后将这些数据写入输出缓冲区。

警告:此功能已被弃用,应替换为 AudioWorklet

Event AudioProcessingEvent

构造函数

AudioProcessingEvent() 已弃用

创建一个新的 AudioProcessingEvent 对象。

实例属性

还实现了其父级 Event 的继承属性。.

playbackTime 只读 已弃用

一个双精度浮点数,表示音频将要播放的时间,根据 AudioContext.currentTime 的时间定义。

inputBuffer 只读 已弃用

一个 AudioBuffer,它是包含要处理的输入音频数据的缓冲区。通道数由工厂方法 AudioContext.createScriptProcessor() 的参数 numberOfInputChannels 定义。请注意,返回的 AudioBuffer 仅在事件处理程序的范围内有效。

outputBuffer 只读 已弃用

一个 AudioBuffer,它是应该写入输出音频数据的缓冲区。通道数由工厂方法 AudioContext.createScriptProcessor() 的参数 numberOfOutputChannels 定义。请注意,返回的 AudioBuffer 仅在事件处理程序的范围内有效。

示例

使用脚本处理器添加白噪声

下面的示例展示了如何使用 ScriptProcessorNode 来处理通过 AudioContext.decodeAudioData() 加载的音轨,向输入音轨(缓冲区)的每个音频样本添加一些白噪声,然后通过 AudioDestinationNode 播放。对于每个通道和每个样本帧,scriptNode.onaudioprocess 函数会接收相关的 audioProcessingEvent,并使用它来遍历输入缓冲区的每个通道以及每个通道中的每个样本,添加少量白噪声,然后将结果设置为每个情况下的输出样本。

注意:有关完整的可运行示例,请参阅我们的 script-processor-node GitHub 仓库。(您也可以访问 源代码。)

js
const myScript = document.querySelector("script");
const myPre = document.querySelector("pre");
const playButton = document.querySelector("button");

// Create AudioContext and buffer source
let audioCtx;

async function init() {
  audioCtx = new AudioContext();
  const source = audioCtx.createBufferSource();

  // Create a ScriptProcessorNode with a bufferSize of 4096 and
  // a single input and output channel
  const scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);

  // Load in an audio track using fetch() and decodeAudioData()
  try {
    const response = await fetch("viper.ogg");
    const arrayBuffer = await response.arrayBuffer();
    source.buffer = await audioCtx.decodeAudioData(arrayBuffer);
  } catch (err) {
    console.error(
      `Unable to fetch the audio file: ${name} Error: ${err.message}`,
    );
  }

  // Give the node a function to process audio events
  scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
    // The input buffer is the song we loaded earlier
    let inputBuffer = audioProcessingEvent.inputBuffer;

    // The output buffer contains the samples that will be modified
    // and played
    let outputBuffer = audioProcessingEvent.outputBuffer;

    // Loop through the output channels (in this case there is only one)
    for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
      let inputData = inputBuffer.getChannelData(channel);
      let outputData = outputBuffer.getChannelData(channel);

      // Loop through the 4096 samples
      for (let 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.1;
      }
    }
  });

  source.connect(scriptNode);
  scriptNode.connect(audioCtx.destination);
  source.start();

  // When the buffer source stops playing, disconnect everything
  source.addEventListener("ended", () => {
    source.disconnect(scriptNode);
    scriptNode.disconnect(audioCtx.destination);
  });
}

// wire up play button
playButton.addEventListener("click", () => {
  if (!audioCtx) {
    init();
  }
});

规范

规范
Web Audio API
# dom-audioprocessingevent-playbacktime
Web Audio API
# dom-audioprocessingevent-outputbuffer
Web Audio API
# dom-audioprocessingevent-inputbuffer
Web Audio API
# dom-audioprocessingevent-audioprocessingevent

浏览器兼容性

另见