<html>
<head>
<meta charset="utf-8">
<title>Web Audio API: An Overview</title>
<style>
#container {
text-align: center;
width: 500px;
margin: auto;
}
#fft {
background: black;
}
</style>
<script src="http://cwilso.github.io/AudioContext-MonkeyPatch/AudioContextMonkeyPatch.js"></script>
</head>
<body>
<div id="container">
<canvas height="200" width="500" id="fft"></canvas>
<audio id="audio" src="https://dl.dropboxusercontent.com/u/1631516/loveDaddy.mp3" preload controls></audio>
</div>
<script>
// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
</script>
<script>
// The audio element
audioElement = document.getElementById('audio');
// The canvas, its context and fillstyle
canvas = document.getElementById('fft');
ctx = canvas.getContext('2d');
ctx.fillStyle = "white";
// Create new Audio Context and an audio analyzer
audioContext = new webkitAudioContext();
analyser = audioContext.createAnalyser();
// Canvas' height and width
CANVAS_HEIGHT = canvas.height;
CANVAS_WIDTH = canvas.width;
// We'll need the offset later
OFFSET = 100;
// Spacing between the individual bars
SPACING = 10;
// Initialize and start drawing
// when the audio starts playing
window.onload = init;
audioElement.addEventListener('play', draw);
function init() {
// Take input from audioElement
source = audioContext.createMediaElementSource(audioElement);
// Connect the stream to an analyzer
source.connect(analyser);
// Connect the analyzer to the speakers
analyser.connect(audioContext.destination);
// Start the animation
draw();
}
function draw() {
// See http://paulirish.com/2011/requestanimationframe-for-smart-animating/
requestAnimFrame(draw, canvas);
// New typed array for the raw frequency data
freqData = new Uint8Array(analyser.frequencyBinCount);
// Put the raw frequency into the newly created array
analyser.getByteFrequencyData(freqData);
// Clear the canvas
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
// This loop draws all the bars
for (var i = 0; i < freqData.length - OFFSET; i++) {
// Work out the hight of the current bar
// by getting the current frequency
var magnitude = freqData[i + OFFSET];
// Draw a bar from the bottom up (cause for the "-magnitude")
ctx.fillRect(i * SPACING, CANVAS_HEIGHT, SPACING / 2, -magnitude);
};
}
</script>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |