Skip to content

Instantly share code, notes, and snippets.

@murilopolese
Last active October 9, 2017 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murilopolese/127dc1b304736af2eff8d1a7ac72a44c to your computer and use it in GitHub Desktop.
Save murilopolese/127dc1b304736af2eff8d1a7ac72a44c to your computer and use it in GitHub Desktop.
Motion Sensor Arpeggiator

Virtual MIDI devices

This is a bunch of scripts that spawn virtual midi devices to interact with your favourite DAW.

/**
* Grab data from Kano's motion sensor and process it into useful midi data
*/
var easymidi = require('easymidi'),
Devices = require('kano2-shared-lib/lib/devices/devices'),
devices = new Devices({autostart:true, logLevel:'debug'}),
device;
var output = new easymidi.Output('motion-arp', true);
var note = undefined;
devices.on('device-added', function(d){
device = d;
console.log(device);
device.on('proximity-data', function(e) {
console.log(e.proximity);
note = undefined;
if (e.proximity && e.proximity != 0) {
note = 24 + parseInt(e.proximity / 15);
}
});
});
setInterval(function() {
if (!note) {
return;
}
output.send('noteon', {
note: note,
velocity: 127,
channel: 1
});
output.send('noteoff', {
note: note,
velocity: 127,
channel: 1
});
}, 250);
/**
Creates generic midi I/O devices
## Linux
1) Enable virtual midi devices: `sudo modprobe snd-virmidi snd_index=1`
1) Start and roll Jack (QjackCtl)
1) Start this script: `node index.js`
1) Open the connections on Jack
1) Connect the virtual output midi device created by this script to the "Virtual Raw Midi 1-0"
1) Connect the "Virtual Raw Midi 1-0" output to the virtual input midi device created by this script
## Macos
No setup required (only on your DAW)
## Playing around
Depending on your setup you will want to sync with the clock or with a midi track from your DAW.
To sync with a midi track you need to send the midi data to the midi input device created
by this script, process the message and send it back to the DAW through the virtual output
midi device also created by this script.
*/
var SerialPort = require('serialport');
var midi = require('midi');
var input = new midi.input();
var output = new midi.output();
var devices = [];
var note = 0;
var rangeSplit = 4;
SerialPort.list()
.then(function (ports) {
return new Promise(function (resolve, reject) {
ports.forEach(function (port) {
if (port.vendorId === '2341') {
var device = new SerialPort(port.comName, {
baudRate: 115200
});
devices.push(device);
}
});
resolve(devices);
});
})
.then(function (devices) {
return new Promise(function (resolve, reject) {
devices.forEach(function (device, index) {
device.open(function (err) {
if (err) {
reject(err);
}
});
device.on('open', function () {
console.log('port opened');
});
device.on('data', function (d) {
var data = {};
try {
data = JSON.parse(d.toString());
} catch (e) {
reject(e);
}
if (data.detail) {
var proximity = parseInt( data.detail.proximity / ( 100/rangeSplit ) );
if (proximity) {
note = proximity;
}
}
});
});
});
})
.catch(function (err) {
//console.log(err);
});
input.on('message', function(deltaTime, message) {
if (message[0] == 144) {
var m = [144, note, message[2]];
var n = [128, note, message[2]];
// console.log('sending message', m);
// Proxy the event type and velocity but not the note
output.sendMessage(m);
setTimeout(() => {
output.sendMessage(n);
}, 10);
}
});
// Create a virtual input port.
input.openVirtualPort("Virtual Input");
output.openVirtualPort("Virtual Output");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment