简体   繁体   中英

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM