-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiin.h
432 lines (387 loc) · 11.7 KB
/
midiin.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <iostream>
#include <cstdlib>
#include "rtmidi/RtMidi.h"
std::string controllerName = "LPD8 20:0";
std::string outputPortName = "Midi Through 14:0";
uint8_t ccMemValues[2048];
uint8_t buttonCC[8] = {52, 53, 54, 55, 56, 57, 58, 59};
uint8_t LPD8Display[8] = {127, 0, 0, 0, 127, 0, 0, 0};
uint8_t LPD8DisplayCH[8] = {127, 0, 0, 0, 127, 0, 0, 0};
uint8_t activeUp = 127;
uint8_t activeDn = 0;
uint8_t LPD8page = 0;
uint8_t LPD8pageCH = 0;
uint8_t LPD8preset = 0;
uint8_t LPD8chan = 0;
unsigned int ledPos = 0;
unsigned int ledPosCH = 0;
uint8_t ccStartNum = 20;
bool isFirstButtonPressed = false;
bool isSecndButtonPressed = false;
bool isPageCH = false;
RtMidiIn *midiin = 0;
RtMidiOut *midiout = 0;
RtMidiOut *midiout2 = 0;
std::vector<unsigned char> message;
void initCCValues()
{
for (int i = 0; i < 2048; i++)
{
ccMemValues[i] = 0;
}
}
void highlightKnobSelector(uint8_t valueCC)
{
for (int i = 0; i <= 7; i++)
{
if ((valueCC / 16) == i)
{
LPD8Display[i] = activeUp;
}
else
{
LPD8Display[i] = activeDn;
}
message[0] = 191; //canale midi 16 !!!
message[1] = buttonCC[i];
message[2] = LPD8Display[i];
midiout->sendMessage(&message);
}
std::cout << "Led Number = " << (valueCC / 16) + 1 << std::endl;
}
void drawLPD8intrfc()
{
for (unsigned int i = 0; i <= 7; i++)
{
message[0] = 191;
message[1] = buttonCC[i];
message[2] = LPD8Display[i];
midiout->sendMessage(&message);
}
}
void drawLPD8intrfcCH()
{
for (unsigned int i = 0; i <= 7; i++)
{
message[0] = 191;
message[1] = buttonCC[i];
message[2] = LPD8DisplayCH[i];
midiout->sendMessage(&message);
}
}
void fillLPD8intrfcCH(uint8_t valueCC)
{
unsigned int i = 0;
if (valueCC >= buttonCC[0] && valueCC <= buttonCC[3]) //check if input value is in upper part
{
LPD8pageCH = (valueCC - buttonCC[0]);
for (i = 0; i <= 3; i++)
{
if (LPD8pageCH == i)
{
LPD8DisplayCH[i] = activeDn;
}
else
{
LPD8DisplayCH[i] = activeUp;
}
}
std::cout << "page: " << (unsigned int)LPD8page + 1 << std::endl;
std::cout << "pageCH: " << (unsigned int)LPD8pageCH + 1 << std::endl;
std::cout << "preset: " << (unsigned int)LPD8preset + 1 << std::endl;
std::cout << "midiCH: " << (unsigned int)LPD8chan + 1 << std::endl;
}
if (valueCC >= buttonCC[4] && valueCC <= buttonCC[7]) //check if input value is in lower part
{
ledPosCH = (valueCC - buttonCC[4]);
LPD8chan = (4 * LPD8pageCH) + ledPosCH;
std::cout << "page: " << (unsigned int)LPD8page + 1 << std::endl;
std::cout << "pageCH: " << (unsigned int)LPD8pageCH + 1 << std::endl;
std::cout << "preset: " << (unsigned int)LPD8preset + 1 << std::endl;
std::cout << "midiCH: " << (unsigned int)LPD8chan + 1 << std::endl;
}
if (LPD8chan >= LPD8pageCH * 4 && LPD8chan < LPD8pageCH * 4 + 4) //check if preset can be viewed from this page
{
for (i = 0; i <= 3; i++)
{
if (i == ledPosCH)
{
LPD8DisplayCH[i + 4] = activeDn;
}
else
{
LPD8DisplayCH[i + 4] = activeUp;
}
}
}
else
{
for (i = 0; i <= 3; i++)
{
LPD8DisplayCH[i + 4] = activeUp;
}
}
drawLPD8intrfcCH();
}
void fillLPD8intrfc(uint8_t valueCC)
{
unsigned int i = 0;
if (valueCC >= buttonCC[0] && valueCC <= buttonCC[3]) //check if input value is in upper part
{
LPD8page = (valueCC - buttonCC[0]);
for (i = 0; i <= 3; i++)
{
if (LPD8page == i)
{
LPD8Display[i] = activeUp;
}
else
{
LPD8Display[i] = activeDn;
}
}
std::cout << "page: " << (unsigned int)LPD8page + 1 << std::endl;
std::cout << "pageCH: " << (unsigned int)LPD8pageCH + 1 << std::endl;
std::cout << "preset: " << (unsigned int)LPD8preset + 1 << std::endl;
std::cout << "midiCH: " << (unsigned int)LPD8chan + 1 << std::endl;
}
if (valueCC >= buttonCC[4] && valueCC <= buttonCC[7]) //check if input value is in lower part
{
ledPos = (valueCC - buttonCC[4]);
LPD8preset = (4 * LPD8page) + ledPos;
std::cout << "page: " << (unsigned int)LPD8page + 1 << std::endl;
std::cout << "pageCH: " << (unsigned int)LPD8pageCH + 1 << std::endl;
std::cout << "preset: " << (unsigned int)LPD8preset + 1 << std::endl;
std::cout << "midiCH: " << (unsigned int)LPD8chan + 1 << std::endl;
}
if (LPD8preset >= LPD8page * 4 && LPD8preset < LPD8page * 4 + 4) //check if preset can be viewed from this page
{
for (i = 0; i <= 3; i++)
{
if (i == ledPos)
{
LPD8Display[i + 4] = activeUp;
}
else
{
LPD8Display[i + 4] = activeDn;
}
}
}
else
{
for (i = 0; i <= 3; i++)
{
LPD8Display[i + 4] = activeDn;
}
}
drawLPD8intrfc();
}
// fill memory and send cc
void fillCCMemValues(uint8_t ccNum, uint8_t ccValue)
{
uint8_t ccNumToSend = (8 * LPD8preset) + (ccNum - ccStartNum);
uint8_t memPos = (128 * LPD8chan) + (8 * LPD8preset) + (ccNum - ccStartNum);
ccMemValues[memPos] = ccValue;
message[0] = (176 + LPD8chan); //canale midi 16 !!!
message[1] = ccNumToSend;
message[2] = ccValue;
midiout2->sendMessage(&message);
std::cout << (int)message[0] << " " << (int)message[1] << " " << (int)message[2] << std::endl;
}
void mycallback(double deltatime, std::vector<unsigned char> *message, void * /*userData*/)
{
unsigned int nBytes = message->size();
for (unsigned int i = 0; i < nBytes; i++)
std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
if (nBytes > 0)
{
std::cout << "stamp = " << deltatime << std::endl;
}
// check for knob controllers
if (message->at(0) == 191 && message->at(1) >= ccStartNum && message->at(1) <= ccStartNum + 7)
{
fillCCMemValues(message->at(1), message->at(2));
}
if (message->at(0) == 191 && message->at(1) == buttonCC[0] && message->at(2) != 0)
{ // first button is pressed
isFirstButtonPressed = true;
}
if (message->at(0) == 191 && message->at(1) == buttonCC[0] && message->at(2) == 0)
{ // first button is released
isFirstButtonPressed = false;
}
if (message->at(0) == 191 && message->at(1) == buttonCC[3] && message->at(2) != 0)
{ // first button is pressed
isSecndButtonPressed = true;
}
if (message->at(0) == 191 && message->at(1) == buttonCC[3] && message->at(2) == 0)
{ // first button is released
isSecndButtonPressed = false;
}
// check for buttons
if (message->at(0) == 191 && message->at(1) >= buttonCC[0] && message->at(1) <= buttonCC[7])
{
if (isFirstButtonPressed && isSecndButtonPressed)
{
if (isPageCH)
{
isPageCH = false;
}
else
{
isPageCH = true;
}
}
}
if (isPageCH)
{
fillLPD8intrfcCH(message->at(1));
}
else
{
fillLPD8intrfc(message->at(1));
}
}
bool chooseMidiIn(RtMidiIn *rtmidi);
bool chooseMidiOut(RtMidiOut *rtmidi);
bool chooseMidiOut2(RtMidiOut *rtmidi);
void setupMidi()//called by main.cpp
{
message.push_back(0);
message.push_back(0);
message.push_back(0);
initCCValues();
try
{
// RtMidiOut constructor
midiout = new RtMidiOut();
// Call function to select port.
if (chooseMidiOut(midiout) == false)
goto cleanup;
}
catch (RtMidiError &error)
{
error.printMessage();
}
try
{
// RtMidiOut constructor
midiout2 = new RtMidiOut();
// Call function to select port.
if (chooseMidiOut2(midiout2) == false)
goto cleanup;
}
catch (RtMidiError &error)
{
error.printMessage();
}
try
{
// RtMidiIn constructor
midiin = new RtMidiIn();
// Call function to select port.
if (chooseMidiIn(midiin) == false)
goto cleanup;
// Set our callback function. This should be done immediately after
// opening the port to avoid having incoming messages written to the
// queue instead of sent to the callback function.
midiin->setCallback(&mycallback);
// Don't ignore sysex, timing, or active sensing messages.
midiin->ignoreTypes(false, false, false);
std::cout << "\nWorking... press <enter> to quit.\n";
char input;
std::cin.get(input);
std::cout << "\nKey pressed: " << input << "\n";
}
catch (RtMidiError &error)
{
error.printMessage();
}
cleanup:
delete midiin;
delete midiout;
delete midiout2;
}
bool chooseMidiOut(RtMidiOut *rtmidi) //choose output for controller display
{
std::string keyHit;
std::string portName;
unsigned int i = 0, nPorts = rtmidi->getPortCount();
if (nPorts == 0)
{
std::cout << "No input ports available!" << std::endl;
return false;
}
std::cout << "\nChoose a port number for controller: \n\n";
for (i = 0; i < nPorts; i++)
{
portName = rtmidi->getPortName(i);
std::cout << " Output port " << i << " " << portName << '\n';
}
do
{
std::cin >> i;
} while (i >= nPorts);
std::getline(std::cin, keyHit); // used to clear out stdin
rtmidi->openPort(i);
controllerName = rtmidi->getPortName(i);
std::cout << "\nAssigned controller: " << controllerName << std::endl;
return true;
}
bool chooseMidiOut2(RtMidiOut *rtmidi) //choose where to output CCs
{
std::cout << "\nWould you like to open a virtual output port? [y/N] ";
std::string keyHit;
std::getline(std::cin, keyHit);
if (keyHit == "y")
{
rtmidi->openVirtualPort();
outputPortName = rtmidi->getPortName();
std::cout << "\nAssigned output port: RtMidi Output Client" << std::endl;
return true;
}
//std::string portName;
unsigned int i = 0, nPorts = rtmidi->getPortCount();
if (nPorts == 0)
{
std::cout << "No output ports available!" << std::endl;
return false;
}
std::cout << "\nChoose a port number for output: \n\n";
for (i = 0; i < nPorts; i++)
{
outputPortName = rtmidi->getPortName(i);
std::cout << " Output port " << i << " " << outputPortName << '\n';
}
do
{
std::cin >> i;
} while (i >= nPorts);
std::getline(std::cin, keyHit); // used to clear out stdin
rtmidi->openPort(i);
std::cout << "\nAssigned output: " << outputPortName << std::endl;
return true;
}
bool chooseMidiIn(RtMidiIn *rtmidi)
{
std::string portName;
unsigned int i = 0, nPorts = rtmidi->getPortCount();
if (nPorts == 0)
{
std::cout << "No input ports available!" << std::endl;
return false;
}
for (i = 0; i < nPorts; i++)
{
portName = rtmidi->getPortName(i);
//std::cout << " Input port '" << i << "' " << portName << '\n';
if (portName == controllerName)
{
rtmidi->openPort(i);
//std::cout << " assigned in port '" << i << "' " << portName << '\n';
return true;
}
}
return false;
}