Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                console.clear();

class MIDIAccess {
  constructor(args = {}) {
    this.onDeviceInput = args.onDeviceInput || console.log;
  }

  start() {
    return new Promise((resolve, reject) => {
      this._requestAccess().then(access => {
        this.initialize(access);
        resolve();
      }).catch(() => reject('Something went wrong.'));
    });
  }

  initialize(access) {
    const devices = access.inputs.values();
    for (let device of devices) this.initializeDevice(device);
  }

  initializeDevice(device) {
    device.onmidimessage = this.onMessage.bind(this);
  }
  
  onMessage(message) {
    let [_, input, value] = message.data;
    this.onDeviceInput({ input, value });
  }

  _requestAccess() {
    return new Promise((resolve, reject) => {
      if (navigator.requestMIDIAccess)
        navigator.requestMIDIAccess()
          .then(resolve)
          .catch(reject);
      else reject();
    });
  }
}

class Instrument {
  constructor() {
    this.synth = new Tone.PolySynth(3, Tone.FMSynth);

    this.filter = new Tone.Filter();
    this.volume = new Tone.Gain();

    this.synth.connect(this.filter);
    this.filter.connect(this.volume);
    this.volume.toMaster();
    
    this.filter.frequency.value = 200; // 200 - 15000
    this.volume.gain.value = 0.8; // 0-0.8
  }

  toggleSound(value) {
    let method = value === 127 ? 'triggerAttack' : 'releaseAll';
    this.synth[method](['C4', 'E4', 'G4']);
  }

  handleVolume(value) { // 0-127
    let val = value / 127 * 0.8;
    this.volume.gain.value = val;
  }

  handleFilter(value) { // 0-127
    let val = value / 127 * 14800 + 200;
    this.filter.frequency.value = val;
  }
}

// UPDATE: there is a problem in chrome with starting audio context
//  before a user gesture. This fixes it.
var started = false;
document.documentElement.addEventListener('mousedown', () => {
  if (started) return;
  started = true;
  const inst = new Instrument();
  const midi = new MIDIAccess({ onDeviceInput });
  midi.start().then(() => {
    console.log('STARTED!');
  }).catch(console.error);

  function onDeviceInput({ input, value }) {
    if (input === 23) inst.toggleSound(value);
    else if (input === 2) inst.handleVolume(value);
    else if (input === 14) inst.handleFilter(value);
    else console.log('onDeviceInput!', input, value);
  }
});

              
            
!
999px

Console