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>

  <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

  Event
  like schedule or setInterval but with more features
*/

let synth;
let start;
let stop;
let pause;
let slider;
let startEvent;
let chord; 
let probability;
let event2; 

function setup() {
  createCanvas(600, 600);
  synth = make_poly().instrument;

  // many similarites to Tone.Transport.schedule
  // but some added features
  chord = new Tone.ToneEvent(
    (time, chord) => {
      console.log(chord, time)
      synth.triggerAttackRelease(chord, "1m", time);
    },
    ["D4", "E4", "F4"]
  );
  // loop every measure for 8 measures
  // true or number of times looped
  chord.loop = 8;
  chord.loopEnd = '1m'
  chord.playbackRate = 1
  chord.probability = 0.5;
  
  event2 = new Tone.ToneEvent(
    (time, chord) => {
      console.log(chord, time)
      synth.triggerAttackRelease(chord, "1m", time);
    },
    ["C3", "B4", "A4"]
  );
  
  event2.loop = 4;
  event2.loopEnd = '2m'
  event2.playbackRate = 1.1
  event2.probability = 0.5;
  
  startEvent = createButton("start Event")
    .position(150, 60)
    .mousePressed(() => chord.start());

  stopEvent = createButton("stop Event")
    .position(150, 80)
    .mousePressed(() => chord.stop());
 startEvent2 = createButton("start Event")
    .position(150+100, 60)
    .mousePressed(() => event2.start());

  stopEvent2 = createButton("stop Event")
    .position(150+100, 80)
    .mousePressed(() => event2.stop());
  // probability
  probability = createSlider(0, 1, 1, 0)
    .position(150, 100)
  
  start = createButton("start transport");
  start.position(20, 60);
  start.mousePressed(() => Tone.Transport.start());

  pause = createButton("pause transport");
  pause.position(20, 80);
  pause.mousePressed(() => Tone.Transport.pause());

  stop = createButton("stop transport");
  stop.position(20, 100);
  stop.mousePressed(() => Tone.Transport.stop());

  slider = createSlider(40, 200, 120, 0);
  slider.position(20, 40);

  rampTransport = createButton("ramp transport bpm");
  rampTransport.position(200, 200);
  rampTransport.mousePressed(() => Tone.Transport.bpm.rampTo(200, 4));
}
function draw() {
  if(chord.state == 'stopped') {
    background(255, 0, 0)
  } else {
    background(0, 255, 0)
  }
  
  text(
    "Transport time in seconds " + Tone.Transport.seconds.toFixed(1),
    20,
    20
  );

  // bars:beats:sixteens
  text("Transport position: " + Tone.Transport.position, 200, 20);
  
  // event position
  text("Event position: " + chord.progress.toFixed(1), 200, 40)
  text("Event probability " + chord.probability.toFixed(1), 320, 40)
  // comment this line to get the ramp to work
  Tone.Transport.bpm.value = slider.value();
  text("BPM " + Tone.Transport.bpm.value.toFixed(0), 20, 40);
  chord.probability = probability.value()

  text("Transport state: " + Tone.Transport.state, 400, 20);
}

function make_poly() {
  // create synth
  var instrument = new Tone.PolySynth(Tone.FMSynth, 3);
  var synthJSON = {
    harmonicity: 5,
    modulationIndex: 10,
    oscillator: {
      type: "sine"
    },
    envelope: {
      attack: 0.001,
      decay: 2,
      sustain: 0.1,
      release: 2
    },
    modulation: {
      type: "square"
    },
    modulationEnvelope: {
      attack: 0.002,
      decay: 0.2,
      sustain: 0,
      release: 0.2
    }
  };

  instrument.set(synthJSON);

  var effect1, effect2, effect3;

  // make connections
  instrument.connect(Tone.Master);

  // define deep dispose function
  function deep_dispose() {
    if (instrument != undefined && instrument != null) {
      instrument.dispose();
      instrument = null;
    }
  }

  return {
    instrument: instrument,
    deep_dispose: deep_dispose
  };
}

              
            
!
999px

Console