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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <div class="musicControls">
  <input id="audiofile" type="file"/>
  <button class="playPauseButton">&#9654;</button>
</div>
<h3 class="loader">Loading...</h3>
              
            
!

CSS

              
                body { 
    margin: 0;
    overflow: hidden;
    color: white;
    background-color: black;
}
.musicControls {
    position: absolute;
    top: 15px;
    left: 15px;
}
.loader {
    font-family: sans-serif;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50% -50%);
    display: none;
}
.loader.loading  { display: block; }
              
            
!

JS

              
                var fft, // Allow us to analyze the song
    numBars = 1024, // The number of bars to use; power of 2 from 16 to 1024
    song; // The p5 sound object

// Returns a single rgb color interpolation between given rgb color
// based on the factor given; via https://codepen.io/njmcode/pen/axoyD?editors=0010
function interpolateColor(color1, color2, factor) {
    if (arguments.length < 3) { 
        factor = 0.5; 
    }
    var result = color1.slice();
    for (var i = 0; i < 3; i++) {
        result[i] = Math.round(result[i] + factor * (color2[i] - color1[i]));
    }
    return result;
};
// My function to interpolate between two colors completely, returning an array
function interpolateColors(color1, color2, steps) {
    var stepFactor = 1 / (steps - 1),
        interpolatedColorArray = [];

    color1 = color1.match(/\d+/g).map(Number);
    color2 = color2.match(/\d+/g).map(Number);

    for(var i = 0; i < steps; i++) {
        interpolatedColorArray.push(interpolateColor(color1, color2, stepFactor * i));
    }

    return interpolatedColorArray;
}


// Load our song
var loader = document.querySelector(".loader");
document.getElementById("audiofile").onchange = function(event) {
    if(event.target.files[0]) {
        if(typeof song != "undefined") { // Catch already playing songs
            song.disconnect();
            song.stop();
        }
        
        // Load our new song
        song = loadSound(URL.createObjectURL(event.target.files[0]));
        loader.classList.add("loading");

        songStarted = false;
    }
}


var canvas;
function setup() { // Setup p5.js
    canvas = createCanvas(windowWidth, windowHeight);
}

var colorArray = interpolateColors("rgb(94, 79, 162)", "rgb(247, 148, 89)", numBars),
    songStarted = false;
function draw() {
    background(51);
    
    if(typeof song != "undefined" 
       && song.isLoaded() 
       && !songStarted) { // Do once
        loader.classList.remove("loading");
        
        song.play();
        song.setVolume(0.5);
        playPauseButton.innerHTML = "&#9646;&#9646;";

        fft = new p5.FFT();
        fft.waveform(numBars);
        fft.smooth(0.85);

        songStarted = true;
    }
    
    if(typeof fft != "undefined") {
        var spectrum = fft.analyze();
        noStroke();
        for(var i = 0; i < numBars; i++) {
            fill(colorArray[i]);
            var x = map(i, 0, numBars, 0, width);
            var h = -height + map(spectrum[i], 0, 255, height, 0);
            rect(x, height, width / numBars, h);
        }
    }
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

var playPauseButton = document.querySelector(".playPauseButton");
playPauseButton.onclick = function() {
    if(!song.isPlaying()) {
        song.play();
        playPauseButton.innerHTML = "&#9646;&#9646;";
    } else {
        song.pause();
        playPauseButton.innerHTML = "&#9654;";
    }
}

              
            
!
999px

Console