aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/api/audiobuffer/index.html
blob: 1909323a6d5e7a43013524af8ca242f807e200cb (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
108
---
title: AudioBuffer
slug: Web/API/AudioBuffer
translation_of: Web/API/AudioBuffer
---
<p>{{APIRef("Web Audio API")}}</p>

<p>La interfaz <strong><code>AudioBuffer</code></strong> representa un pequeño recurso de audio que se almacena en memoria, creado a partir de un archivo de audio usando el método {{ domxref("AudioContext.decodeAudioData()") }}, o de datos en bruto con el método {{ domxref("AudioContext.createBuffer()") }}. Una véz cargado en AudioBuffer, el audio puede ser reproducido pasandolo en el método {{ domxref("AudioBufferSourceNode") }}.</p>

<p>Objetos de este tipo son diseñados para almacenar pequeños trozos de audio, normalmente menos de 45 segundos. Para audios más extensos, los objectos implementan  {{domxref("MediaElementAudioSourceNode")}} que es más adecuado. El buffer contiene data en el siguiente formado: 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="Constructor">Constructor</h2>

<dl>
 <dt>{{domxref("AudioBuffer.AudioBuffer", "AudioBuffer()")}}</dt>
 <dd>Crea y retorna una nueva instancia de <code>AudioBuffer</code> </dd>
</dl>

<h2 id="Propiedades">Propiedades</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="Métodos">Métodos</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="Ejemplo">Ejemplo</h2>

<p>El siguiente ejemplo muestra como se crea un <code>AudioBuffer</code>  y rellena con un sonido blanco aleatorio. Puedes encontrar el código completo en nuestro repositorio: <a href="https://github.com/mdn/webaudio-examples">webaudio-examples</a>; y una versión disponible: <a href="https://mdn.github.io/webaudio-examples/audio-buffer/">running live</a></p>

<pre class="brush: js">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 &lt; myArrayBuffer.numberOfChannels; channel++) {
  // This gives us the actual array that contains the data
  var nowBuffering = myArrayBuffer.getChannelData(channel);
  for (var i = 0; i &lt; 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();
</pre>

<h2 id="Especificaciones">Especificaciones</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="Compatibilidad_de_navegadores">Compatibilidad de navegadores</h2>

<div>
<div class="hidden">La tabla de compatibilidades en esta página está generada por una data estructurada. Si te gustaria contribuir incluyendo data, por favor reviza <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> y envianos un pull request.</div>

<p>{{Compat("api.AudioBuffer")}}</p>
</div>

<h2 id="Mira_también">Mira también</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>