Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>Suelte aquí un archivo de sonido</p>
<canvas id="canvas"></canvas>
              
            
!

CSS

              
                p {
  text-align: center;
  color: hsl(210, 95%, 45%);
  width: 700px;
  position: absolute;
  width:100%;
  top: 150px;
  z-index: -1;
}

#canvas {
  display: block;
  margin: 0 auto;
  border: 1px dotted hsl(210, 95%, 45%);
}

              
            
!

JS

              
                // A U D I O
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var analizador = audioCtx.createAnalyser();
analizador.fftSize = 64; // [32, 64, 128, 256, 512, 1024, 2048]
var dataArray = new Uint8Array(analizador.frequencyBinCount);
var url, audio, audiofile, fuenteDeReproduccion;


// C A N V A S

var canvas = document.querySelector("canvas");
var p = document.querySelector("p");
var ctx = canvas.getContext("2d");
var cw = (canvas.width = 700),
  cx = cw / 2;
var ch = (canvas.height = 300),
  cy = ch / 2;
ctx.fillStyle = "hsla(210,95%,45%,.75)";


var barras = [];// el array de barras
var bNum = 32; // el número de barras

for (var i = 0; i < bNum; i++) {
  var barra = {};
  barra.w = cw / bNum;
  barra.h = 0;
  barra.x = i * barra.w;
  barra.y = ch;
  barras.push(barra);
}


canvas.addEventListener("dragenter", dragenter, false);
canvas.addEventListener("dragover", dragover, false);
canvas.addEventListener("drop", drop, false);

function dragenter(e) {
  e.stopPropagation();
  e.preventDefault();
}

function dragover(e) {
  e.stopPropagation();
  e.preventDefault();
}

function drop(e) {
  e.stopPropagation();
  e.preventDefault();

  var datos = e.dataTransfer;
  var archivos = datos.files;

  manejarArchivos(archivos);
}

function manejarArchivos(archivos) {
  for (var i = 0; i < archivos.length; i++) {
    audioFile = archivos[i]; 

    
    // si es un archivo de sonido empieza con "audio/"
    var esAudio = /^audio\//;
    // si no esAudio ignoralo
    if (!esAudio.test(audioFile.type)){
      continue;
    }

    reproducirAudio();
    visualizarSonido();
    Animacion();
  }
}

function reproducirAudio() {
  audio = new Audio();
  audio.autoplay = true;
  audio.src = window.URL.createObjectURL(audioFile);
  audio.play();
  
  var name = audioFile.name;
  p.innerHTML = name;
  
  audio.onended = function() {
    p.innerHTML = "Suelte un archivo de sonido aqu&iacute;";
    window.URL.revokeObjectURL(audio.src);  
  };
}

function visualizarSonido() {
  fuenteDeReproduccion = audioCtx.createMediaElementSource(audio);
  fuenteDeReproduccion.connect(analizador);
  analizador.connect(audioCtx.destination);
}

// Creamos una función que genera una nueva fotograma
function Animacion() {
  requestId = window.requestAnimationFrame(Animacion);
  /*el método getByteFrequencyData() toma como argumento un array de tipo Uint8Array*/
  analizador.getByteFrequencyData(dataArray);
  ctx.clearRect(0, 0, cw, ch);
  // la doble tilde (~~) es un operador equivalente a Math.floor() o casi
  var n = ~~(analizador.frequencyBinCount / bNum);
  for (var i = 0; i < barras.length; i++) {
    barras[i].h = -dataArray[i * n]; // altura negativa!!
    ctx.beginPath();
    ctx.fillRect(barras[i].x, barras[i].y, barras[i].w - 1, barras[i].h);
  }
}

              
            
!
999px

Console