-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeviceManager.cpp
341 lines (280 loc) · 10.2 KB
/
DeviceManager.cpp
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
#include "DeviceManager.h"
#include <algorithm>
#include <cmath>
#include <sstream>
#include <fstream>
#include <cabl/gfx/TextDisplay.h>
#define OSC_WRITE_STRING(buffer, ptr, str) \
std::strcpy(&buffer[ptr], str.c_str()); \
ptr += str.size() + 1; \
while (ptr & 0x03) { \
buffer[ptr++] = '\0'; \
}
namespace {
const sl::Color COLOR_ON{0xff};
const sl::Color COLOR_OFF{0x00};
const double PAD_THRESHOLD_ON = 0.03;
const double PAD_THRESHOLD_OFF = 0.01;
const double PAD_VELOCITY_MOMENTUM = 0.9;
const double PAD_VELOCITY_EXPONENT = 0.6;
const std::chrono::milliseconds PAD_NOTE_ON_DELAY(2);
const std::chrono::milliseconds PAD_NOTE_OFF_DELAY(10);
const double ENCODER_MULTIPLIER = 32.0;
const std::string OSC_PREFIX = "/MMK1/";
const std::string OSC_SHIFT_PREFIX = "/MMK1/Shift/";
const std::string OSC_MESSAGE_ON = "on";
const std::string OSC_MESSAGE_OFF = "off";
const unsigned char START_NOTE = 36;
const unsigned char MIDI_NOTE_ON = 144;
const unsigned char MIDI_NOTE_OFF = 128;
const unsigned char MIDI_NOTE_AFTERTOUCH = 160;
const unsigned char MIDI_NOTE_CC = 176;
const unsigned char CC[11] = {3, 9, 14, 15, 20, 21, 22, 23, 24, 25, 26};
}
namespace sl {
using namespace cabl;
using namespace std::placeholders;
using namespace boost::asio;
DeviceManager::DeviceManager(
DiscoveryPolicy discoveryPolicy_,
int midiPort,
std::string oscServerAddress,
int oscServerPort,
bool invertLEDs_,
bool invertShift_,
std::string secondDisplayText_
) : Client(discoveryPolicy_) {
try {
midiOut = new RtMidiOut();
}
catch(RtMidiError &error) {
error.printMessage();
exit(EXIT_FAILURE);
}
unsigned nPorts = midiOut->getPortCount();
if (nPorts == 0) {
std::cout << "No MIDI output ports available!" << std::endl;
std::cout << "On macOS, open Audio MIDI Setup.app and enable the IAC Driver." << std::endl;
exit(EXIT_FAILURE);
} else {
std::cout << "Number of available MIDI output ports: " << nPorts << std::endl;
}
if (midiPort >= nPorts) {
std::cout << "Invalid MIDI port number!" << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Opening port " << midiPort << std::endl;
midiOut->openPort(midiPort);
std::cout << "Will send OSC messages to " << oscServerAddress << ":" << oscServerPort << std::endl;
oscRemoteEndpoint = ip::udp::endpoint(ip::address::from_string(oscServerAddress), oscServerPort);
oscSocket.open(ip::udp::v4());
noteMessage.resize(3, 0);
ccMessage.resize(3, 0);
invertLEDs = invertLEDs_;
invertShift = invertShift_;
secondDisplayText = secondDisplayText_;
}
DeviceManager::~DeviceManager() {
delete midiOut;
oscSocket.close();
}
void DeviceManager::initDevice() {
for(unsigned i = static_cast<unsigned>(Device::Button::Control); i < static_cast<unsigned>(Device::Button::Unknown); i++)
{
device()->setButtonLed(static_cast<Device::Button>(i), invertLEDs ? COLOR_ON : COLOR_OFF);
}
device()->graphicDisplay(1)->black();
device()->graphicDisplay(1)->putText(10, 25, secondDisplayText.c_str(), COLOR_ON, "big");
displayText = "Initialised";
}
void DeviceManager::render() {
if (displayText == lastDisplayText) {
return;
}
device()->graphicDisplay(0)->black();
device()->graphicDisplay(0)->putText(10, 25, displayText.c_str(), COLOR_ON, "big");
lastDisplayText = displayText;
}
void DeviceManager::buttonChanged(Device::Button button_, bool buttonState_, bool shiftState_) {
std::string address("Unknown");
#define CASE_BUTTON(button) \
case Device::Button::button: \
address = #button; \
break;
switch (button_)
{
CASE_BUTTON(Control)
CASE_BUTTON(Step)
CASE_BUTTON(Browse)
CASE_BUTTON(Sampling)
CASE_BUTTON(BrowseLeft)
CASE_BUTTON(BrowseRight)
CASE_BUTTON(AutoWrite)
CASE_BUTTON(Snap)
CASE_BUTTON(DisplayButton1)
CASE_BUTTON(DisplayButton2)
CASE_BUTTON(DisplayButton3)
CASE_BUTTON(DisplayButton4)
CASE_BUTTON(DisplayButton5)
CASE_BUTTON(DisplayButton6)
CASE_BUTTON(DisplayButton7)
CASE_BUTTON(DisplayButton8)
CASE_BUTTON(Scene)
CASE_BUTTON(Pattern)
CASE_BUTTON(PadMode)
CASE_BUTTON(Navigate)
CASE_BUTTON(Duplicate)
CASE_BUTTON(Select)
CASE_BUTTON(Solo)
CASE_BUTTON(Mute)
CASE_BUTTON(GroupA)
CASE_BUTTON(GroupB)
CASE_BUTTON(GroupC)
CASE_BUTTON(GroupD)
CASE_BUTTON(GroupE)
CASE_BUTTON(GroupF)
CASE_BUTTON(GroupG)
CASE_BUTTON(GroupH)
CASE_BUTTON(Restart)
CASE_BUTTON(TransportLeft)
CASE_BUTTON(TransportRight)
CASE_BUTTON(Grid)
CASE_BUTTON(Play)
CASE_BUTTON(Rec)
CASE_BUTTON(Erase)
CASE_BUTTON(Shift)
CASE_BUTTON(NoteRepeat)
default:
break;
}
#undef CASE_BUTTON
if (shiftState_ ^ invertShift) {
if (buttonState_) {
bool state = !buttonShiftState[static_cast<unsigned>(button_)];
buttonShiftState[static_cast<unsigned>(button_)] = state;
sendOSCMessage(OSC_SHIFT_PREFIX + address, state ? OSC_MESSAGE_ON : OSC_MESSAGE_OFF);
device()->setButtonLed(button_, state ^ invertLEDs ? COLOR_ON : COLOR_OFF);
}
} else {
if (!buttonShiftState[static_cast<unsigned>(button_)]) {
sendOSCMessage(OSC_PREFIX + address, buttonState_ ? OSC_MESSAGE_ON : OSC_MESSAGE_OFF);
device()->setButtonLed(button_, buttonState_ ^ invertLEDs ? COLOR_ON : COLOR_OFF);
}
}
}
void DeviceManager::encoderChanged(unsigned encoder_, bool valueIncreased_, bool shiftPressed_) {
// do nothing
}
void DeviceManager::encoderChangedRaw(unsigned encoder_, double delta_, bool shiftPressed_) {
ccMessage[0] = MIDI_NOTE_CC;
ccMessage[1] = CC[encoder_];
if (delta_ > 0) {
ccMessage[2] = static_cast<unsigned char>(
std::max(65.0, std::min(127.0, 65.0 + delta_ * ENCODER_MULTIPLIER))
);
} else {
ccMessage[2] = static_cast<unsigned char>(
std::max(1.0, std::min(64.0, -delta_ * ENCODER_MULTIPLIER))
);
}
displayText = "CC " + std::to_string(CC[encoder_]);
requestDeviceUpdate();
midiOut->sendMessage(&ccMessage);
}
void DeviceManager::keyChanged(unsigned index_, double value_, bool shiftPressed_) {
// do nothing
}
void DeviceManager::keyUpdated(unsigned index_, double value_, bool shiftPressed_) {
PadAction action = processPadUpdate(index_, value_);
if (action == PadAction::NO_ACTION) {
return;
}
unsigned col = index_ % 4;
unsigned row = index_ / 4;
unsigned note = START_NOTE + 4 * (3 - row) + col;
double velocity = std::pow(padVelocities[index_], PAD_VELOCITY_EXPONENT) * 127.0;
if (action == PadAction::NOTE_AFTERTOUCH) {
noteMessage[0] = MIDI_NOTE_AFTERTOUCH;
device()->setKeyLed(index_, {static_cast<uint8_t>(velocity)});
} else if (action == PadAction::NOTE_ON) {
noteMessage[0] = MIDI_NOTE_ON;
device()->setKeyLed(index_, {static_cast<uint8_t>(velocity)});
displayText = "Note " + std::to_string(note);
requestDeviceUpdate();
} else if (action == PadAction::NOTE_OFF) {
noteMessage[0] = MIDI_NOTE_OFF;
device()->setKeyLed(index_, {0x00});
}
noteMessage[1] = static_cast<unsigned char>(note);
noteMessage[2] = static_cast<unsigned char>(velocity);
midiOut->sendMessage(¬eMessage);
}
PadAction DeviceManager::processPadUpdate(unsigned index_, double value_) {
PadState currentState = padStates[index_];
padVelocities[index_] = PAD_VELOCITY_MOMENTUM * padVelocities[index_] + (1.0 - PAD_VELOCITY_MOMENTUM) * value_;
if (padVelocities[index_] < PAD_THRESHOLD_OFF) {
if (currentState == PadState::OFF) {
return PadAction::NO_ACTION;
}
if (currentState == PadState::ON) {
padStates[index_] = PadState::ON_TO_OFF;
padTimes[index_] = std::chrono::steady_clock::now();
return PadAction::NOTE_AFTERTOUCH;
}
if (currentState == PadState::ON_TO_OFF) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - padTimes[index_]);
if (elapsed < PAD_NOTE_OFF_DELAY) {
return PadAction::NOTE_AFTERTOUCH;
}
padStates[index_] = PadState::OFF;
return PadAction::NOTE_OFF;
}
if (currentState == PadState::OFF_TO_ON) {
padStates[index_] = PadState::OFF;
return PadAction::NO_ACTION;
}
}
if (padVelocities[index_] > PAD_THRESHOLD_ON) {
if (currentState == PadState::ON) {
return PadAction::NOTE_AFTERTOUCH;
}
if (currentState == PadState::OFF) {
padStates[index_] = PadState::OFF_TO_ON;
padTimes[index_] = std::chrono::steady_clock::now();
return PadAction::NO_ACTION;
}
if (currentState == PadState::ON_TO_OFF) {
padStates[index_] = PadState::ON;
return PadAction::NOTE_AFTERTOUCH;
}
if (currentState == PadState::OFF_TO_ON) {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - padTimes[index_]);
if (elapsed < PAD_NOTE_ON_DELAY) {
return PadAction::NO_ACTION;
}
padStates[index_] = PadState::ON;
return PadAction::NOTE_ON;
}
}
return PadAction::NO_ACTION;
}
void DeviceManager::sendOSCMessage(std::string address, std::string value) {
if (!oscSocket.is_open()) {
oscSocket.open(ip::udp::v4());
}
displayText = address;
requestDeviceUpdate();
unsigned ptr = 0;
OSC_WRITE_STRING(oscBuffer, ptr, address);
if (value.size() > 0) {
OSC_WRITE_STRING(oscBuffer, ptr, std::string(",s"));
OSC_WRITE_STRING(oscBuffer, ptr, value);
} else {
OSC_WRITE_STRING(oscBuffer, ptr, std::string(","));
}
boost::system::error_code error;
oscSocket.send_to(buffer(oscBuffer, ptr), oscRemoteEndpoint, 0, error);
}
}