Last active
November 14, 2016 15:30
-
-
Save tjenkinson/4ee33c784ee60570d3ea7f87802d8d83 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
// e.g | |
var monitor = new Monitor(video); | |
setInterval(function() { | |
if (monitor.getLevel() < 10) { | |
console.log("No sound"); | |
} | |
}, 100); |
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
function Monitor(video) { | |
var ctx = new webkitAudioContext(); | |
var src = ctx.createMediaElementSource(video); | |
var anylz = ctx.createAnalyser(); | |
src.connect(anylz); | |
alyz.connect(ctx.destination); | |
this._bins = new Uint8Array(alyz.frequencyBinCount); | |
this._alyz = anylz; | |
} | |
Monitor.prototype.getLevel = function() { | |
var bins = this._bins; | |
this._alyz.getByteFrequencyData(bins); | |
var sum = 0; | |
for (var i=0; i<bins.length; i++) { | |
sum += parseFloat(bins[i]); | |
} | |
return sum / bins.length; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment