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 id='animation'></div>


  
	<button id='playMusic' ontouchstart="sketch.playMusic()" onClick='sketch.playMusic()'>Play Music</button>
  
	

<div id='song'>
  
	<audio id='songPlayer' crossOrigin='anonymous' loop>
	  
    <source src='https://storage.googleapis.com/musicmation-83a98.appspot.com/changesTrimmed.mp3'  />
    
	</audio>
  
</div>
              
            
!

CSS

              
                body { margin : 0px !important; font-family : 'IBMPlexSans'}

#playMusic { 
  
  position : absolute; 
  top : 20px; 
  left : 20px; 
  cursor : pointer; 
  font-size : 20px; 
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -khtml-border-radius: 10px;  
  border : 0px solid #F687B3; 
  color : #fff; 
  background-color: #F687B3; 
  padding : 10px; 

}

*:focus {
    outline: none;
}
              
            
!

JS

              
                var sketch

const s = p => {
  
    //sketch wide variables can be put here.  useful
    //in the draw function where you may be incrementing
    //or using some fancy trigonometry
    let verticalPosition = 0;
  
    let x = 100;
    let y = 100;

    var beginLow, beginLowMid, beginMid

    store = []
    var canvas;
    store.audioStarted = false
    store.beatAlgorithm = "1"
    store.backgroundColor = "#48639B";
    store.fillColor = "#F687B3";
    store.fontColor = "#7F7F7F";
    store.strokeColor = "#ffffff";


    


    p.playMusic = function() {

      if ( store.audioStarted === false ) {
      
        //get the audio context of the sketch
        store.audioContext = sketch.getAudioContext()
      
        //wire the source of the audio to the hidden 
        //audio element
        store.source = store.audioContext.createMediaElementSource(document.getElementById('songPlayer'));
        
      }

        //safari likes to suspend audio, so resume it
        if (store.audioContext.state === 'suspended') {

            store.audioContext.resume()

        }

        //hook into the amplitude of the song
        //using the source
        store.amplitude = new p5.Amplitude();
        store.amplitude.smooth(0.9);
        store.amplitude.setInput(store.source)

        //hook into the frequency of the audio
        //using the source
        store.FFT = new p5.FFT();
        store.FFT.setInput(store.source)

        //wire everything end to end, e.g. out the speakers
        store.source.connect(store.audioContext.destination);

        //grab our audio and play it
        document.getElementById('songPlayer').play();
 document.getElementById('songPlayer').loop = false;
        store.audioStarted = true
        




    }


    p.isABeat = function(beginLow, beginLowMid, beginMid) {


            store.spectrum = store.FFT.analyze();

            store.bassEnergy = store.FFT.getEnergy("bass")
            store.lowMidEnergy = store.FFT.getEnergy("lowMid")
            store.midEnergy = store.FFT.getEnergy("mid")
            store.highMidEnergy = store.FFT.getEnergy("highMid")
            store.trebleEnergy = store.FFT.getEnergy("treble")


            var isBeat = false


            beginLow.update(store.FFT);
            beginLowMid.update(store.FFT);
            beginMid.update(store.FFT);

            //we allow the user to select 1 of 6 beat detection
            //strategies based on frequency.  Certain songs
            //lend themselves to certain strategies
            switch (store.beatAlgorithm) {
                case "1":
                    if (store.lowCount === 1) {
                        isBeat = true
                    }
                    // code block
                    break;
                case "2":
                    if (store.lowMidCount === 1) {
                        isBeat = true
                    }
                    break;
                case "3":
                    if (store.midCount === 1) {
                        isBeat = true
                    }
                    // code block
                    break;
                case "4":
                    if (store.lowCount + store.lowMidCount === 2) {
                        isBeat = true
                    }
                    break;
                case "5":
                    if (store.lowCount + store.midCount === 2) {
                        isBeat = true
                    }
                    break;
                case "6":
                    if (store.lowMidCount + store.midCount === 2) {
                        isBeat = true
                    }
                    break;
                default:
                    // code block
            }


            return isBeat




        },


        p.setup = function() {



            p.rectMode(p.CORNER)



            p.background('#3BDBFF');


            beginLow = new OnBeginningDetection(40, 120, "low", 0.025);
            beginLowMid = new OnBeginningDetection(140, 400, "lowMid", 0.025);
            beginMid = new OnBeginningDetection(400, 2600, "mid", 0.025);


            store.evenColor = store.backgroundColor



            //var canvas = p.createCanvas(250, 250);
            canvas = p.createCanvas(window.innerWidth, window.innerHeight);
            canvas.parent('animation');



            p.noStroke();



        }

    p.draw = function() {

        //lets reset our beat detection variables for this pass
        store.lowCount = 0
        store.lowMidCount = 0
        store.midCount = 0




        p.background(store.evenColor);



        //if the audio has started, lets do beat detection
        if (store.audioStarted) {

            //if the user has selected beat detection
            if (store.beatAlgorithm !== "0") {

                try {

                    //lets try and find a beat in the audio
                    if (p.isABeat(beginLow, beginLowMid, beginMid)) {

                        //flip the color on each subsequent beat
                        if (store.evenBeat === true) {

                            store.evenBeat = false
                            store.evenColor = store.backgroundColor
                            store.oddColor = store.fillColor

                        } else {

                            store.evenBeat = true
                            store.evenColor = store.fillColor
                            store.oddColor = store.backgroundColor

                        }



                    }

                } catch (err) {

                    //p5 chokes on FFT once in a while
                    //in production we reset it......
                    console.log(err)


                }

            }

        }
      
        //draw a line using two points in space
        sketch.stroke('#F687B3')
        sketch.strokeWeight(10)
        sketch.line(0, verticalPosition, sketch.width, verticalPosition);

        //modify the height during each draw iteration
        verticalPosition = verticalPosition - 0.5;
        if (verticalPosition < 0) {
            verticalPosition = sketch.height;
        }

        



    }


   


}



sketch = new p5(s);



function OnBeginningDetection(f1, f2, str, thresh) {
    this.isDetected = false;
    this.threshold = thresh;
    this.energy = 0;
    this.penergy = 0;
    this.siz = 10;
    this.sensitivity = 500;
    this.f1 = f1;
    this.f2 = f2;
    this.str = str;
}



OnBeginningDetection.prototype.update = function(fastFourier) {
    this.energy = fastFourier.getEnergy(this.f1, this.f2) / 255;

    if (this.isDetected == false) {
        if (this.energy - this.penergy > this.threshold) {
            this.isDetected = true;



            if (this.str === 'low') {
                store.lowCount = 1
            }
            if (this.str === 'lowMid') {
                store.lowMidCount = 1
            }
            if (this.str === 'mid') {
                store.midCount = 1
            }

            var self = this;
            setTimeout(function() {
                self.isDetected = false;
            }, this.sensitivity);
        }
    }

    this.penergy = this.energy;

}



              
            
!
999px

Console