Created
October 3, 2015 15:48
-
-
Save jarlg/250decbbc50ce091f79e to your computer and use it in GitHub Desktop.
js example using webaudio to analyse mic input to control an oscillator
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<script type=text/javascript> | |
navigator.getUserMedia = navigator.getUserMedia | |
|| navigator.webkitGetUserMedia | |
|| navigator.mozGetUserMedia; | |
navigator.getUserMedia({ video : false, audio : true }, callback, console.log); | |
function callback(stream) { | |
var ctx = new AudioContext(); | |
var mic = ctx.createMediaStreamSource(stream); | |
var analyser = ctx.createAnalyser(); | |
var osc = ctx.createOscillator(); | |
mic.connect(analyser); | |
osc.connect(ctx.destination); | |
osc.start(0); | |
var data = new Uint8Array(analyser.frequencyBinCount); | |
function play() { | |
analyser.getByteFrequencyData(data); | |
// get fullest bin | |
var idx = 0; | |
for (var j=0; j < analyser.frequencyBinCount; j++) { | |
if (data[j] > data[idx]) { | |
idx = j; | |
} | |
} | |
var frequency = idx * ctx.sampleRate / analyser.fftSize; | |
console.log(frequency); | |
osc.frequency.value = frequency; | |
requestAnimationFrame(play); | |
} | |
play(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment