createBuffer()
方法在
BaseAudioContext
Interface is used to create a new, empty
AudioBuffer
object, which can then be populated by data, and played via an
AudioBufferSourceNode
For more details about audio buffers, check out the
AudioBuffer
reference page.
注意
:
createBuffer()
used to be able to take compressed data and give back decoded samples, but this ability was removed from the spec, because all the decoding was done on the main thread, therefore
createBuffer()
was blocking other code execution. The asynchronous method
decodeAudioData()
does the same thing — takes compressed audio, say, an MP3 file, and directly gives you back an
AudioBuffer
that you can then set to play via in an
AudioBufferSourceNode
. For simple uses like playing an MP3,
decodeAudioData()
is what you should be using.
var buffer = baseAudioContext.createBuffer(numOfchannels, length, sampleRate);
注意 : For an in-depth explanation of how audio buffers work, and what these parameters mean, read Audio buffers: frames, samples and channels from our Basic concepts guide.
numOfChannels
An integer representing the number of channels this buffer should have. The default value is 1, and all user agents must support at least 32 channels.
length
numOfChannels
). To determine the
length
to use for a specific number of seconds of audio, use
numSeconds * sampleRate
.
sampleRate
The sample rate of the linear audio data in sample-frames per second. All browsers must support sample rates in at least the range 8,000 Hz to 96,000 Hz.
AudioBuffer
configured based on the specified options.
NotSupportedError
numberOfChannels
being higher than supported, or a
sampleRate
outside the nominal range).
RangeError
There isn't enough memory available to allocate the buffer.
First, a couple of simple trivial examples, to help explain how the parameters are used:
var audioCtx = new AudioContext(); var buffer = audioCtx.createBuffer(2, 22050, 44100);
If you use this call, you will get a stereo buffer (two channels), that, when played back on an AudioContext running at 44100Hz (very common, most normal sound cards run at this rate), will last for 0.5 seconds: 22050 frames / 44100Hz = 0.5 seconds.
var audioCtx = new AudioContext(); var buffer = audioCtx.createBuffer(1, 22050, 22050);
If you use this call, you will get a mono buffer (one channel), that, when played back on an
AudioContext
running at 44100Hz, will be automatically *resampled* to 44100Hz (and therefore yield 44100 frames), and last for 1.0 second: 44100 frames / 44100Hz = 1 second.
注意 : audio resampling is very similar to image resizing: say you've got a 16 x 16 image, but you want it to fill a 32x32 area: you resize (resample) it. the result has less quality (it can be blurry or edgy, depending on the resizing algorithm), but it works, and the resized image takes up less space. Resampled audio is exactly the same — you save space, but in practice you will be unable to properly reproduce high frequency content (treble sound).
Now let's look at a more complex
createBuffer()
example, in which we create a three second buffer, fill it with white noise, and then play it via an
AudioBufferSourceNode
. The comment should clearly explain what is going on. You can also
run the code live
,或
view the source
.
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 ArrayBuffer 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 'createBuffer()' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
createBuffer
|
Chrome
10
Prefixed
|
Edge ≤18 |
Firefox
53
注意事项
|
IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android Yes | Chrome Android 33 |
Firefox Android
53
注意事项
|
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android 2.0 |
完整支持
不支持
见实现注意事项。
要求使用供应商前缀或不同名称。
BaseAudioContext
createAnalyser()
createBiquadFilter()
createBuffer()
createBufferSource()
createChannelMerger()
createChannelSplitter()
createConstantSource()
createConvolver()
createDelay()
createDynamicsCompressor()
createGain()
createIIRFilter()
createOscillator()
createPanner()
createPeriodicWave()
createScriptProcessor()
createStereoPanner()
createWaveShaper()
decodeAudioData()
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode