decodeAudioData HTML5音频API

Cra*_*dks 23 javascript html5 buffer decode html5-audio

我想从ArrayBuffer播放音频数据...所以我生成我的数组并用微缩输入填充它.如果我在画布上绘制这些数据,它看起来像 - > 在此输入图像描述

这样可行!

但是,如果我想听这个数据

context.decodeAudioData(tmp, function(bufferN) { //tmp is a arrayBuffer
    var out = context.createBufferSource();
    out.buffer = bufferN;
    out.connect(context.destination);
    out.noteOn(0);
}, errorFunction);
Run Code Online (Sandbox Code Playgroud)

我听不到任何声音......因为调用了errorFunction.但错误是空的!

我也试着像这样得到缓冲区:

var soundBuffer = context.createBuffer(myArrayBuffer, true/*make mono*/);
Run Code Online (Sandbox Code Playgroud)

但我得到错误:Uncaught SyntaxError:指定了无效或非法字符串.

谁能给我一个暗示?

编辑1(更多代码以及我如何获得麦克风输入):

 navigator.webkitGetUserMedia({audio: true}, function(stream) {

                liveSource = context.createMediaStreamSource(stream);

                // create a ScriptProcessorNode
                if(!context.createScriptProcessor){
                   node = context.createJavaScriptNode(2048, 1, 1);
                } else {
                   node = context.createScriptProcessor(2048, 1, 1);
                }


                node.onaudioprocess = function(e){

               var tmp = new Uint8Array(e.inputBuffer.byteLength);
               tmp.set(new      Uint8Array(e.inputBuffer.byteLength), 0);

   //Here comes the code from above.
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

vza*_*llo 6

回调函数返回的错误为null,因为在当前的webaudio api规范中该函数不会返回对象错误

callback DecodeSuccessCallback = void (AudioBuffer decodedData);
callback DecodeErrorCallback = void ();

    void decodeAudioData(ArrayBuffer audioData,
                         DecodeSuccessCallback successCallback,
                         optional DecodeErrorCallback errorCallback);
Run Code Online (Sandbox Code Playgroud)

当完整输入ArrayBuffer被解码并在内部存储为AudioBuffer时会引发DecodeSuccessCallback但由于某些未知原因,decodeAudioData无法解码实时流.

处理音频时,您可以尝试播放捕获的缓冲区设置输出缓冲区数据

function connectAudioInToSpeakers(){

  //var context = new webkitAudioContext();  
  navigator.webkitGetUserMedia({audio: true}, function(stream) {

    var context = new webkitAudioContext();  
    liveSource = context.createMediaStreamSource(stream);

    // create a ScriptProcessorNode
    if(!context.createScriptProcessor){
       node = context.createJavaScriptNode(2048, 1, 1);
    } else {
       node = context.createScriptProcessor(2048, 1, 1);
    }


    node.onaudioprocess = function(e){

        try{
            ctx.clearRect(0, 0, document.getElementById("myCanvas").width, document.getElementById("myCanvas").height);
            document.getElementById("myCanvas").width = document.getElementById("myCanvas").width;
            ctx.fillStyle="#FF0000";

            var input = e.inputBuffer.getChannelData(0);
            var output = e.outputBuffer.getChannelData(0);
            for(var i in input) {
                output[i] = input[i];
                ctx.fillRect(i/4,input[i]*500+200,1,1);
            }


        }catch (e){
            console.log('node.onaudioprocess',e.message);
        }

    }

     // connect the ScriptProcessorNode with the input audio
    liveSource.connect(node);
    // if the ScriptProcessorNode is not connected to an output the "onaudioprocess" event is not triggered in chrome
    node.connect(context.destination);

    //Geb mic eingang auf boxen
    //liveSource.connect(context.destination);
  });
}
Run Code Online (Sandbox Code Playgroud)