Skip to content

Instantly share code, notes, and snippets.

@epicure
Created December 17, 2012 12:00
Show Gist options
  • Save epicure/4317796 to your computer and use it in GitHub Desktop.
Save epicure/4317796 to your computer and use it in GitHub Desktop.
var ac = new webkitAudioContext();
var cap = {};
navigator.webkitGetUserMedia({audio: true}, function(stream) {
cap.mic = ac.createMediaStreamSource(stream);
}, null);
// 여기까지 일단 실행하고~ 마이크 입력을 허락해 주고.
var analyser = ac.createAnalyser();
var buff = new Uint8Array(analyser.frequencyBinCount);
var g = document.createElement('canvas').getContext('2d');
g.canvas.width = 512;
g.canvas.height = 256;
document.body.appendChild(g.canvas);
cap.mic.connect(analyser);
var update = function() {
var i, x, y;
g.clearRect(0, 0, g.canvas.width, g.canvas.height);
analyser.getByteTimeDomainData(buff);
y = g.canvas.height * 0.5;
for(i = 0; i < buff.length; i++) {
x = i / buff.length * g.canvas.width;
g.beginPath();
g.moveTo(x, y);
g.lineTo(x, y + (buff[i] / 255 - 0.5) * g.canvas.height);
g.stroke();
}
};
var loop = function() {
update();
webkitRequestAnimationFrame(loop);
};
loop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment