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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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://unpkg.com/unmute" data-add-button="true"></script> -->
<!--     <script src="https://rawgit.com/nexus-js/ui/master/dist/NexusUI.js"></script> -->
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta charset="utf-8" />

  </head>
  <body>

   <div id="target"></div>
    <script src="sketch.js"></script>
  </body>
</html>

              
            
!

CSS

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

JS

              
                /* 
  Scheduling values
  pay special attention to starttime vs endtime in docs
  https://tonejs.github.io/docs/r13/Signal
*/

var ampEnvelope;
var noise;
let setValueAtTime;
let linearRampToValueAtTime;
let exponentialRampToValueAtTime;
let linearRampTo; 
let exponentialRampTo;
let rampTo;

function setup() {
  createCanvas(400, 200);

  ampEnvelope = new Tone.AmplitudeEnvelope({
    attack: 0.1,
    decay: 0.2,
    sustain: 1.0,
    release: 0.8
  }).toDestination();

  // lets put a filter between the noise and envelope
  //   hi pass filter - lets the highs pass through
  filter = new Tone.Filter({
    type: "highpass"
  }).connect(ampEnvelope);

  //   see: https://tonejs.github.io/docs/r13/Noise
  noise = new Tone.Noise().connect(filter).start();

  filter.frequency.value = 200;
  noise.type = "pink"; // brown, white, pink

  //   setValueAtTime
  setValueAtTime = createButton("setValueAtTime");
  setValueAtTime.position(width / 2, height / 2);
  setValueAtTime.mousePressed(() => {
    Tone.start()
    // play for 3 seconds
    ampEnvelope.triggerAttackRelease("3");
    // volume is set in decibles

    // turn down to -10 1 second after mousePressed
    noise.volume.setValueAtTime(-10, "+1");
    // -20 after 2 seconds
    noise.volume.setValueAtTime(-20, "+2");
  });

  // Schedules a linear continuous change in parameter value from the previous 
  //scheduled parameter value to the given value
  // useful if you know that you want multiple things to end at the same time
  linearRampToValueAtTime = createButton("linerRampToValueAtTime");
  linearRampToValueAtTime.position(width / 2, height / 2 + 30 * 1);
  linearRampToValueAtTime.mousePressed(() => {
    // play for 3 seconds
    ampEnvelope.triggerAttackRelease("3");
    // volume is set in decibles

    //  there is no start time, start time is 'now'
    //  it only makes sense to use one of these per trigger
    //  because they would all start at the same time
    // -20 after 2 seconds
    noise.volume.linearRampToValueAtTime(-30, "+2");
    filter.frequency.linearRampToValueAtTime(1000, "+2")
    // reset
    noise.volume.setValueAtTime(-6, "+4")
  });

  // a little smoother becuase of exponential curve
  // a much sharper dropoff initially 
  exponentialRampToValueAtTime = createButton("exponentialRampToValueAtTime");
  exponentialRampToValueAtTime.position(width / 2, height / 2 + 30 * 2);
  exponentialRampToValueAtTime.mousePressed(() => {
    // play for 3 seconds
    ampEnvelope.triggerAttackRelease("3");
    // volume is set in decibles

    // -20 after 2 seconds
    noise.volume.exponentialRampToValueAtTime(-30, "+2");
    filter.frequency.exponentialRampToValueAtTime(500, "+2");
    // reset
    noise.volume.setValueAtTime(-6, "+4")
  });

  // Sets an array of arbitrary parameter values starting 
  // at the given time for the given duration.
  setValueCurveAtTime = createButton("setValueCurveAtTime");
  setValueCurveAtTime.position(width / 2, height / 2 + 30 * 4);
  setValueCurveAtTime.mousePressed(() => {
    ampEnvelope.triggerAttackRelease(6);

    // values in an array, startTime, duration
    noise.volume.setValueCurveAtTime([-20, -10, -6, -30, -3], Tone.now(), 6);
  });

  //   ramping values
  // setting durations instead of endTimes
  linearRampTo = createButton("linearRampTo");
  linearRampTo.position(width / 2, height / 2 + 30 * 6);
  linearRampTo.mousePressed(() => {
    // play for 3 seconds
    ampEnvelope.triggerAttackRelease(6);
    // volume is set in decibles

    // linearly ramp to the value 4 over 3 seconds.
    // starting now
    noise.volume.linearRampTo(-20, 3);
    // the difference is the ability to choose start time
    // also duration means you don't need to know exactly when
    // it will end 
    // noise.volume.linearRampToValueAtTime(-20, "+3")

    // set back so we can retrigger
    noise.volume.setValueAtTime(-6, "+7");
  });

  exponentialRampTo = createButton("exponentialRampTo");
  exponentialRampTo.position(width / 2, height / 2 + 30 * 7);
  exponentialRampTo.mousePressed(() => {
    ampEnvelope.triggerAttackRelease(6);

    // linearly ramp to the value -20 over 3 seconds
    noise.volume.exponentialRampTo(-20, 3);

    // set back so we can retrigger
    noise.volume.setValueAtTime(-6, "+7");
  });

  // picks either expo or linear depending on signal
  // probaly does exp for audio signals and linear for control
  // if you're not sure just use this one
  rampTo = createButton("rampTo");
  rampTo.position(width / 2, height / 2 + 30 * 8);
  rampTo.mousePressed(() => {
    // play for 3 seconds
    ampEnvelope.triggerAttackRelease(6);

    // linearly ramp to the value 4 over 3 seconds.
    noise.volume.rampTo(-30, 3);
    filter.frequency.rampTo(1500, 2)

    // set back so we can retrigger
    noise.volume.setValueAtTime(-6, "+7");
  });
}

function draw() {
  background(255);
  textSize(30);

  text("noise volume: " + noise.volume.value.toFixed(2), 80, 30);
}

              
            
!
999px

Console