Skip to content

Instantly share code, notes, and snippets.

Created November 13, 2012 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4064367 to your computer and use it in GitHub Desktop.
Save anonymous/4064367 to your computer and use it in GitHub Desktop.
grab audio webRTC audio stream and play with the buffer in javascript
var el = document.getElementById('sound');
var vid = document.getElementById('vid');
var audio = new webkitAudioContext();
var js = audio.createJavaScriptNode(1024);
js.connect(audio.destination);
js.onaudioprocess = function(obj){
var buf = obj.outputBuffer.getChannelData(1) // obj.inputBuffer.getChannelData(1)
var buf2 = obj.inputBuffer.getChannelData(1);
var x;
for(x = 0; x < buf.length; x++){
buf[x] = buf2[x]
}
// console.log(buf2);
}
js.connect(audio.destination)
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
function convertToMono( input ) {
var splitter = audio.createChannelSplitter(2);
var merger = audio.createChannelMerger(2);
input.connect( splitter );
splitter.connect( merger, 0, 0 );
splitter.connect( merger, 0, 1 );
return merger;
}
function gotStream(stream) {
// Create an AudioNode from the stream.
var input = audio.createMediaStreamSource(stream);
var url = webkitURL.createObjectURL(stream);
vid.src = url;
audioInput = convertToMono( input );
audioInput.connect(js);
}
if (!navigator.webkitGetUserMedia)
return(alert("Error: getUserMedia not supported!"));
navigator.webkitGetUserMedia({audio:true, video: true}, gotStream, function(e) {
alert('Error getting audio');
console.log(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment