AudioBufferSourceNode - Web API 接口参考 编辑
AudioBufferSourceNode
接口继承自AudioScheduledSourceNode
,表现为一个音频源,它包含了一些写在内存中的音频数据,通常储存在一个ArrayBuffer对象中。在处理有严格的时间精确度要求的回放的情形下它尤其有用。比如播放那些需要满足一个指定节奏的声音或者那些储存在内存而不是硬盘或者来自网络的声音。为了播放那些有时间精确度需求但来自网络的流文件或者来自硬盘,则使用AudioWorkletNode
来实现回放。
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="/wiki/zh-CN/docs/Web/API/EventTarget" 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="https://developer.mozilla.org/wiki/zh-CN/docs/Web/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="https://developer.mozilla.org/wiki/zh-CN/docs/Web/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="https://developer.mozilla.org/wiki/zh-CN/docs/Web/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;}
AudioBufferSourceNode
没有输入却有一个输出,其通道数与其 buffer
属性所指定的 AudioBuffer
相同。如果没有设置 buffer,也就是说 buffer
属性是 null
的话,输出将包含一个无声的单通道(每个采样点均为0)。
一个 AudioBufferSourceNode
只能被播放一次,也就是说,每次调用 start()
之后,如果还想再播放一遍同样的声音,那么就需要再创建一个 AudioBufferSourceNode
。庆幸的是,创建该节点的代价并不大,并且想要多次播放声音的话,实际上 AudioBuffer
也可以被重用。事实上,你可以用一种“阅后即焚”的方式来使用这些节点:创建节点,调用 start()
开始播放声音,然后,你甚至不需要再保留这个节点的引用了。声音播放完成之后,垃圾收集器会找个恰当的时机回收资源。
多次调用 AudioBufferSourceNode.stop()
是允许的。如果这时候 AudioBufferSourceNode
还没有到达缓冲区末尾的话,最近一次的调用将替换上一次的调用。
输入数量 | 0 |
---|---|
输出数量 | 1 |
频道数量 | 由相关的 AudioBuffer 定义 |
属性
从父级的 AudioNode
继承属性.
AudioBufferSourceNode.buffer
- 是一个
AudioBuffer
它定义了要播放的音频,当设置它的值为0时,它会定义一个静默的单通道。 AudioBufferSourceNode.detune
- Is a k-rate
AudioParam
representing detuning of oscillation in cents. Its default value is0
. AudioBufferSourceNode.loop
- Is a Boolean attribute indicating if the audio asset must be replayed when the end of the
AudioBuffer
is reached. Its default value isfalse
. AudioBufferSourceNode.loopStart
- Is a double value indicating, in seconds, where in the
AudioBuffer
the restart of the play must happen. Its default value is0
. AudioBufferSourceNode.loopEnd
- Is a double value indicating, in seconds, where in the
AudioBuffer
the replay of the play must stop (and eventually loop again). Its default value is0
. AudioBufferSourceNode.playbackRate
- Is an a-rate
AudioParam
that defines the speed factor at which the audio asset will be played. Since no pitch correction is applied on the output, this can be used to change the pitch of the sample.
事件
AudioBufferSourceNode.onended
- 是一个
EventHandler
类型,包含了与ended
相关联的结束事件。
方法
从父级的 AudioNode
继承方法.
AudioBufferSourceNode.start()
- Schedules the start of the playback of the audio asset.
AudioBufferSourceNode.stop()
- Schedules the end of the playback of an audio asset.
例子
在这个例子中, 我们将会创建一个2秒的缓冲器,并用白噪音填充它, 然后通过AudioBufferSourceNode
来播放它. 注释里说明了它的功能.
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var button = document.querySelector('button');
var pre = document.querySelector('pre');
var myScript = document.querySelector('script');
pre.innerHTML = myScript.innerHTML;
// 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(2, 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 ArrayBuffer 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();
}
注意: 音频数据解码的例子请查看 AudioContext.decodeAudioData
页面.
规范
Specification | Status | Comment |
---|---|---|
Web Audio API AudioBufferSourceNode | Working Draft |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 14 webkit[1] | 23.0 (23.0) | 未实现 | 15 webkit 22 | 6 webkit |
detune property | (Yes) | 40.0 (40.0) | 未实现 | ? | ? |
Feature | Android | Chrome | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 未实现 | 28 webkit[1] | 25.0 (25.0) | 1.2 | 未实现 | 未实现 | 6 webkit |
detune property | 未实现 | (Yes) | (Yes) | (Yes) | 未实现 | 未实现 | ? |
[1] As of Chrome 42.0 setting AudioBufferSourceNode.buffer more than once is deprecated. A deprecation message is displayed if the buffer attribute is assigned more than once.
相关页面
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论