AudioBuffer
interface represents a short audio asset residing in memory, created from an audio file using the
AudioContext.decodeAudioData()
method, or from raw data using
AudioContext.createBuffer()
. Once put into an AudioBuffer, the audio can then be played by being passed into an
AudioBufferSourceNode
.
Objects of these types are designed to hold small audio snippets, typically less than 45 s. For longer sounds, objects implementing the
MediaElementAudioSourceNode
are more suitable. The buffer contains data in the following format: non-interleaved IEEE754 32-bit linear PCM with a nominal range between
-1
and
+1
, that is, 32bits floating point buffer, with each samples between -1.0 and 1.0. If the
AudioBuffer
has multiple channels, they are stored in separate buffer.
AudioBuffer()
AudioBuffer
对象实例。
AudioBuffer.sampleRate
只读
Returns a float representing the sample rate, in samples per second, of the PCM data stored in the buffer.
AudioBuffer.length
只读
Returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.
AudioBuffer.duration
只读
Returns a double representing the duration, in seconds, of the PCM data stored in the buffer.
AudioBuffer.numberOfChannels
只读
Returns an integer representing the number of discrete audio channels described by the PCM data stored in the buffer.
AudioBuffer.getChannelData()
Float32Array
containing the PCM data associated with the channel, defined by the
channel
parameter (with
0
representing the first channel).
AudioBuffer.copyFromChannel()
AudioBuffer
到
destination
数组。
AudioBuffer.copyToChannel()
AudioBuffer
, from the
source
数组。
The following simple example shows how to create an
AudioBuffer
and fill it with random white noise. You can find the full source code at our
webaudio-examples
repository; a
running live
version is also available.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// Create an empty three-second stereo buffer at the sample rate of the AudioContext
var myArrayBuffer = audioCtx.createBuffer(2, audioCtx.sampleRate * 3, audioCtx.sampleRate);
// Fill the buffer with white noise;
// just random values between -1.0 and 1.0
for (var channel = 0; channel < myArrayBuffer.numberOfChannels; channel++) {
// This gives us the actual array that contains the data
var nowBuffering = myArrayBuffer.getChannelData(channel);
for (var i = 0; i < myArrayBuffer.length; 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 音频 API
The definition of 'AudioBuffer' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
AudioBuffer
|
Chrome 14 | Edge ≤18 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
AudioBuffer()
构造函数
|
Chrome
55
注意事项
|
Edge ≤79 | Firefox 53 | IE 不支持 No |
Opera
42
注意事项
|
Safari ? |
WebView Android
55
注意事项
|
Chrome Android
55
注意事项
|
Firefox Android 53 |
Opera Android
42
注意事项
|
Safari iOS ? |
Samsung Internet Android
6.0
注意事项
|
copyFromChannel
|
Chrome 14 | Edge 13 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS ? | Samsung Internet Android 1.0 |
copyToChannel
|
Chrome 14 | Edge 13 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS ? | Samsung Internet Android 1.0 |
duration
|
Chrome 14 | Edge 12 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
getChannelData
|
Chrome 14 | Edge 12 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
length
|
Chrome 14 | Edge 12 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
numberOfChannels
|
Chrome 14 | Edge 12 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
sampleRate
|
Chrome 14 | Edge 12 | Firefox 25 | IE 不支持 No | Opera 15 | Safari 6 | WebView Android Yes | Chrome Android 18 | Firefox Android 26 | Opera Android 14 | Safari iOS Yes | Samsung Internet Android 1.0 |
完整支持
不支持
兼容性未知
见实现注意事项。
AudioBuffer
AnalyserNode
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode