Skip to content

Instantly share code, notes, and snippets.

@fiWhy
Forked from eric-wood/tuner.js
Created July 23, 2020 14:09
Show Gist options
  • Save fiWhy/6aeb925f0bf7d2d103c4243142c17bef to your computer and use it in GitHub Desktop.
Save fiWhy/6aeb925f0bf7d2d103c4243142c17bef to your computer and use it in GitHub Desktop.
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