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

瀏覽器相容性

另見