-
Notifications
You must be signed in to change notification settings - Fork 0
/
knot.js
225 lines (187 loc) · 7.01 KB
/
knot.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
/***********************************************************************
(c) Copyright 2021 by Stefano Marina.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/
const Fs = require('fs');
const Filters = require('./filter.js');
const MIDI = require('midi');
const OSC = require ('osc');
const OSCParser = require ('./parser.js');
const {execSync, exec} = require("child_process");
const EventEmitter = require('events');
var exports = module.exports = {};
class Knot extends EventEmitter{
constructor(oscPort) {
super();
this.osc = oscPort;
this.parser = new OSCParser.OSCParser();
this.emitOnly = false;
}
/**
* sets "emit only" mode. In emit only mode, osc is never send and
* commands are never executed, only events are triggered. this is
* useful if you want to use Knot as a "dispatcher" rather than a
* standalone app.
* @param value sets the emit only mode
* @return the current emit only mode (before changing)
*/
setEmitOnly(value) {
this.emitOnly = value;
}
/**
* sets filterMap to null. this must be called if the old configuration
* is to be discarded entirely.
*/
clear() {this.filterMap = null;}
/**
* Parse a configuration file and creates the FilterMap object.
* @param configuration string with a path to a .json file or FilterMap object. This can also be a mixed array,
* in which case every filter is loaded.
* @param preserve (optional) (default=false) if true, the new configuration will be overimposed without
* overwriting conflicting bindings with the current filtermap.
* @param disableShell (default=false) any filterMap will disregard filters with shell commands.
* @throws error on loading, parsing or sanitizing
*/
loadConfiguration(configuration, preserve, disableShell) {
if (!Array.isArray(configuration))
configuration = [configuration];
var newConfiguration;
if (undefined === disableShell) disableShell = false;
configuration.forEach ((cfg) => {
if (typeof cfg == "string") {
try {
let data = Fs.readFileSync(cfg);
newConfiguration = JSON.parse(data);
} catch (err) {
throw `error in loading ${cfg}: ${err}`;
}
} else
newConfiguration = cfg;
if (this.filterMap !== undefined && this.filterMap !== null) {
if (preserve === undefined) preserve = false;
this.filterMap = Filters.FilterMap.merge(this.filterMap,
new Filters.FilterMap(newConfiguration, disableShell, false), preserve);
} else {
try {
this.filterMap = new Filters.FilterMap(newConfiguration, disableShell,false);
} catch (err) {
throw `error in creating filter ${configuration.indexOf(cfg)}: ${err}`;
}
}
}, this);
}
/**
* @param request can be a number, in which case it will be opened directly,
* a string,in which case it will be looked with presence (it is not required to be the
* full string), or a Input object, in which case it will be simply referenced.
*/
setMidi(request) {
if (this.midi !== undefined) {
this.midi.closePort();
this.midi = undefined;
}
this.deviceID = null;
if (typeof request != "object") {
this.midi = new MIDI.Input();
if (typeof request == "number")
this.deviceID = request;
else if (typeof request == "string") {
let ports = this.midi.getPortCount();
let rxDevice= new RegExp(request, 'gi');
let deviceName;
for (let i = 0; i < ports; i++) {
deviceName = this.midi.getPortName(i);
if (deviceName.match(rxDevice)) {
this.deviceID = i;
break;
}
}
}
if (this.deviceID == null)
throw `Cannot find device id for request ${request}`;
try {
this.midi.ignoreTypes(false, false, false);
this.midi.openPort(this.deviceID);
} catch (err) {
throw `Cannot open midi port ${deviceID}: ${err}`;
}
} else {
this.midi = request;
}
this.midi.on('message', (delta, msg) =>{
this.midiCallback(delta, msg);
});
}
getMidi() {return this.midi;}
/**
* Sets a Output for any midi message that is not intercepted.
* @param midiOut a midi output
*/
setMidiOut(midiOut) {
this.midiOut = midiOut;
}
midiCallback(delta, message) {
if (this.filterMap === undefined) {
if (this.midiOut != null) {
//console.log(`redirecting ${message}...`);
this.emit('midi', delta, message);
this.midiOut.sendMessage(message);
}
return;
}
let outcome = this.filterMap.process(message);
//console.log(`filtered ${JSON.stringify(message)} : ${JSON.stringify(outcome)}`);
if (outcome !== false && outcome !== undefined) {
let request = null;
this.emit('filter', outcome, delta, message);
for (let i = 0; i < outcome.length; i++) {
request = outcome[i];
switch (request.type) {
case "command" :
this.emit('command', request.path, delta, message);
if (!this.emitOnly) exec (request.path);
break;
case "osc":
this.emit('osc', request.path, delta, message);
if (this.osc != null && !this.emitOnly) {
try {
this.osc.send(this.parser.translate(request.path));
} catch (err) {
console.log(`bad osc message for ${request.path}: ${err}`);
}
}
break;
default : break;
}
}
} else {
this.emit('midi', delta, message);
if (this.midiOut != null)
this.midiOut.sendMessage(message);
}
}
getOSC() {return this.osc;}
}
/*
* Module exports
*/
exports.Knot = Knot;
exports.Filter = Filters.Filter;
exports.FilterMap = Filters.FilterMap;
exports.OSCParser = OSCParser.OSCParser;
exports.MIDIParser = OSCParser.MIDIParser;