-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
50 lines (46 loc) · 1.28 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var MT = require('midi-test');
var src = new MT.MidiSrc('VIRTUAL MIDI-In');
var dst = new MT.MidiDst('VIRTUAL MIDI-Out');
if (!src.connect() || !dst.connect()) {
console.error('Cannot open MIDI port!');
process.exit(1);
}
console.log('Created VIRTUAL MIDI-In and VIRTUAL MIDI-Out!');
console.log('Enter MIDI message (e.g. 90 40 7f) or ^C to exit...');
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('SIGINT', function() {
console.log('Thank you!');
src.disconnect();
dst.disconnect();
rl.close();
});
rl.on('line', function(input) {
var msg = parse(input.trim());
if (msg) {
src.emit(msg);
console.log('MIDI sent:', format(msg));
}
rl.prompt();
});
rl.prompt();
dst.receive = function(msg) { console.log('MIDI received:', format(msg)); };
function format(msg) {
return msg.map(function(x) { return x < 16 ? '0' + x.toString(16) : x.toString(16); }).join(' ');
}
function parse(input) {
if (!input) return;
var arr = input.split(/\s+/);
var msg = [];
for (var i = 0; i < arr.length; i++) {
var x = parseInt(arr[i], 16);
if (isNaN(x) || x < 0 || x > 255 || arr[i].search(/[^0-9A-Fa-f]/) != -1) {
console.log('Bad MIDI value:', arr[i]);
return;
}
msg.push(x);
}
return msg;
}