Advertisement
Shiny_

Untitled

Nov 19th, 2014
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Generated by CoffeeScript 1.8.0
  2. (function() {
  3.   var URL, context, ctx, drawSpectrum, gradient, javascriptNode, loadSound, onError, playSound, setupAudioNodes, sourceNode;
  4.  
  5.   URL = "Gesaffelstein%20&%20Jesper%20Kyd%20-%20In%20Pursuit%20for%20Ezio%27s%20Family.ogg";
  6.  
  7.   if (!window.AudioContext) {
  8.     if (!window.webkitAudioContext) {
  9.       alert("Nie znaleziono obsługi dla AudioContext!");
  10.     }
  11.     window.AudioContext = window.webkitAudioContext;
  12.   }
  13.  
  14.   context = new AudioContext();
  15.  
  16.   ctx = $("#music").get()[0].getContext("2d");
  17.  
  18.   gradient = ctx.createLinearGradient(0, 0, 0, 325);
  19.  
  20.   javascriptNode = 0;
  21.  
  22.   sourceNode = 0;
  23.  
  24.   gradient.addColorStop(1, "#FFFFFF");
  25.  
  26.   gradient.addColorStop(0.75, "#00FFFF");
  27.  
  28.   gradient.addColorStop(0.25, "#0000FF");
  29.  
  30.   gradient.addColorStop(0, "#000000");
  31.  
  32.   setupAudioNodes();
  33.  
  34.   setupAudioNodes = function() {
  35.     var analyser;
  36.     javascriptNode = context.createScriptProcessor(2048, 1, 1);
  37.     javascriptNode.connect(context.destination);
  38.     analyser = context.createAnalyser();
  39.     analyser.smoothingTimeConstant = 0.75;
  40.     analyser.fftsize = 512;
  41.     sourceNode = context.createBufferSource();
  42.     sourceNode.connect(analyser);
  43.     analyser.connect(javascriptNode);
  44.     sourceNode.connect(context.destination);
  45.   };
  46.  
  47.   loadSound = function(url) {
  48.     var request;
  49.     request = new XMLHttpRequest();
  50.     request.open("GET", url, true);
  51.     request.responseType = "arraybuffer";
  52.     request.onload = function() {
  53.       context.decodeAudioData(request.response, function(buffer) {
  54.         playSound(buffer);
  55.       }, onError);
  56.     };
  57.     request.send();
  58.   };
  59.  
  60.   playSound = function(buffer) {
  61.     sourceNode.buffer = buffer;
  62.     sourceNode.start(0);
  63.   };
  64.  
  65.   onError = function(e) {
  66.     console.log(e);
  67.   };
  68.  
  69.   javascriptNode.onaudioprocess = function() {
  70.     var array;
  71.     array = new Uint8Array(analyser.frequencyBinCount);
  72.     analyser.getByteFrequencyData(array);
  73.     ctx.clearRect(0, 0, 1024, 325);
  74.     ctx.fillStyle = gradient;
  75.     drawSpectrum(array);
  76.   };
  77.  
  78.   drawSpectrum = function(array) {
  79.     var i, val;
  80.     i = 0;
  81.     while (i < array.length) {
  82.       val = array[i];
  83.       ctx.fillRect(i * 5, 325 - val, 3, 325);
  84.     }
  85.   };
  86.  
  87.   $(document).ready(function() {
  88.     $.ajax({
  89.       url: URL,
  90.       success: function() {
  91.         $("#play_button").show();
  92.       }
  93.     });
  94.   });
  95.  
  96.   loadSound(URL);
  97.  
  98. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement