Last active
December 13, 2015 17:38
-
-
Save foldi/4949155 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
<h1>Audio visualization</h1> | |
<script> | |
var numRows = 16, bars = []; | |
for (var i = 0; i < numRows * 2; i++) { | |
var bar = document.createElement('meter'); | |
bar.min = -1; | |
document.body.appendChild(bar); | |
// Add a newline after each pair of bars. | |
if (i % 2) document.body.appendChild(document.createElement('br')); | |
bars.push(bar); | |
} | |
require(['$api/audio', '$api/models'], function(audio, models) { | |
var analyzer = audio.RealtimeAnalyzer.forPlayer(models.player); | |
analyzer.addEventListener('audio', function(evt) { | |
console.log(evt); | |
// There will be 256 samples, but we want to only display every [step] | |
// samples because we have fewer than 256 rows. | |
var step = 256 / numRows; | |
for (var i = 0; i < numRows; i++) { | |
bars[i * 2].value = evt.audio.wave.left[step * i]; | |
bars[i * 2 + 1].value = evt.audio.wave.right[step * i]; | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment