<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<style>
button {
  position: fixed;
  top: 46px;
  left: 46px;
  z-index: 100;
  display: inline-block;
  font-size: 48px;
  border: none;
  background: none;
  color: rgba(223, 6, 39, 0.8);
  cursor: pointer;
}

button:hover {
  color: rgba(255, 0, 128, 0.8);
}  

button:focus  {
  outline: 0
}

#canvas {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

#player {
  position: fixed;
  left: 10px;
  bottom: 10px;
  width: calc(100% - 20px);
}
</style>
</head>
<body>

<button id='load' class='load' type='button'>▶</button>
<canvas id="canvas"></canvas>
<audio id="player" controls>
  <source src='about:blank'>
</audio>

<script>

document.getElementById('load').addEventListener('click', audioViz);

function audioViz(e) {

  var player = document.getElementById("player");
  
  player.crossOrigin = "anonymous";
  player.src = "https://glsbx.s3-us-west-1.amazonaws.com/-/dd.mp3";
  player.load();    
  player.play();
  
  var context = new AudioContext();
  var src = context.createMediaElementSource(player);
  var analyser = context.createAnalyser();

  var canvas = document.getElementById("canvas");
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  var ctx = canvas.getContext("2d");

  src.connect(analyser);
  analyser.connect(context.destination);

  analyser.fftSize = 256;

  var bufferLength = analyser.frequencyBinCount;
  console.log(bufferLength);

  var dataArray = new Uint8Array(bufferLength);

  var WIDTH = canvas.width;
  var HEIGHT = canvas.height;

  var barWidth = (WIDTH / bufferLength) * 2.5;
  var barHeight;
  var x = 0;

  function renderFrame() {
  
    requestAnimationFrame(renderFrame);

    x = 0;

    analyser.getByteFrequencyData(dataArray);

    ctx.fillStyle = "#000";
    ctx.fillRect(0, 0, WIDTH, HEIGHT);

    for (var i = 0; i < bufferLength; i++) {
      barHeight = dataArray[i];

      var r = barHeight + (25 * (i / bufferLength));
      var g = 250 * (i / bufferLength);
      var b = 50;

      ctx.fillStyle = "rgb(" + r + "," + g + "," + b + ")";
      ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);

      x += barWidth + 1;
    }
  }

  player.play();
  renderFrame();
}


</script>
</body>
</html>