forked from LykaiosNZ/companion-module-cockos-reaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
316 lines (268 loc) · 9.63 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
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
var instance_skel = require('../../instance_skel');
var OSC = require('osc');
var actions = require('./actions');
var feedbacks = require('./feedback');
var variables = require('./variables');
var presets = require('./presets');
var debug;
var log;
function instance(system, id, config) {
var self = this;
// super-constructor
instance_skel.apply(this, arguments);
Object.assign(this, {...variables, ...actions, ...presets, ...feedbacks});
self.actions(); // export actions
return self;
}
instance.prototype.updateConfig = function (config) {
var self = this;
self.config = config;
self.init_presets();
self.init_variables();
self.init_feedbacks();
self.init_osc();
};
instance.prototype.init = function () {
var self = this;
self.status(self.STATE_OK); // report status ok!
self.init_presets();
self.init_variables();
self.init_feedbacks();
self.init_osc();
debug = self.debug;
log = self.log;
if (self.config.refreshOnInit) {
debug("Sending control surface refresh action to " + self.config.host);
self.system.emit('osc_send', self.config.host, self.config.port, '/action/41743');
}
};
// Return config fields for web config
instance.prototype.config_fields = function () {
var self = this;
return [
{
type: 'text',
id: 'info',
width: 12,
label: 'Information',
value: 'This module controls <a href="https://www.reaper.fm" target="_new">REAPER</a>.'
},
{
type: 'textinput',
id: 'host',
label: 'Target IP',
width: 6,
tooltip: 'The IP of the computer running REAPER',
regex: self.REGEX_IP
},
{
type: 'textinput',
id: 'port',
label: 'Target Port',
width: 3,
tooltip: 'The port REAPER is listening to OSC on',
regex: self.REGEX_SIGNED_NUMBER
},
{
type: 'textinput',
id: 'feedbackPort',
label: 'Feedback Port',
width: 3,
tooltip: 'The port REAPER is sending OSC to',
regex: self.REGEX_SIGNED_NUMBER
},
{
type: 'checkbox',
id: 'refreshOnInit',
label: 'Refresh On Start',
tooltip: 'If enabled, a "Control surface: Refresh all surfaces" command will be sent to reaper on start.',
default: false,
}
]
};
// When module gets deleted
instance.prototype.destroy = function () {
var self = this;
debug("destory", self.id);
};
instance.prototype.init_presets = function () {
var self = this;
var presets = self.getPresets();
self.setPresetDefinitions(presets);
}
instance.prototype.actions = function (system) {
var self = this;
var actions = self.getActions();
self.setActions(actions);
}
instance.prototype.action = function (action) {
var self = this;
var cmd, args;
[cmd, args] = self.getCommand(action)
if (cmd !== undefined) {
debug('sending', cmd, args, "to", self.config.host);
self.oscSend(self.config.host, self.config.port, cmd, args);
}
};
instance.prototype.init_osc = function () {
var self = this;
self.ready = true;
if (self.listener) {
self.listener.close();
}
self.listener = new OSC.UDPPort({
localAddress: '0.0.0.0',
localPort: self.config.feedbackPort,
broadcast: true,
metadata: true
});
self.listener.open();
self.listener.on("ready", function () {
self.ready = true;
});
self.listener.on("error", function (err) {
if (err.code === "EADDRINUSE") {
self.log('error', "Error: Selected port in use." + err.message);
}
});
self.messageHandlers = self.getMessageHandlers();
self.listener.on("message", function (message) {
if (self.customMessageStatus === undefined) {
self.customMessageStatus = {}
}
var customMessageFound = false
self.customMessages.forEach(function (customMessage, index) {
if (!customMessageFound &&
customMessage.address === message.address &&
customMessage.args.type === message.args[0].type &&
customMessage.args.value === message.args[0].value
) {
self.customMessageStatus[customMessage.id] = message.args[0].value
self.setVariable('customFeedback' + index, self.customMessageStatus[customMessage.id])
self.checkFeedbacks('customMessage');
debug("Custom Feedback " + index + " is", self.customMessageStatus[customMessage.id])
customMessageFound = true;
}
});
var addressParts = message.address.split('/');
var messageType = addressParts[1];
if (self.messageHandlers[messageType] !== undefined)
{
self.messageHandlers[messageType](addressParts, message.args);
}
}
)
}
instance.prototype.init_variables = function () {
var self = this;
var variables = self.getVariables();
self.setVariableDefinitions(variables);
}
instance.prototype.init_feedbacks = function () {
var self = this
var feedbacks = self.getFeedbacks();
self.setFeedbackDefinitions(feedbacks)
}
instance.prototype.getMessageHandlers = function()
{
var self = this;
var trackHandlers = self.getTrackMessageHandlers();
return {
play: (addressParts, args) => self.handleBinaryMessage('playStatus', args, self.valueConverters.play),
stop: (addressParts, args) => self.handleBinaryMessage('stopStatus', args, self.valueConverters.stop),
record: (addressParts, args) => self.handleBinaryMessage('recordStatus', args, self.valueConverters.record),
rewind: (addressParts, args) => self.handleBinaryMessage('rewindStatus', args, self.valueConverters.rewind),
forward: (addressParts, args) => self.handleBinaryMessage('forwardStatus', args, self.valueConverters.forward),
click: (addressParts, args) => self.handleBinaryMessage('clickStatus', args, self.valueConverters.click),
repeat: (addressParts, args) => self.handleBinaryMessage('repeatStatus', args, self.valueConverters.repeat),
track: (addressParts, args) => self.handleTrackMessage(addressParts, args, trackHandlers)
}
}
instance.prototype.getTrackMessageHandlers = function() {
var self = this;
var trackFxHandlers = self.getTrackFxMessageHandlers();
return {
mute: (trackNumber, addressParts, args) => self.handleBinaryTrackMessage('mute', trackNumber, addressParts, args, self.valueConverters.trackMute),
solo: (trackNumber, addressParts, args) => self.handleBinaryTrackMessage('solo', trackNumber, addressParts, args, self.valueConverters.trackSolo),
recarm: (trackNumber, addressParts, args) => self.handleBinaryTrackMessage('recarm', trackNumber, addressParts, args, self.valueConverters.trackRecArm),
select: (trackNumber, addressParts, args) => self.handleBinaryTrackMessage('select', trackNumber, addressParts, args, self.valueConverters.trackSelect),
name: (trackNumber, addressParts, args) => {
var self = this;
self.setTrackProperty(trackNumber, 'name', args[0].value);
},
monitor: (trackNumber, addressParts, args) => self.handleBinaryTrackMessage('monitor', trackNumber, addressParts, args, self.valueConverters.trackMonitor),
fx: (trackNumber, addressParts, args) => self.handleTrackFxMessage(trackNumber, addressParts[4], addressParts, args, trackFxHandlers)
};
}
instance.prototype.getTrackFxMessageHandlers = function() {
var self = this;
return {
bypass: (trackNumber, fxNumber, addressParts, args) => self.handleBinaryTrackFxMessage('bypass', trackNumber, fxNumber, args, self.valueConverters.trackFxBypass),
name: (trackNumber, fxNumber, addressParts, args) => self.setTrackFxProperty(trackNumber, fxNumber, 'name', args[0].value),
openui: (trackNumber, fxNumber, addressParts, args) => self.handleBinaryTrackFxMessage('openui', trackNumber, fxNumber, args, self.valueConverters.trackFxOpenUi)
}
}
instance.prototype.handleBinaryMessage = function(variableName, args, valueConverter) {
var self = this;
if (args.length >= 0) {
var value = args[0].value;
if (typeof value === "number") {
var textValue = valueConverter !== undefined ? valueConverter(value) : value.toString();
self[variableName] = textValue
self.setVariable(variableName, textValue);
self.checkFeedbacks(variableName);
self.debug("message " + variableName + " value is", value)
self.debug(variableName + " is", self[variableName])
}
}
}
// TODO: Handle messages regarding selected track (has no track number)
// /track/[trackNumber]/[messageType]
instance.prototype.handleTrackMessage = function (addressParts, args, handlers) {
var trackNumber = parseInt(addressParts[2]);
var messageType = addressParts[3];
if (handlers[messageType] !== undefined)
{
handlers[messageType](trackNumber, addressParts, args);
}
}
instance.prototype.handleBinaryTrackMessage = function(propertyName, trackNumber, addressParts, args, valueConverter) {
var self = this;
// Toggles can be ignored
if (addressParts[4] === 'toggle')
{
return;
}
self.setTrackProperty(trackNumber, propertyName, args[0].value === 1, valueConverter);
self.checkFeedbacks('track_' + propertyName);
}
// /track/[trackNumber]/fx/[fxNumber]/[messageType]
instance.prototype.handleTrackFxMessage = function(trackNumber, fxNumber, addressParts, args, handlers){
var messageType = addressParts[5];
if (handlers[messageType] !== undefined)
{
handlers[messageType](trackNumber, fxNumber, addressParts, args);
}
}
instance.prototype.handleBinaryTrackFxMessage = function(propertyName, trackNumber, fxNumber, args, valueConverter)
{
var self = this;
self.setTrackFxProperty(trackNumber, fxNumber, propertyName, args[0].value === 1, valueConverter);
self.checkFeedbacks('track_fx_' + propertyName);
}
instance.GetUpgradeScripts = function() {
return [
instance_skel.CreateConvertToBooleanFeedbackUpgradeScript({
'playStatus': true,
'stopStatus': true,
'recordStatus': true,
'rewindStatus': true,
'forwardStatus': true,
'repeatStatus': true,
'clickStatus': true,
'customMessage': true,
})
]
}
instance_skel.extendedBy(instance);
exports = module.exports = instance;