Skip to content

Instantly share code, notes, and snippets.

@tjenkinson
Last active November 14, 2016 15:30
Show Gist options
  • Save tjenkinson/4ee33c784ee60570d3ea7f87802d8d83 to your computer and use it in GitHub Desktop.
Save tjenkinson/4ee33c784ee60570d3ea7f87802d8d83 to your computer and use it in GitHub Desktop.
// e.g
var monitor = new Monitor(video);
setInterval(function() {
if (monitor.getLevel() < 10) {
console.log("No sound");
}
}, 100);
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