diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/audiobuffer/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/audiobuffer/index.html')
-rw-r--r-- | files/zh-cn/web/api/audiobuffer/index.html | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/audiobuffer/index.html b/files/zh-cn/web/api/audiobuffer/index.html new file mode 100644 index 0000000000..2922de46d6 --- /dev/null +++ b/files/zh-cn/web/api/audiobuffer/index.html @@ -0,0 +1,111 @@ +--- +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)包含以下数据:不间断的IEEE75432位线性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> + +<div class="hidden"> +<p>此页面上的兼容性表是由结构化数据生成的。如果您想贡献数据,请查看https://github.com/mdn/browser-compat-data并向我们提交pull request。</p> +</div> + +<p>{{Compat("api.AudioBuffer")}}</p> + +<div></div> + +<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> |