-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
375 lines (334 loc) · 18.3 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"use strict";
/*
* Created with @iobroker/create-adapter v1.21.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const midi = require("easymidi");
const adapter = utils.Adapter ("midi");
let midiIn;
// Note names
const NOTE_NAMES = [ "C", "Db", "D", "Eb", "E", "F", "F#", "G", "Ab", "A", "Bb", "B" ];
function noteNameFromMidiNumber (midi) {
const note = NOTE_NAMES[midi % 12];
const octave = Math.floor(midi / 12) - 1;
return note + octave;
}
class Midi extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "midi",
});
this.on("ready", this.onReady.bind(this));
this.on("objectChange", this.onObjectChange.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
adapter.log.info("Available MIDI Input Devices: " + midi.getInputs());
adapter.log.info("config midiin: " + adapter.config.midiin);
//adapter.subscribeStates("*"); //not used for input
//establish connection to midi input device
try {
if(adapter.config.midiin != "__insert midi input device from log here __") {
midiIn = new midi.Input(adapter.config.midiin);
}
} catch(e) {
adapter.log.error(e.message);
}
//check if connection was successful
if(typeof midiIn != "undefined") {
adapter.setState("info.connection", true, true);
adapter.log.info("Connected to " + adapter.config.midiin);
// removed with version 0.0.3
// //create all states in advance if configured
// if(adapter.config.createAllObjects) {
// for(let channel = 0; channel < 16; channel++) {
// for(let note = 0; note < 128; note++) {
// adapter.setObjectNotExists("channel" + channel + ".noteoff." + noteNameFromMidiNumber(note), {
// type:"state",
// common:{name:"Channel " + channel + " noteoff " + noteNameFromMidiNumber(note), type:"number", role:"value", read:true,write:false},
// native:{}
// });
// adapter.setObjectNotExists("channel" + channel + ".noteon." + noteNameFromMidiNumber(note), {
// type:"state",
// common:{name:"Channel " + channel + " noteon " + noteNameFromMidiNumber(note), type:"number", role:"value", read:true,write:false},
// native:{}
// });
// adapter.setObjectNotExists("channel" + channel + ".note." + noteNameFromMidiNumber(note), {
// type:"state",
// common:{name:"Channel " + channel + " note " + note, type:"boolean", role:"value", read:true,write:false},
// native:{}
// });
// adapter.setObjectNotExists("channel" + channel + ".cc." + note, {
// type:"state",
// common:{name:"Channel " + channel + " cc " + note, type:"number", role:"value", read:true,write:false},
// native:{}
// });
// adapter.setObjectNotExists("channel" + channel + ".polyaftertouch." + note, {
// type:"state",
// common:{name:"Channel " + channel + " polyaftertouch " + note, type:"number", role:"value", read:true,write:false},
// native:{}
// });
// adapter.setObjectNotExists("channel" + channel + ".program." + note, {
// type:"state",
// common:{name:"Channel " + channel + " program " + note, type:"boolean", role:"value", read:true,write:false},
// native:{}
// });
// }
// }
// adapter.setObjectNotExists("position", {
// type:"state",
// common:{name:"Position", type:"number", role:"value", read:true,write:false},
// native:{}
// });
// }
//midi input handlers
midiIn.on("noteoff", function (msg) {
adapter.log.debug("midi < noteoff "+ noteNameFromMidiNumber(msg.note) +" "+ msg.velocity +" "+ msg.channel);
//set value of noteoff
adapter.getObject("channel" + msg.channel + ".noteoff." + noteNameFromMidiNumber(msg.note), function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of channel" + msg.channel + ".noteoff." + noteNameFromMidiNumber(msg.note)+": "+msg.velocity);
adapter.setStateAsync("channel" + msg.channel + ".noteoff." + noteNameFromMidiNumber(msg.note), msg.velocity, true);
}
else {
adapter.log.debug("noteoff object not existing, creating: channel" + msg.channel + ".noteoff." + noteNameFromMidiNumber(msg.note)+": "+msg.velocity);
adapter.setObjectNotExists("channel" + msg.channel + ".noteoff." + noteNameFromMidiNumber(msg.note), {
type:"state",
common:{name:"Channel " + msg.channel + " noteoff " + noteNameFromMidiNumber(msg.note), type:"number", role:"value", read:true, write:false, def: msg.velocity},
native:{}
});
}
});
//set boolean for note to false
adapter.getObject("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set boolean for note " + noteNameFromMidiNumber(msg.note)+": false");
adapter.setStateAsync("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), false, true);
}
else {
adapter.log.debug("note object not existing, creating: channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note)+": false");
adapter.setObjectNotExists("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), {
type:"state",
common:{name:"Channel " + msg.channel + " note " + noteNameFromMidiNumber(msg.note), type:"boolean", role:"value", read:true, write:false, def: false },
native:{}
});
}
});
});
midiIn.on("noteon", function (msg) {
adapter.log.debug("midi < noteon "+ noteNameFromMidiNumber(msg.note) +" "+ msg.velocity +" "+ msg.channel);
//set value of noteon
adapter.getObject("channel" + msg.channel + ".noteon." + noteNameFromMidiNumber(msg.note), function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of channel" + msg.channel + ".noteon." + noteNameFromMidiNumber(msg.note)+": "+msg.velocity);
adapter.setStateAsync("channel" + msg.channel + ".noteon." + noteNameFromMidiNumber(msg.note), msg.velocity, true);
}
else {
adapter.log.debug("noteon object not existing, creating: channel" + msg.channel + ".noteon." + noteNameFromMidiNumber(msg.note)+": "+msg.velocity);
adapter.setObjectNotExists("channel" + msg.channel + ".noteon." + noteNameFromMidiNumber(msg.note), {
type:"state",
common:{name:"Channel " + msg.channel + " noteon " + noteNameFromMidiNumber(msg.note), type:"number", role:"value", read:true, write:false, def: msg.velocity},
native:{}
});
}
});
//set boolean for note to false
adapter.getObject("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set boolean for note " + noteNameFromMidiNumber(msg.note)+": true");
adapter.setStateAsync("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), true, true);
}
else {
adapter.log.debug("note object not existing, creating: channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note)+": true");
adapter.setObjectNotExists("channel" + msg.channel + ".note." + noteNameFromMidiNumber(msg.note), {
type:"state",
common:{name:"Channel " + msg.channel + " note " + noteNameFromMidiNumber(msg.note), type:"boolean", role:"value", read:true, write:false, def: true },
native:{}
});
}
});
});
midiIn.on("poly aftertouch", function (msg) {
adapter.log.debug("midi < polyaftertouch "+ noteNameFromMidiNumber(msg.note) +" "+ msg.pressure +" "+ msg.channel);
adapter.getObject("channel" + msg.channel + ".polyaftertouch." + noteNameFromMidiNumber(msg.note), function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of channel" + msg.channel + ".polyaftertouch." + noteNameFromMidiNumber(msg.note)+": " + msg.pressure);
adapter.setStateAsync("channel" + msg.channel + ".polyaftertouch." + noteNameFromMidiNumber(msg.note), msg.pressure, true);
}
else {
adapter.log.debug("polyaftertouch object not existing, creating: channel" + msg.channel + ".polyaftertouch." + noteNameFromMidiNumber(msg.note)+": " + msg.pressure);
adapter.setObjectNotExists("channel" + msg.channel + ".polyaftertouch." + noteNameFromMidiNumber(msg.note), {
type:"state",
common:{name:"Channel " + msg.channel + " polyaftertouch " + noteNameFromMidiNumber(msg.note), type:"number", role:"value", read:true, write:false, def: msg.pressure},
native:{}
});
}
});
});
midiIn.on("cc", function (msg) {
adapter.log.debug("midi < cc "+ msg.controller +" "+ msg.value +" "+ msg.channel);
adapter.getObject("channel" + msg.channel + ".cc." + msg.controller, function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of channel" + msg.channel + ".cc." + msg.controller+": " + msg.value);
adapter.setStateAsync("channel" + msg.channel + ".cc." + msg.controller, msg.value, true);
}
else {
adapter.log.debug("cc object not existing, creating: channel" + msg.channel + ".cc." + msg.controller+": " + msg.value);
adapter.setObjectNotExists("channel" + msg.channel + ".cc." + msg.controller, {
type:"state",
common:{name:"Channel " + msg.channel + " cc " + msg.controller, type:"number", role:"value", read:true, write:false, def: msg.value},
native:{}
});
}
});
});
midiIn.on("program", function (msg) {
adapter.log.debug("midi < program "+ msg.number +" "+ msg.channel);
adapter.getObject("channel" + msg.channel + ".program." + msg.number, function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of channel" + msg.channel + ".program." + msg.number+": true");
adapter.setStateAsync("channel" + msg.channel + ".program." + msg.number, true, true);
}
else {
adapter.log.debug("program object not existing, creating: channel" + msg.channel + ".program." + msg.number +": true");
adapter.setObjectNotExists("channel" + msg.channel + ".program." + msg.number, {
type:"state",
common:{name:"Channel " + msg.channel + " program " + msg.number, type:"boolean", role:"value", read:true, write:false, def: true},
native:{}
});
}
});
});
midiIn.on("position", function (msg) {
adapter.log.debug("midi < position "+ msg.value);
adapter.getObject("position", function (err, noteObj) {
if (noteObj) {
adapter.log.debug("set value of position: " + msg.value);
adapter.setStateAsync("position", msg.value, true);
}
else {
adapter.log.debug("position object not existing, creating: position: " + msg.value);
adapter.setObjectNotExists("position", {
type:"state",
common:{name:"Position", type:"number", role:"value", read:true, write:false, def: msg.value},
native:{}
});
}
});
});
//ToDo: implement states
midiIn.on("channel aftertouch", function (msg) {
adapter.log.debug("midi < channel aftertouch "+ msg.pressure +" "+ msg.channel);
});
midiIn.on("pitch", function (msg) {
adapter.log.debug("midi < pitch "+ msg.value +" "+ msg.channel);
});
midiIn.on("select", function (msg) {
adapter.log.debug("midi < select"+ msg.song);
});
midiIn.on("clock", function () {
// adapter.log.debug("midi < clock");
});
midiIn.on("start", function () {
adapter.log.debug("midi < start");
});
midiIn.on("continue", function () {
adapter.log.debug("midi < continue");
});
midiIn.on("stop", function () {
adapter.log.debug("midi < stop");
});
midiIn.on("reset", function () {
adapter.log.debug("midi < reset");
});
} else { // connection failed
adapter.setStateAsync("info.connection", false, true);
if(adapter.config.midiin != "__insert midi input device from log here __" && adapter.config.midiin != "") {
adapter.log.error("Connection failed");
} else {
adapter.log.info("Please configure midi input device in settings");
}
}
this.subscribeStates("*");
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
midiIn.close(); //close midi input connection
adapter.setStateAsync("info.connection", false, true);
this.log.info("cleaned everything up...");
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed object changes
* @param {string} id
* @param {ioBroker.Object | null | undefined} obj
*/
onObjectChange(id, obj) {
if (obj) {
// The object was changed
this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`);
} else {
// The object was deleted
this.log.info(`object ${id} deleted`);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
// The state was deleted
this.log.info(`state ${id} deleted`);
}
}
/**
* Some message was sent to this instance over message box. Used by email, pushover, text2speech, ...
* Using this method requires "common.message" property to be set to true in io-package.json
* @param {ioBroker.Message} obj
*/
// onMessage(obj) {
// this.log.debug("Message received: " + obj.message);
// if (typeof obj === "object" && obj.message) {
// if (obj.command === "listMidiInputDevices") {
// const inputs = midi.getInputs();
// this.log.info("List of Midi Inputs: " + inputs);
// // Send response in callback
// if (obj.callback) this.sendTo(obj.from, obj.command, inputs, obj.callback);
// }
// }
// }
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Midi(options);
} else {
// otherwise start the instance directly
new Midi();
}