Last active
February 11, 2016 06:33
-
-
Save rmcauley/a30b5808bcd478bf098e 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
var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); | |
var source; | |
var analyser = audioCtx.createAnalyser(); | |
analyser.minDecibels = -60.0; | |
analyser.maxDecibels = 0.0; | |
analyser.smoothingTimeConstant = 0.8; | |
analyser.fftSize = 128; | |
var javascriptNode = audioCtx.createScriptProcessor(1024); | |
javascriptNode.onaudioprocess = function(evt) { | |
var array = new Float32Array(analyser.frequencyBinCount); | |
analyser.getFloatFrequencyData(array); | |
var bufLength = array.length; | |
var db; | |
var sum = 0; | |
var peak = 0; | |
for (var i = 0; i < bufLength; i++) { | |
db = array[i]; // - analyser.minDecibels; | |
sum += (db * db); | |
peak = Math.max(db, peak); | |
} | |
var rms = Math.sqrt(sum / bufLength); | |
javascriptNode.dbVolume = rms; | |
}; | |
var drawMicVolume = function(timestamp) { | |
console.log(javascriptNode.dbVolume); | |
requestAnimationFrame(drawMicVolume); | |
}; | |
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { | |
navigator.mediaDevices.getUserMedia({ audio: true}).then(function(stream) { | |
source = audioCtx.createMediaStreamSource(stream); | |
source.connect(analyser); | |
source.connect(javascriptNode); | |
// analyser.connect(audioCtx.destination); | |
requestAnimationFrame(drawMicVolume); | |
}).catch(function(err) { | |
console.error("The following getUserMedia error occured: " + err); | |
}); | |
} else { | |
console.error("navigator.mediaDevices.getUserMedia does not exist."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment