BaseAudioContext: createScriptProcessor() 方法

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

BaseAudioContext 介面的 createScriptProcessor() 方法建立一個 ScriptProcessorNode,用於直接音訊處理。

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

語法

js
createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)

引數

bufferSize

以取樣幀為單位的緩衝區大小。如果指定,bufferSize 必須是以下值之一:256、512、1024、2048、4096、8192、16384。如果未傳入或值為 0,則實現將為給定環境選擇最佳緩衝區大小,該大小在節點的生命週期內將是 2 的恆定冪。

此值控制 audioprocess 事件的觸發頻率以及每次呼叫需要處理的取樣幀數。較低的 bufferSize 值將導致較低(更好)的延遲。為了避免音訊中斷和失真,需要更高的值。建議作者不要指定此緩衝區大小,而是允許實現選擇一個好的緩衝區大小來平衡延遲和音訊質量。

numberOfInputChannels

指定此節點輸入通道數的整數,預設為 2。最多支援 32 個值。

numberOfOutputChannels

指定此節點輸出通道數的整數,預設為 2。最多支援 32 個值。

警告: WebKit 目前(31 版本)要求在呼叫此方法時傳入一個有效的 bufferSize

注意: numberOfInputChannelsnumberOfOutputChannels 都為零是無效的。

返回值

一個 ScriptProcessorNode

示例

使用指令碼處理器新增白噪聲

以下示例展示瞭如何使用 ScriptProcessorNode 來處理透過 AudioContext.decodeAudioData() 載入的音軌,為其每個音訊樣本新增少量白噪聲,然後透過 AudioDestinationNode 播放。

對於每個通道和每個取樣幀,指令碼節點的 audioprocess 事件處理程式使用關聯的 audioProcessingEvent 來遍歷輸入緩衝區的每個通道,以及每個通道中的每個樣本,新增少量白噪聲,然後在每種情況下將結果設定為輸出樣本。

注意: 您可以 線上執行完整的示例,或 檢視原始碼

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-baseaudiocontext-createscriptprocessor

瀏覽器相容性

另見