Skip to content

Instantly share code, notes, and snippets.

@bigeyex
Created July 12, 2014 20:30
Show Gist options
  • Save bigeyex/dd5b75049e26ddb68f19 to your computer and use it in GitHub Desktop.
Save bigeyex/dd5b75049e26ddb68f19 to your computer and use it in GitHub Desktop.
simple web audio real-time data logging
<!DOCTYPE html>
<html>
<head>
<title>Audio jack</title>
</head>
<body>
<script>
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
// if (navigator.getUserMedia){
// navigator.getUserMedia({audio:true}, success, function(e) {
// alert('Error capturing audio.');
// });
// } else alert('getUserMedia not supported in this browser.');
navigator.getUserMedia({audio:true}, success, function(e) {
alert('Error capturing audio.');
});
function success(e){
// creates the audio context
audioContext = window.AudioContext || window.webkitAudioContext;
context = new audioContext();
// creates a gain node
volume = context.createGain();
// creates an audio node from the microphone incoming stream
audioInput = context.createMediaStreamSource(e);
// connect the stream to the gain node
audioInput.connect(volume);
/* From the spec: This value controls how frequently the audioprocess event is
dispatched and how many sample-frames need to be processed each call.
Lower values for buffer size will result in a lower (better) latency.
Higher values will be necessary to avoid audio breakup and glitches */
var bufferSize = 256; //2048
recorder = context.createScriptProcessor(bufferSize, 2, 2);
recorder.onaudioprocess = function(e){
console.log ('recording');
var left = e.inputBuffer.getChannelData (0);
var right = e.inputBuffer.getChannelData (1);
// we clone the samples
console.log(new Float32Array (left));
// leftchannel.push (new Float32Array (left));
// rightchannel.push (new Float32Array (right));
// recordingLength += bufferSize;
}
// we connect the recorder
volume.connect (recorder);
recorder.connect (context.destination);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment