Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var analyser = context.createAnalyser();
var source;
var freqData;
var samples = 128;
var initStream = function(stream) {
analyser.fftSize = samples;
source = context.createMediaStreamSource(stream);
source.connect(analyser);
freqData = new Uint8Array(analyser.frequencyBinCount);
};
var initAudio = function() {
if (!navigator.getUserMedia)
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
navigator.getUserMedia({audio:true}, initStream, function(e) {
alert('Hey, you should click "allow" so I can listen to you!');
console.log(e);
});
};
var maxValue = function(data) {
var max = 0;
var maxIndex = 0;
for(var i=0; i < data.length; i++) {
if(data[i] > max) {
maxIndex = i;
}
}
return maxIndex;
};
var getData = function() {
analyser.getByteFrequencyData(freqData);
console.log(freqData);
console.log(maxValue(freqData));
};
initAudio();
//setInterval(getData, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment