AudioBuffer:length 属性
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
AudioBuffer 接口的 length 属性返回一个表示缓冲区中存储的 PCM 数据的长度的整数(以采样帧数为单位)。
值
一个整数。
示例
js
// 立体声
const channels = 2;
// 以 AudioContext 的采样率创建一个 2 秒的空白立体声缓冲区
const frameCount = audioCtx.sampleRate * 2.0;
const myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate);
button.onclick = () => {
// 用白噪声填充缓冲区;
// 即介于 -1.0 和 1.0 之间的随机值
for (let channel = 0; channel < channels; channel++) {
// 获取实际包含音频数据的
const nowBuffering = myArrayBuffer.getChannelData(channel);
for (let i = 0; i < frameCount; i++) {
// Math.random() 的取值范围是 [0; 1.0]
// 音频数据的取值范围应为 [-1.0; 1.0]
nowBuffering[i] = Math.random() * 2 - 1;
}
}
console.log(myArrayBuffer.length);
};
规范
| Specification |
|---|
| Web Audio API> # dom-audiobuffer-length> |
浏览器兼容性
Loading…