AudioBufferSourceNode
interface is an
AudioScheduledSourceNode
which represents an audio source consisting of in-memory audio data, stored in an
AudioBuffer
. It's especially useful for playing back audio which has particularly stringent timing accuracy requirements, such as for sounds that must match a specific rhythm and can be kept in memory rather than being played from disk or the network.
To play sounds which require accurate timing but must be streamed from the network or played from disk, use a
AudioWorkletNode
to implement its playback.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 23.333333333333332%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 140" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/EventTarget.html" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/AudioNode" target="_top"><rect x="151" y="1" width="90" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="196" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioNode</text></a><polyline points="241,25 251,20 251,30 241,25" stroke="#D4DDE4" fill="none"/><line x1="251" y1="25" x2="281" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/AudioScheduledSourceNode" target="_top"><rect x="281" y="1" width="240" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="401" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioScheduledSourceNode</text></a><polyline points="521,25 531,20 531,30 521,25" stroke="#D4DDE4" fill="none"/><line x1="531" y1="25" x2="561" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/AudioBufferSourceNode" target="_top"><rect x="561" y="1" width="210" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="666" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">AudioBufferSourceNode</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
An
AudioBufferSourceNode
has no inputs and exactly one output, which has the same number of channels as the
AudioBuffer
indicated by its
buffer
property. If there's no buffer set—that is, if
buffer
is
null
—the output contains a single channel of silence (every sample is 0).
An
AudioBufferSourceNode
can only be played once; after each call to
start()
, you have to create a new node if you want to play the same sound again. Fortunately, these nodes are very inexpensive to create, and the actual
AudioBuffer
s can be reused for multiple plays of the sound. Indeed, you can use these nodes in a "fire and forget" manner: create the node, call
start()
to begin playing the sound, and don't even bother to hold a reference to it. It will automatically be garbage-collected at an appropriate time, which won't be until sometime after the sound has finished playing.
Multiple calls to
stop()
are allowed. The most recent call replaces the previous one, if the
AudioBufferSourceNode
has not already reached the end of the buffer.
| 输入数 |
0
|
|---|---|
| 输出数 |
1
|
| 通道计数 |
defined by the associated
AudioBuffer
|
AudioBufferSourceNode()
AudioBufferSourceNode
object. An
AudioBufferSourceNode
can be instantiated using the
AudioContext.createBufferSource()
方法。
继承的特性来自其父级,
AudioNode
.
AudioBufferSourceNode.buffer
AudioBuffer
that defines the audio asset to be played, or when set to the value
null
, defines a single channel of silence (in which every sample is 0.0).
AudioBufferSourceNode.detune
AudioParam
representing detuning of playback in
cents
. This value is compounded with
playbackRate
to determine the speed at which the sound is played. Its default value is
0
(meaning no detuning), and its nominal range is -∞ to ∞.
AudioBufferSourceNode.loop
AudioBuffer
is reached. Its default value is
false
.
AudioBufferSourceNode.loopStart
可选
AudioBuffer
must begin when
loop
is
true
. Its default value is
0
(meaning that at the beginning of each loop, playback begins at the start of the audio buffer).
AudioBufferSourceNode.loopEnd
可选
AudioBuffer
stops and loops back to the time indicated by
loopStart
,若
loop
is
true
。默认值为
0
.
AudioBufferSourceNode.playbackRate
AudioParam
that defines the speed factor at which the audio asset will be played, where a value of 1.0 is the sound's natural sampling rate. Since no pitch correction is applied on the output, this can be used to change the pitch of the sample. This value is compounded with
detune
to determine the final playback rate.
Inherits event handlers from its parent,
AudioScheduledSourceNode
.
继承方法来自其父级
AudioScheduledSourceNode
.
AudioBufferSourceNode.start()
Used to schedule playback of the audio data contained in the buffer, or to begin playback immediately.
In this example, we create a two-second buffer, fill it with white noise, and then play it using an
AudioBufferSourceNode
. The comments 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();
对于
decodeAudioData()
example, see the
AudioContext.decodeAudioData()
页面。
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'AudioBufferSourceNode' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
AudioBufferSourceNode
|
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 |
AudioBufferSourceNode()
构造函数
|
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
注意事项
|
buffer
|
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 |
detune
|
Chrome 44 | Edge 13 | Firefox 40 | IE 不支持 No | Opera 31 | Safari 不支持 No | WebView Android 44 | Chrome Android 44 | Firefox Android 40 | Opera Android 32 | Safari iOS ? | Samsung Internet Android 4.0 |
loop
|
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 |
loopEnd
|
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 |
loopStart
|
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 |
onended
|
Chrome 14 | Edge 12 | Firefox ? | IE 不支持 No | Opera 不支持 ? — 44 | Safari ? | WebView Android Yes | Chrome Android 18 | Firefox Android ? | Opera Android 不支持 ? — 43 | Safari iOS ? | Samsung Internet Android 1.0 |
playbackRate
|
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 |
start
|
Chrome 不支持 14 — 57 | Edge 不支持 12 — 79 | 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 |
完整支持
不支持
兼容性未知
见实现注意事项。
AudioBufferSourceNode
AnalyserNode
AudioBuffer
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