The getChannelData() method of the AudioBuffer Interface returns a Float32Array containing the PCM data associated with the channel, defined by the channel parameter (with 0 representing the first channel).
Syntax
var myArrayBuffer = audioCtx.createBuffer(2, frameCount, audioCtx.sampleRate); var nowBuffering = myArrayBuffer.getChannelData(channel);
Parameters
- channel
- The channel property is an index representing the particular channel to get data for. An index value of 0 represents the first channel. If the
channelindex value is greater than of equal toAudioBuffer.numberOfChannels, anINDEX_SIZE_ERRexception will be thrown.
Return value
A Float32Array.
Example
In the following example we create a two second buffer, fill it with white noise, and then play it via an AudioBufferSourceNode. The comments should clearly explain what is going on. You can also [[../../../../../../../mdn.github.io/webaudio-examples/audio-buffer/index|run the code live]], or view the source.
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();
}
Specification
| Specification | Status | Comment |
| Web Audio APIThe definition of 'getChannelData' in that specification. | Working Draft |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getChannelData
|
Chrome
Full support 14 |
Edge
Full support 12 |
Firefox
Full support 25 |
IE
No support No |
Opera
Full support 15 |
Safari
Full support 6 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support 26 |
Opera Android
Full support 14 |
Safari iOS
Full support 6 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
- No support
- No support
See also
AudioBuffer.getChannelData() by Mozilla Contributors is licensed under CC-BY-SA 2.5.