我正在尝试构建一个实时语音通话应用程序。我的目标是使用原生 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);
我得到的缓冲区正确吗?我怎么玩?