blob: e1e0bacf0b2972a5a8cbf8ecfb3f8560fb72d989 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
---
title: AudioBuffer
slug: Web/API/AudioBuffer
translation_of: Web/API/AudioBuffer
---
<p>{{APIRef("Web Audio API")}}</p>
<div>
<p>AudioBuffer接口表示存在内存里的一段短小的音频资源,利用{{ domxref("AudioContext.decodeAudioData()") }}方法从一个音频文件构建,或者利用 {{ domxref("AudioContext.createBuffer()") }}从原始数据构建。把音频放入AudioBuffer后,可以传入到一个 {{ domxref("AudioBufferSourceNode") }}进行播放。</p>
</div>
<p>这些类型对象被设计来控制小音频片段,往往短于45秒。对于更长的声音,通过 {{domxref("MediaElementAudioSourceNode")}}来实现更为合适。缓存区(buffer)包含以下数据:不间断的IEEE754 32位线性PCM,从-1到1的范围额定,就是说,32位的浮点缓存区的每个样本在-1.0到1.0之间。如果{{domxref("AudioBuffer")}}有不同的频道,他们通常被保存在独立的缓存区。</p>
<h2 id="属性">属性</h2>
<dl>
<dt>{{domxref("AudioBuffer.sampleRate")}} {{readonlyInline}}</dt>
<dd>存储在缓存区的PCM数据的采样率:浮点数,单位为 sample/s。</dd>
<dt>{{domxref("AudioBuffer.length")}} {{readonlyInline}}</dt>
<dd>返回存储在缓存区的PCM数据的采样帧率:整形。</dd>
<dt>{{domxref("AudioBuffer.duration")}} {{readonlyInline}}</dt>
<dd>返回存储在缓存区的PCM数据的时长:双精度型(单位为秒),。</dd>
<dt>{{domxref("AudioBuffer.numberOfChannels")}} {{readonlyInline}}</dt>
<dd>返回存储在缓存区的PCM数据的通道数:整形。</dd>
</dl>
<h2 id="方法">方法</h2>
<dl>
<dt>{{domxref("AudioBuffer.getChannelData()")}}</dt>
<dd>返回一个 {{jsxref("Float32Array")}},包含了带有频道的PCM数据,由频道参数定义(有0代表第一个频道)</dd>
<dt>{{domxref("AudioBuffer.copyFromChannel()")}}</dt>
<dd>从AudioBuffer的指定频道复制到数组终端。</dd>
<dt>{{domxref("AudioBuffer.copyToChannel()")}}</dt>
<dd>复制样品到原数组的AudioBuffer的指定频道</dd>
</dl>
<h2 id="例子">例子</h2>
<p>以下的例子展示了如何构建一个AudioBuffer以及随机用白噪音填充。你可以在 <a href="https://github.com/mdn/webaudio-examples/tree/master/audio-buffer">audio-buffer demo</a>库发现完整的源代码;一个<a href="https://mdn.github.io/webaudio-examples/audio-buffer/">running live</a> 的版本也可获得。</p>
<pre class="brush: js;highlight:[7,14,27] notranslate">// 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();
}
</pre>
<h2 id="规格参数">规格参数</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规格参数</th>
<th scope="col">状态</th>
<th scope="col">注释</th>
</tr>
<tr>
<td>{{SpecName('Web Audio API', '#the-audiobuffer-interface', 'AudioBuffer')}}</td>
<td>{{Spec2('Web Audio API')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.AudioBuffer")}}</p>
<h2 id="可查看">可查看</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API">应用Web Audio API </a></li>
</ul>
|