AudioBuffer

翻译不完整。 请帮助我们翻译这篇文章!

AudioBuffer接口表示存在内存里的一段短小的音频资源,利用AudioContext.decodeAudioData()方法从一个音频文件构建,或者利用 AudioContext.createBuffer()从原始数据构建。把音频放入AudioBuffer后,可以传入到一个 AudioBufferSourceNode进行播放。

这些类型对象被设计来控制小音频片段,往往短于45秒。对于更长的声音,通过 MediaElementAudioSourceNode来实现更为合适。缓存区(buffer)包含以下数据:不间断的IEEE75432位线性PCM,从-1到1的范围额定,就是说,32位的浮点缓存区的每个样本在-1.0到1.0之间。如果AudioBuffer有不同的频道,他们通常被保存在独立的缓存区。

属性

AudioBuffer.sampleRate 只读
存储在缓存区的PCM数据的采样率:浮点数,单位为 sample/s。
AudioBuffer.length 只读
返回存储在缓存区的PCM数据的采样帧率:整形。
AudioBuffer.duration 只读
返回存储在缓存区的PCM数据的时长:双精度型(单位为秒),。
AudioBuffer.numberOfChannels 只读
返回存储在缓存区的PCM数据的通道数:整形。

方法

AudioBuffer.getChannelData()
返回一个 Float32Array,包含了带有频道的PCM数据,由频道参数定义(有0代表第一个频道)
AudioBuffer.copyFromChannel()
从AudioBuffer的指定频道复制到数组终端。
AudioBuffer.copyToChannel()
复制样品到原数组的AudioBuffer的指定频道

例子

以下的例子展示了如何构建一个AudioBuffer以及随机用白噪音填充。你可以在 audio-buffer demo库发现完整的源代码;一个running live 的版本也可获得。

// Stereo
var channels = 2;

// Create an empty two second stereo buffer at the
// sample rate of the AudioContext
var frameCount = audioCtx.sampleRate * 2.0;
var myArrayBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);

button.onclick = function() {
  // Fill the buffer with white noise;
  // just random values between -1.0 and 1.0
  for (var channel = 0; channel < channels; channel++) {
    // This gives us the actual array that contains the data
    var nowBuffering = myArrayBuffer.getChannelData(channel);
    for (var i = 0; i < frameCount; i++) {
      // Math.random() is in [0; 1.0]
      // audio needs to be in [-1.0; 1.0]
      nowBuffering[i] = Math.random() * 2 - 1;
    }
  }

  // Get an AudioBufferSourceNode.
  // This is the AudioNode to use when we want to play an AudioBuffer
  var source = audioCtx.createBufferSource();

  // set the buffer in the AudioBufferSourceNode
  source.buffer = myArrayBuffer;

  // connect the AudioBufferSourceNode to the
  // destination so we can hear the sound
  source.connect(audioCtx.destination);

  // start the source playing
  source.start();

}

规格参数

规格参数 状态 注释
Web Audio API
AudioBuffer
Working Draft Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
AudioBufferChrome Full support 14Edge Full support ≤18Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
AudioBuffer() constructorChrome Full support 55
Notes
Full support 55
Notes
Notes The context parameter was supported up until version 57, but has now been removed.
Edge Full support ≤79Firefox Full support 53IE No support NoOpera Full support 42
Notes
Full support 42
Notes
Notes The context parameter was supported up until version 44, but has now been removed.
Safari ? WebView Android Full support 55
Notes
Full support 55
Notes
Notes The context parameter was supported up until version 57, but has now been removed.
Chrome Android Full support 55
Notes
Full support 55
Notes
Notes The context parameter was supported up until version 57, but has now been removed.
Firefox Android Full support 53Opera Android Full support 42
Notes
Full support 42
Notes
Notes The context parameter was supported up until version 44, but has now been removed.
Safari iOS ? Samsung Internet Android Full support 6.0
Notes
Full support 6.0
Notes
Notes The context parameter was supported up until Samsung Internet 7.0, but has now been removed.
copyFromChannelChrome Full support 14Edge Full support 13Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS ? Samsung Internet Android Full support 1.0
copyToChannelChrome Full support 14Edge Full support 13Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS ? Samsung Internet Android Full support 1.0
durationChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
getChannelDataChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
lengthChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
numberOfChannelsChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
sampleRateChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.

可查看