Skip to content

Instantly share code, notes, and snippets.

@tiborsaas
Forked from leemartin/wave64.html
Created April 13, 2012 14:38
Show Gist options
  • Save tiborsaas/2377315 to your computer and use it in GitHub Desktop.
Save tiborsaas/2377315 to your computer and use it in GitHub Desktop.
$.wave64 Example with invert waveform
<html>
<head>
<title>SoundCloud $.wave64 Example</title>
<style>
body{
background: #333;
}
canvas{
display: block;
margin: auto;
}
</style>
<!-- Include jQuery, SoundCloud JS SDK, and the $.wave64 Plugin -->
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>
<script src='http://connect.soundcloud.com/sdk.js'></script>
<script src='http://wave64.it/jquery.wave64.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
// Get the canvas & its context, width, and height, waveform color, gradient level
var canvas = $("#waveform")[0];
context = canvas.getContext("2d");
width = context.canvas.clientWidth,
height = context.canvas.clientHeight,
targetColor = [255, 102, 0],
gi = 0, // Gradient index
gradLevel = 0.5; // Level of gradient effent. 0 = no gradient
// Make sure the canvas is sized to the appropriate size.
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
// Initialize the SoundCloud Javascript SDK
SC.initialize({
client_id: "YOUR_CLIENT_ID"
});
// SC.get a track from SoundCloud.
SC.get("/tracks/33820987", function(track) {
// Pass the waveform url, height, width, RGB array, and callback to $wave64.
$.wave64(track.waveform_url, height, width, targetColor, function(waveformData) {
// Iterate through the canvas pixel array, and invert the transparency
for(var i=0; i<waveformData.data.length; i+=4) {
if(i%width == 0) {
gi++;
}
waveformData.data[i] = targetColor[0];
waveformData.data[i+1] = targetColor[1];
waveformData.data[i+2] = targetColor[2];
waveformData.data[i+3] = 255 - waveformData.data[i+3] - gi*gradLevel;
}
context.putImageData(waveformData, 0, 0);
});
});
});
</script>
</head>
<body>
<canvas id='waveform' style='width:600px;height:100px'></canvas>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment