ScriptProcessorNode: audioprocess 事件

已棄用:此特性不再推薦。雖然某些瀏覽器可能仍然支援它,但它可能已經從相關的網路標準中刪除,可能正在刪除過程中,或者可能僅為相容性目的而保留。請避免使用它,如果可能,請更新現有程式碼;請參閱本頁底部的相容性表格以指導您的決策。請注意,此特性可能隨時停止工作。

ScriptProcessorNode 介面的 audioprocess 事件在指令碼處理器的輸入緩衝區準備好進行處理時觸發。

注意:此功能已被 AudioWorkletsAudioWorkletNode 介面取代。

此事件不可取消,也不會冒泡。

語法

在諸如 addEventListener() 之類的方法中使用事件名稱,或設定事件處理程式屬性。

js
addEventListener("audioprocess", (event) => { })

onaudioprocess = (event) => { }

事件型別

一個 AudioProcessingEvent。繼承自 Event

Event AudioProcessingEvent

事件屬性

還實現了其父介面 Event 的繼承屬性。

playbackTime 只讀

一個雙精度浮點數,表示音訊將要播放的時間,定義為 AudioContext.currentTime 的時間。

inputBuffer 只讀

一個 AudioBuffer,它是一個包含待處理輸入音訊資料的緩衝區。通道數量由工廠方法 AudioContext.createScriptProcessor() 的引數 numberOfInputChannels 定義。請注意,返回的 AudioBuffer 僅在事件處理器的作用域內有效。

outputBuffer 只讀

一個 AudioBuffer,它是一個應該寫入輸出音訊資料的緩衝區。通道數量由工廠方法 AudioContext.createScriptProcessor() 的引數 numberOfOutputChannels 定義。請注意,返回的 AudioBuffer 僅在事件處理器的作用域內有效。

示例

js
scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
  // The input buffer is a song we loaded earlier
  const inputBuffer = audioProcessingEvent.inputBuffer;

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

  // Loop through the output channels (in this case there is only one)
  for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
    const inputData = inputBuffer.getChannelData(channel);
    const 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.2;
    }
  }
});

您也可以透過 onaudioprocess 屬性設定事件處理程式。

js
scriptNode.onaudioprocess = (audioProcessingEvent) => {
  // …
};

規範

此特性似乎未在任何規範中定義。

瀏覽器相容性

另見