Skip to content

Midi

Created: 2019-05-26 17:59:29 -0700 Modified: 2019-05-26 19:21:32 -0700

This code opens the first MIDI input connected to the device, then prints every message received:

const midi = require("midi");
const input = new midi.input();
const portNumberToGet = 0;
const numPorts = input.getPortCount();
console.log("numPorts: " + JSON.stringify(numPorts));
const portName = input.getPortName(portNumberToGet);
console.log("portName: " + JSON.stringify(portName));
const MIDI_CC_CHANNEL_0 = 176; // Got this from here: https://www.midi.org/specifications/item/table-1-summary-of-midi-message
const MIDI_SUSTAIN_PEDAL = 64; // Got this just by pressing the pedal and seeing what happened
input.on("message", function(deltaTime, message) {
const [status, data1, data2] = message;
// ↑ do stuff here with those
});
input.openPort(portNumberToGet);
input.ignoreTypes(true, true, true);
// Just so that the program doesn't exit immediately
setTimeout(() => {}, 1e9);

Sending keystrokes when taking in MIDI data

Section titled Sending keystrokes when taking in MIDI data

I use RobotJS, but on Windows, keyToggle has this issue where it won’t actually hold keys down, and the solution in the issue didn’t work for me:

  1. (remember: these instructions don’t work)
  2. cd node_modules/robotjs
  3. Find keypress.c
  4. Comment out this code around line 100
if ( flags & KEYEVENTF_KEYUP ) {
scan |= 0x80;
}
  1. yarn —dev
  2. Don’t bother doing “yarn run prebuild”; it doesn’t succeed.
  3. node-gyp rebuild

According to Raymond Chen’s SO comment, you just need to emulate key toggles by repeatedly sending the key.

https://www.midi.org/specifications/item/table-1-summary-of-midi-message