Created
November 13, 2012 06:48
-
-
Save anonymous/4064367 to your computer and use it in GitHub Desktop.
grab audio webRTC audio stream and play with the buffer in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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