Created
August 21, 2016 01:17
-
-
Save davidgilbertson/5f816d48f562405d683d630345006a65 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
const audioContext = new window.AudioContext(); | |
const analyser = audioContext.createAnalyser(); | |
const pitchSamples = []; | |
let audioReady = false; | |
navigator.getUserMedia( | |
{audio: true}, | |
stream => { | |
audioContext.createMediaStreamSource(stream).connect(analyser); | |
audioReady = true; | |
}, | |
err => console.log(err) | |
); | |
const dataArray = new Uint8Array(analyser.frequencyBinCount); | |
const canvasContext = refs.canvas.getContext('2d'); | |
canvasContext.fillStyle = 'firebrick'; | |
const drawWave = () => { // this gets called via requestAnimationFrame, so runs roughly every 16ms | |
analyser.getByteTimeDomainData(dataArray); | |
let lastPos = 0; | |
dataArray.forEach((item, i) => { | |
if (item > 128 && lastItem <= 128) { // we have crossed below the mid point | |
const elapsedSteps = i - lastPos; // how far since the last time we did this | |
lastPos = i; | |
const hertz = 1 / (elapsedSteps / 44100); | |
pitchSamples.push(hertz); // an array of every pitch encountered | |
} | |
canvasContext.fillRect(i, item, 1, 1); // point in the wave | |
lastItem = item; | |
}); | |
}; | |
const renderAudio = () => { | |
requestAnimationFrame(renderAudio); | |
if (!audioReady) return; | |
canvasContext.clearRect(0, 0, 1024, 300); | |
drawWave(); | |
}; | |
renderAudio(); // kick the whole thing off | |
setInterval(() => { | |
renderKey(pitchSamples); // defined elsewhere, will get the average pitch and render a key | |
}, 250); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment