Convert AudioBuffer to ArrayBuffer

3.6k views Asked by At

I've been looking all over the internet for an answer to this question and I can't find any resources. Does anyone know how to use JavaScript to convert and AudioBuffer object to an ArrayBuffer? This is my current attempt

//Send audio data to websockets server
function callback(stream) {

var ws = new WebSocket("ws://localhost:8080");
ws.binaryType = 'arraybuffer';

var context = new webkitAudioContext();
var mediaStreamSource = context.createMediaStreamSource(stream);
console.log(mediaStreamSource);

var request = new XMLHttpRequest();
request.open('GET', 'http://s3.amazonaws.com/fldrMusic/01_-%20Fire%20Walker.mp3', true);    
request.responseType = 'arraybuffer'; 

console.log("Current 'request.response' value");  
console.log(request.response);

request.onload = function() {
  context.decodeAudioData(request.response, function(buffer){

    console.log("sent message");
    console.log(buffer); 
    ws.send(buffer);

    //Serialize javascript object
    console.log("sent message encoded");       
    var buff = Base64Binary.decodeArrayBuffer(buffer.toString());       
    console.log(buff);


  }, 
  function(buffer){ 
    alert("decode failed!");
  })
} 

request.send();
}//end 'callback' function
1

There are 1 answers

0
The Busy Wizard On
AudioBuffer.getChannelData(channel_number);

You can get the number of channels from AudioBuffer.numberOfChannels

EDIT:
For more details, you can check the MDN docs for AudioBuffer and the getChannelData method.