-
Notifications
You must be signed in to change notification settings - Fork 0
/
iobroker-script-midi2mqtt.js
179 lines (140 loc) · 4.35 KB
/
iobroker-script-midi2mqtt.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
//
// MIDI to MQTT bridge
//
// Original-Author: Andy Swing
// Adapted for iobroker by Boris Werner
var Mqtt = require('mqtt');
var Midi = require('easymidi');
var MQTT_BROKER = 'mqtt://192.168.178.54';
var MIDI_DEVICE = 'Numark ORBIT:Numark ORBIT MIDI 1 20:0';
var mqttConnected;
//
// MQTT
//
log('mqtt trying to connect to'+ MQTT_BROKER);
var mqtt = Mqtt.connect(MQTT_BROKER, {will: {topic: 'midi' + '/connected', payload: '0'}});
// Shotcut for publishing to MQTT and logging it
function pubMQTT(topic, payload){
var fullTopic = 'midi' + '/' + topic;
log('mqtt >'+ fullTopic+ payload);
mqtt.publish(fullTopic, payload);
}
mqtt.on('connect', function () {
mqttConnected = true;
log('mqtt connected ' + MQTT_BROKER);
mqtt.publish('midi' + '/connected', '1');
log('mqtt subscribe'+ 'midi' + '/in/+/+/+');
mqtt.subscribe('midi' + '/in/+/+/+');
});
mqtt.on('close', function () {
if (mqttConnected) {
mqttConnected = false;
log('mqtt closed ' + MQTT_BROKER);
}
});
mqtt.on('error', function (error) {
log('mqtt error ' + error);
});
mqtt.on('message', function (topic, payload) {
payload = payload.toString();
log('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('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('Unsupported command \'' + type + '\' recieved via MQTT');
break;
}
});
//
// Midi connections
//
log('Available MIDI inputs: '+ Midi.getInputs());
log('Available MIDI outputs: '+ Midi.getOutputs());
var midiIn = new Midi.Input(MIDI_DEVICE);
var midiOut = new Midi.Output(MIDI_DEVICE);
midiIn.on('noteoff', function (msg) {
log('midi < noteoff'+ msg.note+ msg.velocity+ msg.channel);
pubMQTT('out/' + msg.channel + '/noteoff/' + msg.note, msg.velocity.toString());
});
midiIn.on('noteon', function (msg) {
log('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('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('midi < cc'+ msg.controller+ msg.value+ msg.channel);
pubMQTT('out/' + msg.channel + '/cc/' + msg.controller, msg.value.toString());
});
midiIn.on('program', function (msg) {
log('midi < program'+ msg.number+ msg.channel);
});
midiIn.on('channel aftertouch', function (msg) {
log('midi < channel aftertouch'+ msg.pressure+ msg.channel);
});
midiIn.on('pitch', function (msg) {
log('midi < pitch'+ msg.value+ msg.channel);
});
midiIn.on('position', function (msg) {
log('midi < position'+ msg.value);
});
midiIn.on('select', function (msg) {
log('midi < select'+ msg.song);
});
midiIn.on('clock', function () {
//log('midi < clock');
});
midiIn.on('start', function () {
log('midi < start');
});
midiIn.on('continue', function () {
log('midi < continue');
});
midiIn.on('stop', function () {
log('midi < stop');
});
midiIn.on('reset', function () {
log('midi < reset');
});
//
// Catch ctrl-c to exit program
//
onStop(function (callback) {
log("\nGracefully shutting down from SIGINT (Ctrl+C)");
midiIn.close();
midiOut.close();
mqtt.publish('midi' + '/connected', '0');
mqtt.end(function() {
log("Exiting...");
callback();
}, 2000 /*ms*/);
});