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

浏览器兼容性

另见