Advertisement
Guest User

Audio visualizer stop or pause

a guest
Feb 2nd, 2013
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function initVisual(){
  2.     // create the audio context (chrome only for now)
  3.     var context = new webkitAudioContext();
  4.     var audioBuffer;
  5.     var sourceNode;
  6.     var analyser;
  7.     var javascriptNode;
  8.  
  9.     // get the context from the canvas to draw on
  10.     var canvas = document.getElementById('visualizer');
  11.     var ctx = canvas.getContext("2d");
  12.  
  13.     // create a gradient for the fill. Note the strange
  14.     // offset, since the gradient is calculated based on
  15.     // the canvas, not the specific element we draw
  16.     var gradient = ctx.createLinearGradient(0,0,0,300);
  17.     gradient.addColorStop(1,'#000000');
  18.     gradient.addColorStop(0.75,'#ff0000');
  19.     gradient.addColorStop(0.25,'#ffff00');
  20.     gradient.addColorStop(0,'#ffffff');
  21.  
  22.     // load the sound
  23.     setupAudioNodes();
  24.     loadSound("skrillex-summit.mp3");
  25.  
  26.     function setupAudioNodes() {
  27.  
  28.         // setup a javascript node
  29.         javascriptNode = context.createJavaScriptNode(2048, 1, 1);
  30.         // connect to destination, else it isn't called
  31.         javascriptNode.connect(context.destination);
  32.  
  33.  
  34.         // setup a analyzer
  35.         analyser = context.createAnalyser();
  36.         analyser.smoothingTimeConstant = 0.3;
  37.         analyser.fftSize = 512;
  38.  
  39.         // create a buffer source node
  40.         sourceNode = context.createBufferSource();
  41.         sourceNode.connect(analyser);
  42.         analyser.connect(javascriptNode);
  43.  
  44.         sourceNode.connect(context.destination);
  45.     }
  46.  
  47.     // load the specified sound
  48.     function loadSound(url) {
  49.         var request = new XMLHttpRequest();
  50.         request.open('GET', url, true);
  51.         request.responseType = 'arraybuffer';
  52.  
  53.         // When loaded decode the data
  54.         request.onload = function() {
  55.  
  56.             // decode the data
  57.             context.decodeAudioData(request.response, function(buffer) {
  58.                 // when the audio is decoded play the sound
  59.                 playSound(buffer);
  60.             }, onError);
  61.         }
  62.         request.send();
  63.     }
  64.  
  65.  
  66.     function playSound(buffer) {
  67.         sourceNode.buffer = buffer;
  68.         sourceNode.noteOn(0);
  69.     }
  70.  
  71.     // log if an error occurs
  72.     function onError(e) {
  73.         console.log(e);
  74.     }
  75.  
  76.     // when the javascript node is called
  77.     // we use information from the analyzer node
  78.     // to draw the volume
  79.     javascriptNode.onaudioprocess = function() {
  80.  
  81.         // get the average for the first channel
  82.         var array =  new Uint8Array(analyser.frequencyBinCount);
  83.         analyser.getByteFrequencyData(array);
  84.  
  85.         // clear the current state
  86.         ctx.clearRect(0, 0, 1000, 325);
  87.  
  88.         // set the fill style
  89.         ctx.fillStyle=gradient;
  90.         drawSpectrum(array);
  91.  
  92.     }
  93.  
  94.  
  95.     function drawSpectrum(array) {
  96.         for ( var i = 0; i < (array.length); i++ ){
  97.             var value = array[i];
  98.  
  99.             ctx.fillRect(i*5,325-value,3,325);
  100.             //  console.log([i,value])
  101.         }
  102.     };
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement