将AudioBuffer转换为ArrayBuffer

时间:2013-08-17 21:49:15

标签: javascript websocket audiobuffer

我一直在网上寻找这个问题的答案,我找不到任何资源。有谁知道如何使用JavaScript转换和AudioBuffer对象到ArrayBuffer?这是我目前的尝试

//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 个答案:

答案 0 :(得分:1)

AudioBuffer.getChannelData(channel_number);

您可以从AudioBuffer.numberOfChannels

获取频道数量

修改
有关详细信息,您可以查看AudioBuffergetChannelData方法的MDN文档。