Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 765629 of AudioBuffer

  • Revision slug: Web/API/AudioBuffer
  • Revision title: AudioBuffer
  • Revision id: 765629
  • Created:
  • Creator: Jeremie
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("Web Audio API")}}

The AudioBuffer interface represents a short audio asset residing in memory, created from an audio file using the {{ domxref("AudioContext.decodeAudioData()") }} method, or from raw data using {{ domxref("AudioContext.createBuffer()") }}. Once put into an AudioBuffer, the audio can then be played by being passed into an {{ domxref("AudioBufferSourceNode") }}.

Objects of these types are designed to hold small audio snippets, typically less than 45 s. For longer sounds, objects implementing the {{domxref("MediaElementAudioSourceNode")}} are more suitable. The buffer contains data in the following format:  non-interleaved IEEE754 32-bit linear PCM with a nominal range between -1 and +1, that is, 32bits floating point buffer, with each samples between -1.0 and 1.0. If the {{domxref("AudioBuffer")}} has multiple channels, they are stored in separate buffer.

Properties

{{domxref("AudioBuffer.sampleRate")}} {{readonlyInline}}
Returns a float representing the sample rate, in samples per second, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.length")}} {{readonlyInline}}
Returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.duration")}} {{readonlyInline}}
Returns a double representing the duration, in seconds, of the PCM data stored in the buffer.
{{domxref("AudioBuffer.numberOfChannels")}} {{readonlyInline}}
Returns an integer representing the number of discrete audio channels described by the PCM data stored in the buffer.

Methods

{{domxref("AudioBuffer.getChannelData()")}}
Returns a {{jsxref("Float32Array")}} containing the PCM data associated with the channel, defined by the channel parameter (with 0 representing the first channel).
{{domxref("AudioBuffer.copyFromChannel()")}}
Copies the samples from the specified channel of the AudioBuffer to the destination array.
{{domxref("AudioBuffer.copyToChannel()")}}
Copies the samples to the specified channel of the AudioBuffer, from the source array.

Example

The following simple example shows how to create an AudioBuffer and fill it with random white noise. You can find the full source code at our audio-buffer demo repository; a running live version is also available.

// 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();

}

Specifications

Specification Status Comment
{{SpecName('Web Audio API', '#the-audiobuffer-interface', 'AudioBuffer')}} {{Spec2('Web Audio API')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 14 {{property_prefix("webkit")}} {{CompatGeckoDesktop(25)}} {{CompatNo}} 15 {{property_prefix("webkit")}}
22 (unprefixed)
6 {{property_prefix("webkit")}}
copyFromChannel() and copyToChannel() {{CompatUnknown}} {{CompatGeckoDesktop(27)}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support {{CompatNo}} 28 {{property_prefix("webkit")}} {{CompatGeckoMobile(25)}} 1.2 {{CompatNo}} {{CompatNo}} 6 {{property_prefix("webkit")}}
copyFromChannel() and copyToChannel() {{CompatNo}} {{CompatUnknown}} {{CompatGeckoMobile(27)}}   {{CompatNo}} {{CompatNo}} {{CompatUnknown}}

See also

Revision Source

<p>{{APIRef("Web Audio API")}}</p>

<div>
<p>The <code>AudioBuffer</code> interface represents a short audio asset residing in memory, created from an audio file using the {{ domxref("AudioContext.decodeAudioData()") }} method, or from raw data using {{ domxref("AudioContext.createBuffer()") }}. Once put into an AudioBuffer, the audio can then be played by being passed into an {{ domxref("AudioBufferSourceNode") }}.</p>
</div>

<p>Objects of these types are designed to hold small audio snippets, typically less than 45&nbsp;s. For longer sounds, objects implementing the {{domxref("MediaElementAudioSourceNode")}} are more suitable. The buffer contains data in the following format:&nbsp; non-interleaved IEEE754 32-bit linear PCM with a nominal range between <code>-1</code> and <code>+1</code>, that is, 32bits floating point buffer, with each samples between -1.0 and 1.0. If the {{domxref("AudioBuffer")}} has multiple channels, they are stored in separate buffer.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("AudioBuffer.sampleRate")}} {{readonlyInline}}</dt>
 <dd>Returns a float representing the sample rate, in samples per second, of the PCM data stored in the buffer.</dd>
 <dt>{{domxref("AudioBuffer.length")}} {{readonlyInline}}</dt>
 <dd>Returns an integer representing the length, in sample-frames, of the PCM data stored in the buffer.</dd>
 <dt>{{domxref("AudioBuffer.duration")}} {{readonlyInline}}</dt>
 <dd>Returns a double representing the duration, in seconds, of the PCM data stored in the buffer.</dd>
 <dt>{{domxref("AudioBuffer.numberOfChannels")}} {{readonlyInline}}</dt>
 <dd>Returns an integer representing the number of discrete audio channels described by the PCM data stored in the buffer.</dd>
</dl>

<h2 id="Methods">Methods</h2>

<dl>
 <dt>{{domxref("AudioBuffer.getChannelData()")}}</dt>
 <dd>Returns a {{jsxref("Float32Array")}} containing the PCM data associated with the channel, defined by the <code>channel</code> parameter (with <code>0</code> representing the first channel).</dd>
 <dt>{{domxref("AudioBuffer.copyFromChannel()")}}</dt>
 <dd>Copies the samples from the specified channel of the <span class="idlType"><code>AudioBuffer</code></span> to the <code>destination</code> array.</dd>
 <dt>{{domxref("AudioBuffer.copyToChannel()")}}</dt>
 <dd>Copies the samples to the specified channel of the <span class="idlType"><code>AudioBuffer</code></span>, from the <code>source</code> array.</dd>
</dl>

<h2 id="Example">Example</h2>

<p>The following simple example shows how to create an <code>AudioBuffer</code> and fill it with random white noise. You can find the full source code at our <a href="https://github.com/mdn/audio-buffer">audio-buffer demo</a> repository; a <a href="https://mdn.github.io/audio-buffer/">running live</a> version is also available.</p>

<pre class="brush: js;highlight:[7,14,27]">
// 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 &lt; channels; channel++) {
    // This gives us the actual array that contains the data
    var nowBuffering = myArrayBuffer.getChannelData(channel);
    for (var i = 0; i &lt; 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="Specification">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</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="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>14 {{property_prefix("webkit")}}</td>
   <td>{{CompatGeckoDesktop(25)}}</td>
   <td>{{CompatNo}}</td>
   <td>15&nbsp;{{property_prefix("webkit")}}<br />
    22 (unprefixed)</td>
   <td>6 {{property_prefix("webkit")}}</td>
  </tr>
  <tr>
   <td><code>copyFromChannel()</code> and <code>copyToChannel()</code></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(27)}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>28&nbsp;{{property_prefix("webkit")}}</td>
   <td>{{CompatGeckoMobile(25)}}</td>
   <td>1.2</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>6&nbsp;{{property_prefix("webkit")}}</td>
  </tr>
  <tr>
   <td><code>copyFromChannel()</code> and <code>copyToChannel()</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(27)}}</td>
   <td>&nbsp;</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li>
</ul>
Revert to this revision