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://cdn.jsdelivr.net/gh/molleindustria/p5.play/lib/p5.play.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

              
                /*
Making a Simple Synthesizer Sequence with Tone.js and P5
Programming Digital Media
code by Matthew A. Bardin [2020]

The goal of this code is to build on the basic Tone.Synth object and sequence the output notes to automatically play a melody
*/
//In addition to the original codefor making a synthesizer, you will need 2 new variables: one to store the sequencing information, and an array to hold the notes
let sequence1, simpSynth;
let bgMelody = ["C3", ["E3", "G3", "D3", "C3"], "A3", "B2", "C2", "E3", ["A2", "G2"], "C4"]; //each indexed element is one beat of musical time. Nested arrays are subdivisions of those beats


function setup() {
  createCanvas(400, 400);
//Make the synth like normal
    simpSynth = new Tone.Synth({
    oscillator: {
      type: "square" //the type of waveform the synthesizer produces. Can be square, since, triangle, or sawtooth
    },
    envelope: { //sets the various sound properties for the synth
      attack: 0.05,
      decay: 0.5,
      sustain: 1,
      release: 5
    }
  }).toDestination(); //sends the synth's output to the speakers

//Here we generate the sequencer.
  sequence1 = new Tone.Sequence(function(time, note) { //the object has a built in function where we call triggerAttackRelease(), which is what makes our synthesizer generate sound
    simpSynth.triggerAttackRelease(note, 0.5);
//adding the console log lets the programmer see the current note being generated
    console.log(note);
  }, bgMelody, '4n'); //after making the function, you have to specify what the notes you want to feed into it are, and how you are defining the beat. In this case, notes come from the bgMelody array, and each element is one quarter note

//Next we need to set up the clock that will act as the driving force for the sequence of notes. 
  Tone.Transport.bpm.value = 80; //how many beats(quarter notes) per minute
  Tone.Transport.start(); //starts the transport
}

function draw() {
  background(220, 0, 255);
 textAlign(CENTER);
  textFont('futura');
  text("Hold the mouse button play a melody. \nYou may have to click, then click and hold.", width / 2, height / 2);
}

//starts the sequence
function mousePressed() {
  Tone.start();
  sequence1.start();
  console.log('lets go!')
}

//end the sequence
function mouseReleased(){
  sequence1.stop();
}
              
            
!
999px

Console