Skip to content

Instantly share code, notes, and snippets.

@cemrich
Last active Dec 18, 2015
Embed
What would you like to do?
Simple example of how to get audio data from live microphone input via getUserMedia.
window.onload = function() {
var context = new webkitAudioContext();
var analyser = context.createAnalyser();
var buffer = new Float32Array(analyser.frequencyBinCount);
function analyze() {
requestAnimationFrame(analyze);
analyser.getFloatFrequencyData(buffer);
// buffer is now filled with data
// if there are -100 values only set your systems microphone & speakers sample rates to the same values
console.log(buffer[0]);
}
function connectStream(stream) {
// uncomment if you want to playback the recorded audio too
//var audioElement = document.getElementById('audio');
//audioElement.autoplay = true;
//audioElement.src = window.webkitURL.createObjectURL(stream);
var source = context.createMediaStreamSource(stream);
source.connect(analyser);
analyze();
}
function microphoneError(e) {
alert('MicrophoneError error!', e);
};
if (navigator.webkitGetUserMedia) {
navigator.webkitGetUserMedia({audio: true, video: false}, connectStream, microphoneError);
} else {
alert('Your browser does not support webkitGetUserMedia.');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment