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

              
                
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.1/addons/p5.dom.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

              
                html, body {
  margin: 0;
  padding: 0;
}
              
            
!

JS

              
                /* PDM Course: Sound Unit

Alternating Between Multiple patterns


Based on a project by Jake Albaugh

Code by Anthony T. Marasco and Tate Carson [2018]

Press the P key to start the Transport and the S key 
to stop the Transport

Click on each tab to switch between different sequences!
*/
 
let patterns = [
  ["D4", "F5", "A5", "C6", "D6", "A6"],
  ["A3", "C#4", "E4", "G4", "A4", "E5"],
  ["B3", "D4", "F#4", "A4", "B4", "F#5"],
  ["G3", "B4", "D4", "F4", "G4", "D5"], 
  ["A3", "C#4", "F4", "G4", "A4", "E5"]
];

let sequence; 
let tabs = [];
let currentTab =1;
let unclickedColor = [0, 0, 0, 0, 0];

let synth;
let currentSequence;
let sequenceNum = 5; 



function setup() {
  createCanvas(windowWidth, windowHeight);
  
  synth = new Tone.Synth({
    oscillator: {
      type: "fatsawtooth"
    }
  }).toDestination();

  for (let i = 0; i <= sequenceNum; i++) {
    tabs[i] = new Tab(i,windowWidth / sequenceNum, unclickedColor, i+1);
  }
  
  sequence = new Tone.Sequence(function(time, note){
     synth.triggerAttackRelease(note, "16n", time);
  }, patterns[0], "16n");
 
  
  
   Tone.Transport.bpm.value = 100;
 
  
  sequence.start();
  
  tabs[0].isSelected = true;
  tabs[0].changeColor();
}

//Tab class constructor function
function Tab(locationX, width,tColor, tLabel) {
  this.x = locationX * width;
  this.y = 0;
  this.width = width;
  this.color = tColor;
  this.label = tLabel;

  this.height = height;
  this.isSelected = false;
  let colors = [
    [234,231,175],
    [138,229,218],
    [253,255,0],
    [251,200,138], 
    [55,159,167]
  ];

  this.display = function() {
    stroke(122);
    fill(this.color);
    rect(this.x, this.y, this.width, this.height);
    textAlign(CENTER);
    fill(255);
    textSize(30);
    text(this.label, this.x + 45, this.height / 2);
  };

  this.checkForClick = function() {
    if (this.isSelected == false) {
      if (
        mouseX > this.x &&
        mouseX < this.x + this.width &&
        mouseY > this.y &&
        mouseY < this.height
      ) {
        this.isSelected = true;
        return true;
      }
    }
  };

  this.changeColor = function() {
    if (this.isSelected == true) {
      this.color = colors[this.label-1];
    }
  };
}

function draw() {
  background(200);
  for (let i = 0; i <= sequenceNum; i++) {
    if (tabs[i].label != currentTab) {
      tabs[i].isSelected = false;
      tabs[i].color = unclickedColor;
      
    }

    tabs[i].display();
  }
  
}

function mousePressed(){
  //remove previous sequence
  sequence.clear(); 
  
  for (let i = 0; i <= sequenceNum; i++) {
    if (tabs[i].checkForClick()) {
      tabs[i].changeColor();
      currentTab = tabs[i].label;
      console.log(tabs[i].color)
      //add patterns array to sequence
      for(let j = 0; j<=patterns[i].length; j++) {
        //j - index of sequence / index of pattern 
        //i - number of pattern 
        sequence.add(j, patterns[i][j]);
      }     
    }
  }

}


function keyPressed() { 
  
  if(key == "P"){
    
    Tone.Transport.start();
  } else if (key == "S"){
    
    Tone.Transport.stop();
  } 
  
  console.log(key);
}


              
            
!
999px

Console