Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert AudioBuffer to ArrayBuffer

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
like image 266
random Avatar asked Aug 17 '13 21:08

random


1 Answers

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.

like image 86
The Busy Wizard Avatar answered Sep 27 '22 18:09

The Busy Wizard