Skip to content

Connecting a MIDI keyboard #8

@mobyvb

Description

@mobyvb

There was this cool project called Sky Tunes at HackGT that worked natively with a MIDI keyboard. I'm looking through their code here to see how we can do the same. It should work with Chrome and Firefox at least with no plugins.

Here are the relevant portions of their code:

var midiAccess;
if (navigator.requestMIDIAccess) {
  navigator.requestMIDIAccess().then( onMIDIInit, function() {});
}

function onMIDIInit(midi) {
  midiAccess = midi;
  hookUpMIDIInput();
  midiAccess.onstatechange = hookUpMIDIInput;
}

function hookUpMIDIInput() {
  var inputs = midiAccess.inputs.values();
  midiAccess.inputs.forEach(function(entry) {
    entry.onmidimessage = MIDIMessageEventHandler;
  });
}

function MIDIMessageEventHandler(event) {
  switch (event.data[0] & 0xf0) {
    case 0x90:
      if (event.data[2] != 0) {
        noteOn(event.data[1], event.data[2]);
        return;
      }
    case 0x80:
      noteOff(event.data[1]);
      return;
  }
}

noteOn and noteOff are both functions in the MIDI.js library, which we are not currently using. We should do some research on our own, but it looks like by this implementation, event.data[0] contains information about the type of MIDI event (note on or note off in this case), event.data[1] contains the channel for that MIDI event, and event.data[2] contains the actual note.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions