11

我正在尝试构建一个实时语音通话应用程序。我的目标是使用原生 JS 麦克风 api 并通过 websocket 将数据发送到其他客户端。我想出了以下代码:

<script>
// Globals
var aCtx;
var analyser;
var microphone;

navigator.getUserMedia_ = (   navigator.getUserMedia
                           || navigator.webkitGetUserMedia 
                           || navigator.mozGetUserMedia 
                           || navigator.msGetUserMedia);

if (navigator.getUserMedia_) {
    navigator.getUserMedia_({audio: true}, function(stream) {
        aCtx = new webkitAudioContext();
        analyser = aCtx.createAnalyser();
        microphone = aCtx.createMediaStreamSource(stream);
        microphone.connect(analyser);
        process();
    });
};
function process(){
    console.log(analyser);
    setInterval(function(){
        FFTData = new Float32Array(analyser.frequencyBinCount);
        analyser.getFloatFrequencyData(FFTData);

        console.log(FFTData); // display
    },10);
}

</script>

所以每 10 毫秒我将获取缓冲区并通过节点发送它。问题是我无法弄清楚如何播放缓冲区,我什至不确定我是否以正确的方式获得缓冲区。我试过了:

var source = audioContext.createBufferSource();
var buffer; // the result printed in the code below
var audioBuffer = audioContext.createBuffer(1, buffer.length, 44100);
audioBuffer.getChannelData(0).set(buffer);
source.buffer = audioBuffer;
source.connect(audioContext.destination);

我得到的缓冲区正确吗?我怎么玩?

4

1 回答 1

6

To issue the playback of the buffer, you have to call the start method on your AudioBufferSourceNode instance. The problem here is: you want to playback an audio stream, and an AudioBuffer isn't designed for this. If you keep creating AudioBuffer objects, filling them with the data and providing them to your AudioBufferSourceNode instance, there will surely be noticeable pauses in the sound.

You should instead keep a cache buffer, fill it as soon as data arrives and empty it at normal speed (not immediately, you have to wait to have enough milliseconds of audio in it).

The best way to do this is using the properly APIs provided: give a look at http://www.w3.org/TR/webaudio/#MediaStreamAudioDestinationNode-section and http://www.w3.org/TR/webaudio/#MediaStreamAudioSourceNode-section.

于 2013-11-28T02:09:14.670 回答