AudioBuffer:copyToChannel() 方法

Baseline 已廣泛支援

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

AudioBuffer 介面的 copyToChannel() 方法將源陣列的樣本複製到 AudioBuffer 的指定通道。

語法

js
copyToChannel(source, channelNumber)
copyToChannel(source, channelNumber, startInChannel)

引數

source

用於從中複製通道資料的 Float32Array

channelNumber

當前 AudioBuffer 中用於複製通道資料的通道號。如果 channelNumber 大於或等於 AudioBuffer.numberOfChannels,則會丟擲 INDEX_SIZE_ERR 異常。

startInChannel 可選

用於複製資料的可選偏移量。如果 startInChannel 大於 AudioBuffer.length,則會丟擲 INDEX_SIZE_ERR 異常。

返回值

無(undefined)。

示例

js
const myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
const anotherArray = new Float32Array();
// Copy channel data from second channel of myArrayBuffer.
myArrayBuffer.copyFromChannel(anotherArray, 1, 0);
// Copy data from anotherArray to first channel of myArrayBuffer. Both channels have the same data now.
myArrayBuffer.copyToChannel(anotherArray, 0, 0);

規範

規範
Web Audio API
# dom-audiobuffer-copytochannel

瀏覽器相容性

另見