BaseAudioContext: createChannelSplitter() 方法

Baseline 已廣泛支援

此特性已得到良好支援,可在多種裝置和瀏覽器版本上使用。自 2021 年 4 月起,所有瀏覽器均已支援此特性。

BaseAudioContext 介面的 createChannelSplitter() 方法用於建立一個 ChannelSplitterNode,該節點用於訪問音訊流的各個通道並單獨處理它們。

注意: 建立 ChannelSplitterNode 的推薦方法是使用 ChannelSplitterNode() 建構函式;請參閱 建立 AudioNode

語法

js
createChannelSplitter(numberOfOutputs)

引數

numberOfOutputs

您希望單獨輸出的輸入音訊流中的通道數;如果未指定此引數,則預設為 6。

返回值

一個 ChannelSplitterNode

示例

以下簡單示例展示瞭如何分離立體聲音軌(例如,一段音樂),並分別處理左聲道和右聲道。要使用它們,您需要使用 AudioNode.connect(AudioNode) 方法的第二個和第三個引數,這些引數允許您指定要連線的源通道索引和目標通道索引。

js
const ac = new AudioContext();
ac.decodeAudioData(someStereoBuffer, (data) => {
  const source = ac.createBufferSource();
  source.buffer = data;
  const splitter = ac.createChannelSplitter(2);
  source.connect(splitter);
  const merger = ac.createChannelMerger(2);

  // Reduce the volume of the left channel only
  const gainNode = ac.createGain();
  gainNode.gain.setValueAtTime(0.5, ac.currentTime);
  splitter.connect(gainNode, 0);

  // Connect the splitter back to the second input of the merger: we
  // effectively swap the channels, here, reversing the stereo image.
  gainNode.connect(merger, 0, 1);
  splitter.connect(merger, 1, 0);

  const dest = ac.createMediaStreamDestination();

  // Because we have used a ChannelMergerNode, we now have a stereo
  // MediaStream we can use to pipe the Web Audio graph to WebRTC,
  // MediaRecorder, etc.
  merger.connect(dest);
});

規範

規範
Web Audio API
# dom-baseaudiocontext-createchannelsplitter

瀏覽器相容性

另見