-
-
Save fiWhy/6aeb925f0bf7d2d103c4243142c17bef to your computer and use it in GitHub Desktop.
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
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