forked from TheOriginalAndrobot/midi2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·190 lines (150 loc) · 4.74 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env node
//
// MIDI to MQTT bridge
//
// Author: Andy Swing
//
var pkg = require('./package.json');
var config = require('./config.js');
var log = require('yalm');
var Mqtt = require('mqtt');
var Midi = require('easymidi');
log.setLevel(config.v);
log.info(pkg.name + ' ' + pkg.version + ' starting');
var mqttConnected;
//
// MQTT
//
log.info('mqtt trying to connect to', config.url);
var mqtt = Mqtt.connect(config.url, {will: {topic: config.topic + '/connected', payload: '0'}});
// Shotcut for publishing to MQTT and logging it
function pubMQTT(topic, payload){
var fullTopic = config.topic + '/' + topic;
log.debug('mqtt >', fullTopic, payload);
mqtt.publish(fullTopic, payload);
}
mqtt.on('connect', function () {
mqttConnected = true;
var sub = '';
if (config.subscribe == 'midiin') {
sub = '/in/+/+/+';
} else if (config.subscribe == 'midiout') {
sub = '/out/+/+/+';
}
log.info('mqtt connected ' + config.url);
mqtt.publish(config.topic + '/connected', '1');
log.info('mqtt subscribe', config.topic + sub);
mqtt.subscribe(config.topic + sub);
});
mqtt.on('close', function () {
if (mqttConnected) {
mqttConnected = false;
log.info('mqtt closed ' + config.url);
}
});
mqtt.on('error', function (error) {
log.error('mqtt error ' + error);
});
mqtt.on('message', function (topic, payload) {
payload = payload.toString();
log.debug('mqtt <', topic, payload);
var parts = topic.split('/');
var channel = parseInt(parts[parts.length-3]);
var type = parts[parts.length-2].toString();
var param = parseInt(parts[parts.length-1]);
var value = parseInt(payload);
log.debug('midi > channel:', channel, 'type:', type, 'parameter:', param, 'value:', value);
switch (type) {
case 'noteon':
case 'noteoff':
var data = Object();
data['note'] = param;
data['velocity'] = value;
data['channel'] = channel;
midiOut.send(type, data);
break;
case 'cc':
var data = Object();
data['controller'] = param;
data['value'] = value;
data['channel'] = channel;
midiOut.send(type, data);
break;
case 'poly_aftertouch':
var data = Object();
data['note'] = param;
data['velocity'] = value;
data['channel'] = channel;
midiOut.send('poly aftertouch', data);
break;
default:
log.error('Unsupported command \'' + type + '\' recieved via MQTT');
break;
}
});
//
// Midi connections
//
log.info('Available MIDI inputs: ', Midi.getInputs());
log.info('Available MIDI outputs: ', Midi.getOutputs());
var midiIn = new Midi.Input(config.inputMidiPort);
var midiOut = new Midi.Output(config.outputMidiPort);
midiIn.on('noteoff', function (msg) {
log.debug('midi < noteoff', msg.note, msg.velocity, msg.channel);
pubMQTT('out/' + msg.channel + '/noteoff/' + msg.note, msg.velocity.toString());
});
midiIn.on('noteon', function (msg) {
log.debug('midi < noteon', msg.note, msg.velocity, msg.channel);
pubMQTT('out/' + msg.channel + '/noteon/' + msg.note, msg.velocity.toString());
});
midiIn.on('poly aftertouch', function (msg) {
log.debug('midi < poly aftertouch', msg.note, msg.pressure, msg.channel);
pubMQTT('out/' + msg.channel + '/poly_aftertouch/' + msg.note, msg.pressure.toString());
});
midiIn.on('cc', function (msg) {
log.debug('midi < cc', msg.controller, msg.value, msg.channel);
pubMQTT('out/' + msg.channel + '/cc/' + msg.controller, msg.value.toString());
});
midiIn.on('program', function (msg) {
log.debug('midi < program', msg.number, msg.channel);
});
midiIn.on('channel aftertouch', function (msg) {
log.debug('midi < channel aftertouch', msg.pressure, msg.channel);
});
midiIn.on('pitch', function (msg) {
log.debug('midi < pitch', msg.value, msg.channel);
});
midiIn.on('position', function (msg) {
log.debug('midi < position', msg.value);
});
midiIn.on('select', function (msg) {
log.debug('midi < select', msg.song);
});
midiIn.on('clock', function () {
//log.debug('midi < clock');
});
midiIn.on('start', function () {
log.debug('midi < start');
});
midiIn.on('continue', function () {
log.debug('midi < continue');
});
midiIn.on('stop', function () {
log.debug('midi < stop');
});
midiIn.on('reset', function () {
log.debug('midi < reset');
});
//
// Catch ctrl-c to exit program
//
process.on('SIGINT', function() {
console.log("\nGracefully shutting down from SIGINT (Ctrl+C)");
midiIn.close();
midiOut.close();
mqtt.publish(config.topic + '/connected', '0');
mqtt.end(function() {
console.log("Exiting...");
process.exit();
});
});