forked from Sian-Lee-SA/StudioOne-LaunchKey-Mini-MK3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchKeyMKIIIMidiDevice.js
276 lines (233 loc) · 7.12 KB
/
LaunchKeyMKIIIMidiDevice.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
/**
* @Author: Sian Croser <Sian-Lee-SA>
* @Date: 2020-05-27T21:56:33+09:30
* @Email: [email protected]
* @Filename: LaunchKeyMKIIIMidiDevice.js
* @Last modified by: Sian Croser <Sian-Lee-SA>
* @Last modified time: 2020-05-28T11:05:58+09:30
* @License: GPL-3
*/
// include SDK files from host
include_file("resource://com.presonus.musicdevices/sdk/midiprotocol.js");
include_file("resource://com.presonus.musicdevices/sdk/controlsurfacedevice.js");
include_file("Debug.js");
include_file("Color.js");
ColorLEDHandler.prototype = new PreSonus.ControlHandler ();
function ColorLEDHandler (name, status, address)
{
this.name = name;
this.status = status;
this.address = address;
this.effect = 0;
this.state = 1;
this.color;
this.value = 0x00;
this.setState = function( _state )
{
this.state = _state;
this.update();
}
this.setEffect = function( _effect )
{
this.sendMidi( this.status, this.address, 0x00 );
this.effect = _effect;
this.update();
}
this.sendValue = function( _value, _flags )
{
this.color = new Color( _value );
this.value = this.color.midi;
this.update();
}
this.update = function()
{
let midi = ( this.state ) ? this.value : 0x00;
this.sendMidi( this.status|this.effect, this.address, midi );
}
}
ColorEffectHandler.prototype = new PreSonus.ControlHandler ();
function ColorEffectHandler(name, handler)
{
this.name = name;
this.handler = handler;
this.sendValue = function ( _value, _flags )
{
this.handler.setEffect( _value );
}
}
ColorStateHandler.prototype = new PreSonus.ControlHandler();
function ColorStateHandler( name, handler )
{
this.name = name;
this.handler = handler;
this.handler.setState(0);
this.sendValue = function( value, flags )
{
this.handler.setState( value );
}
}
MonoLEDHandler.prototype = new PreSonus.ControlHandler ();
function MonoLEDHandler (name, address)
{
this.name = name;
this.address = address;
this.sendValue = function ( _value, _flags )
{
this.value = _value;
this.sendMidi( 0xBF, this.address, this.value );
}
}
ButtonHandler.prototype = new PreSonus.ControlHandler ();
function ButtonHandler(name, status, address)
{
this.name = name;
this.status = status;
this.address = address;
}
ButtonHoldHandler.prototype = new PreSonus.ControlHandler ();
function ButtonHoldHandler(name, status, address)
{
this.name = name;
this.status = status;
this.address = address;
this.altControl = null;
this.timeout = 500;
this.activeTime = 0;
this.isPressed = false;
this.isHeld = false;
this.onIdle = function(time)
{
if( ! this.isPressed || this.isHeld )
return;
if( ! this.activeTime )
this.activeTime = time;
if( time > this.activeTime + this.timeout )
{
this.updateValue(1);
this.isHeld = true;
}
}
this.bindControlHandler = function( control )
{
this.altControl = control;
}
this.reset = function()
{
this.isPressed = false;
this.isHeld = false;
this.activeTime = 0;
}
this.receiveMidi = function( status, address, value )
{
if( status != this.status || address != this.address )
return false;
// If the button press is release before reaching the timeout
// then return false to allow another handler to handle the midi event
if( ! value && ! this.isHeld )
{
this.altControl.updateValue(1);
this.altControl.updateValue(0);
this.reset();
return true;
}
// Button value of 0 will be a release
if( ! value )
{
if( this.isHeld )
this.updateValue(0);
else
this.altControl.updateValue(0);
this.reset();
} else {
this.isPressed = true;
}
return true;
}
}
LaunchKeyMK3ExtendedMidiDevice.prototype = new PreSonus.ControlSurfaceDevice ();
function LaunchKeyMK3ExtendedMidiDevice()
{
this.handlers = {};
this.idleListeners = [];
this.enableInControlMode = function( bool )
{
this.sendMidi(0x9F, 0x0C, (bool) ? 0x7F : 0x00);
}
this.onInit = function (hostDevice)
{
PreSonus.ControlSurfaceDevice.prototype.onInit.call (this, hostDevice);
}
this.createHandler = function (name, attributes)
{
function getAttr( name )
{
let attr = attributes.getAttribute(name);
if( ! attr )
return null;
if( typeof attr == 'string' )
return parseInt( attr.replace('#', '0x') );
return attr;
};
let handler = null;
switch( attributes.getAttribute("class") )
{
case "ColorLEDHandler":
handler = new ColorLEDHandler( name, getAttr('status'), getAttr('address') );
break;
case "ColorEffectHandler":
handler = new ColorEffectHandler( name, this.handlers[name.replace('Effect','LED')] );
break;
case "ColorStateHandler":
handler = new ColorStateHandler( name, this.handlers[name.replace('State','LED')] );
break;
case "MonoLEDHandler":
handler = new MonoLEDHandler( name, getAttr('address') );
break;
case "ButtonHoldHandler":
handler = new ButtonHoldHandler(name, getAttr('status'), getAttr('address'));
this.idleListeners.push(handler);
this.addReceiveHandler(handler);
break;
case "ButtonHandler":
handler = new ButtonHandler(name, getAttr('status'), getAttr('address'));
let bind = attributes.getAttribute('bind');
this.handlers[bind].bindControlHandler(handler);
break;
}
if(!handler)
return false;
this.handlers[name] = handler;
this.addHandler (handler);
return true;
}
this.onIdle = function (time)
{
for( let i = 0; i < this.idleListeners.length; i++ )
this.idleListeners[i].onIdle(time);
}
this.onMidiOutConnected = function (state)
{
PreSonus.ControlSurfaceDevice.prototype.onMidiOutConnected.call (this, state);
if(state)
{
this.log("Starting LaunchKey MK3 Extended");
// Reset Pads
this.enableInControlMode( false );
this.enableInControlMode( true );
this.sendMidi(0xBF, 0x03, 0x01);
this.hostDevice.invalidateAll ();
}
}
this.onExit = function ()
{
// Transmit native mode off message
this.sendMidi(0xBF, 0x03, 0x01);
this.enableInControlMode( false );
PreSonus.ControlSurfaceDevice.prototype.onExit.call (this);
}
}
// factory entry called by host
function createLaunchKeyMK3ExtendedDeviceInstance ()
{
return new LaunchKeyMK3ExtendedMidiDevice;
}