forked from Sian-Lee-SA/StudioOne-LaunchKey-Mini-MK3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchKeyMKIIIMidiDevice_basic.js
217 lines (173 loc) · 5.4 KB
/
LaunchKeyMKIIIMidiDevice_basic.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
/**
* @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");
TouchModHandler.prototype = new PreSonus.ControlHandler();
function TouchModHandler(name, channel)
{
this.name = name;
this.status = 0xB0;
this.channel = channel - 1;
this.address = 1;
this.lastValue = 0;
this.counter = 0;
this.receiveMidi = function(status, address, value)
{
if( status != (this.status|this.channel) || address != this.address )
return false;
this.counter += ( value > this.lastValue ) ? 1 : -1;
this.lastValue = value;
if( Math.abs(this.counter) < 10 )
return true;
// Divide by 10 as counter could be positive or negative
// Giving a result of 1 or -1
this.updateValue(this.counter / 10);
this.counter = 0;
return true;
}
};
TouchPitchHandler.prototype = new PreSonus.ControlHandler();
function TouchPitchHandler(name, channel)
{
this.name = name;
this.status = 0xE0;
this.channel = channel - 1;
this.lastValue = [0, 0, 0];
this.counter = 0;
this.receiveMidi = function(status, address, value)
{
if( status != (this.status|this.channel) )
return false;
let combined_value = (value << 7)|address;
// We assume the pitch whell has been released as the resolution
// stays the same when returning
if( address == this.lastValue[0] )
{
this.counter = 0;
return true;
}
let dir = ( combined_value > this.lastValue[1] ) ? 1 : -1;
if( this.lastValue[2] != dir )
this.counter = 0;
else
this.counter += dir;
this.lastValue = [address, combined_value, dir];
if( Math.abs(this.counter) < 8 )
return true;
// Divide by 10 as counter could be positive or negative
// Giving a result of 1 or -1
this.updateValue(this.counter / 8);
this.counter = 0;
return true;
}
};
TouchDoubleTapHandler.prototype = new PreSonus.ControlHandler();
function TouchDoubleTapHandler(name, channel)
{
this.name = name;
this.status = 0xE0;
this.channel = channel - 1;
this.lastAddressValue = 0x00;
this.touchStartTime = 0;
this.touchEndTime = 0;
this.tapCount = 0;
this.time = 0;
this.onIdle = function(time)
{
this.time = time;
}
this.receiveMidi = function(status, address, value)
{
if( status != (this.status|this.channel) )
return false;
if( this.lastAddressValue == 0x00 )
{
if( this.time - this.touchEndTime > 250 )
this.tapCount = 0;
this.touchStartTime = this.time;
}
if( address == 0x00 )
{
this.touchEndTime = this.time;
if( this.touchEndTime - this.touchStartTime < 250 )
this.tapCount++;
else
this.tapCount = 0;
}
this.lastAddressValue = address;
if( this.tapCount >= 2 )
{
this.updateValue(1);
this.tapCount = 0;
return true;
}
return false;
}
}
LaunchKeyMK3BasicDevice.prototype = new PreSonus.ControlSurfaceDevice ();
function LaunchKeyMK3BasicDevice()
{
this.handlers = {};
this.idleListeners = [];
this.onInit = function(hostDevice)
{
PreSonus.ControlSurfaceDevice.prototype.onInit.call (this, hostDevice);
}
this.createHandler = function (name, attributes)
{
// additional handlers created on the fly via <Handler> in XML
let className = attributes.getAttribute("class");
let handler = null;
let ch = parseInt( attributes.getAttribute("channel") );
switch( className )
{
case "TouchModHandler":
handler = new TouchModHandler(name, ch);
break;
case "TouchPitchHandler":
handler = new TouchPitchHandler(name, ch);
break;
case "TouchDoubleTapHandler":
handler = new TouchDoubleTapHandler(name, ch);
this.idleListeners.push(handler);
break;
}
if(!handler)
return false;
this.handlers[name] = handler;
this.addReceiveHandler(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 Basic");
this.hostDevice.invalidateAll ();
}
}
this.onExit = function ()
{
PreSonus.ControlSurfaceDevice.prototype.onExit.call (this);
}
}
// factory entry called by host
function createLaunchKeyMK3BasicDeviceInstance ()
{
return new LaunchKeyMK3BasicDevice;
}