I'm trying to build a real-time voice call application. My goal is to use native JS microphone api and send data via websocket to the other clients. I figured out the following code:
<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>
so every 10ms I'm gonna get the buffer and send it via node. The problem is that I couldn't figure out how to play the buffer and I'm not even sure if I'm getting the buffer in right way. I've tried:
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);
Am I getting the buffer right? How I can play it?