forked from davidsami/rb3_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rb3_driver.c
563 lines (481 loc) · 19.2 KB
/
rb3_driver.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
* Copyright (c) 2015 Martin Sidaway
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <libusb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <portmidi.h>
//#ifdef _POSIX_SOURCE
#include <unistd.h>
// #endif
// #ifdef _WIN32
// #include <windows.h>
// #define sleep(x) Sleep(1000*(x))
// #endif
#include "myusb_atexit.h"
#include "myusb_utils.h"
#define DATA_BUFFER_LEN 27
#define TRANSFER_TIMEOUT 500
#define BITMAP_OFFSET 5
#define BITMAP_LEN 4
#define VELS_OFFSET 8
#define VELS_LEN 5
#define NUM_KEYS (BITMAP_LEN * 8 - 7)
#define NORMAL_CHAN 0
#define DEFAULT_VEL 0x40
#define DEFAULT_1STNOTE 48
#define DEFAULT_PATCH 0
#define DRUMMAP_CHAN 9
#define DRUMMAP_NKEYS 12
#define OCTAVE_OFFSET 0
#define OCTAVE_DOWN 1 // Key "1"
#define OCTAVE_UP 4 // Key "B"
#define PATCH_OFFSET 0
#define PATCH_DOWN 2 // Key "A"
#define PATCH_UP 8 // Key "2"
#define PBBTN_OFFSET 13
#define PBBTN_VALUE 0x80
#define PED_OFFSET 14
#define MOD_OFFSET 15
#define PEDST_OFFSET 20
#define PEDST_REST 0
#define PEDST_STOMP 1
#define PEDST_PARTIAL 2
#define PEDST_FULL 3
#define SEQ_OFFSET 1
#define SEQ_STOP 0x01
#define SEQ_CONT 0x10
#define SEQ_START 0x02
#define DPAD_OFFSET 2
#define DPAD_MASK 0xf
#define DPAD_UP 0
#define DPAD_RIGHT 2
#define DPAD_DOWN 4
#define DPAD_LEFT 6
#define DPAD_CENTER 8
#define MIDI_NOTEON 0x90
#define MIDI_NOTEOFF 0x80
#define MIDI_PROGCH 0xC0
#define MIDI_CTRLCH 0xB0
#define MIDI_PBEND 0xE0
#define MIDI_SEQSTART 0xFA
#define MIDI_SEQCONT 0xFB
#define MIDI_SEQSTOP 0xFC
#define MIDI_CC_MOD 1
#define MIDI_CC_FOOT 4
#define MIDI_CC_VOL 7
#define MIDI_CC_EXP 11
#define MIDI_CC_DAMP 64
#define MIDI_CM_ALLOFF 0x7B
#define MIDI_CHANMASK 0xf
#define MIDI_NUMPATCHES 0x80
#define SLEEP_IF_CHOOSEDEVICE() do { if (chooseDevice) sleep(3); } while (0)
uint8_t drumMapNotes[] = {
35, 36, 38, 40, 41, 47,
50, 42, 46, 49, 51, 53,
};
void mypm_terminate(void *ignored) {
Pm_Terminate();
}
void mypm_close(void *data) {
PortMidiStream *str = (PortMidiStream *)data;
Pm_Close(str);
}
static inline int keyDown(const char *buffer, int index, int mask) {
return (buffer[index] & mask) == mask;
}
static inline int keyPressed(const char *curBuffer, const char *lastBuffer,
int index, int mask) {
return (curBuffer[index] & mask) == mask && (lastBuffer[index] & mask) == 0;
}
int main(int argc, char **argv) {
int r;
int i, output_i, input_i, k;
int chooseDevice = 0;
if (argc < 2) {
fprintf(stderr, "\nAvailable output devices:\n\n");
chooseDevice = 1;
//return 4;
}
/* initialize portmidi */
r = Pm_Initialize();
if (r < 0) {
fprintf(stderr, "Failed to initialize portmidi\n");
SLEEP_IF_CHOOSEDEVICE();
return r;
};
my_atexit(mypm_terminate, NULL);
// Check if we're dealing with a port number
int isPortNumber = 0;
int portNumber = 0;
if (chooseDevice == 0)
{
portNumber = atoi( argv[1] );
isPortNumber = (portNumber != 0 || (argv[1][0] == '0' && strlen(argv[1]) == 1));
}
int pmdCount = Pm_CountDevices();
//fprintf(stderr, "Got %d portmidi devices\n", pmdCount);
PmDeviceID pmDev = -1;
for (i = 0, output_i = 1, input_i = 0; i < pmdCount; ++i) {
const PmDeviceInfo *pmdInfo = Pm_GetDeviceInfo(i);
if (pmdInfo != NULL && pmdInfo->output != 0) {
if (chooseDevice) {
fprintf(stderr, " Output %d: \"%s\" (ID %d)\n", output_i, pmdInfo->name, i);
} else if ((strcmp(argv[1], pmdInfo->name) == 0) ||
(isPortNumber && (output_i == portNumber))) {
pmDev = i;
}
++output_i;
}
else
{
// Display inputs as well
if (chooseDevice) {
fprintf(stderr, " Input %d: \"%s\" (ID %d)\n", input_i, pmdInfo->name, i);
}
++input_i;
}
}
if (chooseDevice) {
fprintf(stderr, "\nPlease type the number of your chosen output MIDI device and press the Enter key.\n\n");
scanf("%d", &k);
fprintf(stderr, "\n");
for (i = 0, output_i = 1; i < pmdCount; ++i) {
const PmDeviceInfo *pmdInfo = Pm_GetDeviceInfo(i);
if (pmdInfo != NULL && pmdInfo->output != 0) {
if (output_i == k) {
pmDev = i;
break;
}
++output_i;
}
}
}
if (pmDev == -1) {
fprintf(stderr, "Unable to find MIDI output device\n");
SLEEP_IF_CHOOSEDEVICE();
return 3;
}
PortMidiStream *outStream;
r = Pm_OpenOutput(&outStream, pmDev, NULL, 0, NULL, NULL, 0);
if (r < 0) {
fprintf(stderr, "Failed to open MIDI output device\n");
SLEEP_IF_CHOOSEDEVICE();
return r;
}
my_atexit(mypm_close, outStream);
fprintf(stderr, "Got MIDI output device!\n");
/* Initialize libusb */
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "Failed to initialize libusb\n");
SLEEP_IF_CHOOSEDEVICE();
return r;
}
my_atexit(myusb_exit, NULL);
// Hardcoded args n shit
int device_index = 0;
if (argc >= 3)
{
device_index = atoi(argv[2]);
}
libusb_device *dev =
myusb_get_device_by_prod_name_prefix("Harmonix RB3 Keyboard", device_index);
if (dev == NULL) {
fprintf(stderr, "Failed to find input device\n");
SLEEP_IF_CHOOSEDEVICE();
return 1;
}
fprintf(stderr, "Brilliant news! Found input device!\n");
uint8_t interface_number = 0;
const struct libusb_endpoint_descriptor *endpoint =
myusb_get_endpoint(dev, LIBUSB_ENDPOINT_IN,
LIBUSB_TRANSFER_TYPE_MASK, LIBUSB_TRANSFER_TYPE_INTERRUPT, 0,
&interface_number);
if (endpoint == NULL) {
fprintf(stderr, "No suitable endpoint on input device\n");
SLEEP_IF_CHOOSEDEVICE();
return 2;
}
fprintf(stderr, "Got input device endpoint!\n");
libusb_device_handle *h = NULL;
r = libusb_open(dev, &h);
if (r < 0) {
fprintf(stderr, "Failed to open input device\n");
SLEEP_IF_CHOOSEDEVICE();
return r;
}
my_atexit(myusb_close, h);
r = libusb_detach_kernel_driver(h, interface_number);
r = libusb_claim_interface(h, interface_number);
if (r < 0) {
printf("%d\n%d", r);
fprintf(stderr, "Failed to claim input device interface\n");
SLEEP_IF_CHOOSEDEVICE();
return r;
}
myusb_atexit_release_interface(h, interface_number);
uint8_t buffer1[DATA_BUFFER_LEN];
uint8_t buffer2[DATA_BUFFER_LEN];
memset(buffer1, 0, sizeof(buffer1));
memset(buffer2, 0, sizeof(buffer2));
uint8_t *curBuffer = buffer1;
uint8_t *lastBuffer = buffer2;
int transferred_len = 0;
// MIDI state
int numKeysDown = 0;
uint8_t notesDown[NUM_KEYS]; // What note was last activated for a given key?
uint8_t chansDown[DRUMMAP_NKEYS]; // What channel was last activated for a given key?
uint8_t firstNote = DEFAULT_1STNOTE;
int8_t curPatch = DEFAULT_PATCH;
int8_t drumMapOn = 0;
uint8_t pedalCC = MIDI_CC_EXP;
//struct libusb_transfer transfer;
//libusb_fill_interrupt_transfer(&transfer, h, endpoint->bEndpointAddress, buffer, DATA_BUFFER_LEN, got_data, NULL, TRANSFER_TIMEOUT);
uint8_t bitmask;
int cv, lv;
while (1) {
r = libusb_interrupt_transfer(h, endpoint->bEndpointAddress, curBuffer, DATA_BUFFER_LEN, &transferred_len, TRANSFER_TIMEOUT);
if (r == LIBUSB_ERROR_TIMEOUT) {
fprintf(stderr, "Data transfer timed out (input)\n");
continue;
}
if (r < 0 || transferred_len == 0) {
// N.B. this happens when the USB dongle is removed.
fprintf(stderr, "Data transfer failed (input)\n");
break;
}
if (transferred_len < DATA_BUFFER_LEN) {
fprintf(stderr, "Wrong packet size (input)\n");
continue;
}
if (memcmp(curBuffer, lastBuffer, DATA_BUFFER_LEN) != 0) {
// DEBUG: dump input USB packet
//for (i = 0; i < DATA_BUFFER_LEN; i++) {
// fprintf(stderr, " %02x", curBuffer[i]);
//}
//fprintf(stderr, "\n");
// Octave and program change
if (curBuffer[OCTAVE_OFFSET] != lastBuffer[OCTAVE_OFFSET]) {
uint8_t upOctPressed = keyPressed(curBuffer, lastBuffer, OCTAVE_OFFSET, OCTAVE_UP);
uint8_t downOctPressed = keyPressed(curBuffer, lastBuffer, OCTAVE_OFFSET, OCTAVE_DOWN);
if ((upOctPressed || downOctPressed) &&
keyDown(curBuffer, OCTAVE_OFFSET, OCTAVE_UP) &&
keyDown(curBuffer, OCTAVE_OFFSET, OCTAVE_DOWN)) {
firstNote = DEFAULT_1STNOTE;
} else if (upOctPressed && firstNote <= 84) {
firstNote += 12;
} else if (downOctPressed && firstNote >= 12) {
firstNote -= 12;
}
// N.B. assuming OCTAVE_OFFSET == PATCH_OFFSET (to save time)
uint8_t upPatchPressed = keyPressed(curBuffer, lastBuffer, PATCH_OFFSET, PATCH_UP);
uint8_t downPatchPressed = keyPressed(curBuffer, lastBuffer, PATCH_OFFSET, PATCH_DOWN);
if ((upPatchPressed || downPatchPressed) &&
keyDown(curBuffer, PATCH_OFFSET, PATCH_UP) &&
keyDown(curBuffer, PATCH_OFFSET, PATCH_DOWN)) {
curPatch = DEFAULT_PATCH;
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_PROGCH, curPatch, 0));
} else if (upPatchPressed) {
curPatch = (curPatch + 1) % MIDI_NUMPATCHES;
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_PROGCH, curPatch, 0));
} else if (downPatchPressed) {
curPatch -= 1;
if (curPatch < 0) {
curPatch = MIDI_NUMPATCHES - 1;
}
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_PROGCH, curPatch, 0));
}
}
// Sequencer controls
if (curBuffer[SEQ_OFFSET] != lastBuffer[SEQ_OFFSET]) {
uint8_t start = keyPressed(curBuffer, lastBuffer, SEQ_OFFSET, SEQ_START);
uint8_t cont = keyPressed(curBuffer, lastBuffer, SEQ_OFFSET, SEQ_CONT);
uint8_t stop = keyPressed(curBuffer, lastBuffer, SEQ_OFFSET, SEQ_STOP);
if ((start || cont || stop) &&
keyDown(curBuffer, SEQ_OFFSET, SEQ_START) &&
keyDown(curBuffer, SEQ_OFFSET, SEQ_CONT) &&
keyDown(curBuffer, SEQ_OFFSET, SEQ_STOP)) {
// Turn off all notes on all channels.
// First, cancel any sequence commands that may have been
// started by mistake.
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_SEQSTOP, 0, 0));
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_CTRLCH | NORMAL_CHAN, MIDI_CM_ALLOFF, 0));
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_CTRLCH | DRUMMAP_CHAN, MIDI_CM_ALLOFF, 0));
} else if (start) {
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_SEQSTART, 0, 0));
} else if (cont) {
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_SEQCONT, 0, 0));
} else if (stop) {
Pm_WriteShort(outStream, 0, Pm_Message(MIDI_SEQSTOP, 0, 0));
}
}
// Modulation and pitch bend
if (curBuffer[MOD_OFFSET] != lastBuffer[MOD_OFFSET] &&
curBuffer[PBBTN_OFFSET] == lastBuffer[PBBTN_OFFSET]) {
if ((curBuffer[PBBTN_OFFSET] & PBBTN_VALUE) == PBBTN_VALUE) {
// Pitch bend
uint8_t value = 0x40; // Reset value
if (curBuffer[MOD_OFFSET] != 0) {
value = curBuffer[MOD_OFFSET] - 1;
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_PBEND, 0, value));
}
} else {
// Modulation
if (curBuffer[MOD_OFFSET] != 0) {
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_CTRLCH, MIDI_CC_MOD,
curBuffer[MOD_OFFSET] - 1));
}
}
}
// Drum split & pedal mode
/*
int curDpad = curBuffer[DPAD_OFFSET] & DPAD_MASK;
int lastDpad = lastBuffer[DPAD_OFFSET] & DPAD_MASK;
if (curDpad != DPAD_CENTER && curDpad != lastDpad) {
uint8_t newPedalCC = pedalCC;
switch (curDpad) {
case DPAD_UP:
drumMapOn = !drumMapOn;
break;
case DPAD_LEFT:
newPedalCC = MIDI_CC_EXP;
break;
case DPAD_DOWN:
newPedalCC = MIDI_CC_VOL;
break;
case DPAD_RIGHT:
newPedalCC = MIDI_CC_FOOT;
break;
}
if (newPedalCC != pedalCC) {
// Reset old pedal to rest state
// Don't do this - the keyboard doesn't.
// It may be useful to "hold" a value of one pedal while
// changing another.
//Pm_WriteShort(outStream, 0,
// Pm_Message(MIDI_CTRLCH, pedalCC, 0x7F));
pedalCC = newPedalCC;
}
}
*/
// Pedal value
uint8_t curPedal = curBuffer[PED_OFFSET];
uint8_t lastPedal = lastBuffer[PED_OFFSET];
if (curPedal != lastPedal) {
uint8_t curAnalog = curPedal & 0x7F;
uint8_t lastAnalog = lastPedal & 0x7F;
if (curAnalog != lastAnalog) {
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_CTRLCH, pedalCC, curAnalog));
}
uint8_t curDigital = curPedal & 0x80;
uint8_t lastDigital = lastPedal & 0x80;
if (curDigital != lastDigital) {
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_CTRLCH, MIDI_CC_DAMP,
curDigital ? 0x7F : 0));
}
}
// Keys
uint8_t t = 0;
uint8_t keyIndex = 0;
int velsKeyIndex = 0;
uint8_t vel;
uint8_t chan;
uint8_t note;
// Each byte in bitmap
for (i = BITMAP_OFFSET; i < BITMAP_OFFSET + BITMAP_LEN; i++) {
// Interferes with velocity array index calculations.
//if (curBuffer[i] == lastBuffer[i]) {
// keyIndex += 8;
// continue;
//}
if (i == BITMAP_OFFSET + BITMAP_LEN - 1) {
// Last byte only has one bit to process.
t = 0x40;
}
// Each bit
for (bitmask = 0x80; bitmask != t; bitmask >>= 1) {
cv = curBuffer[i] & bitmask;
lv = lastBuffer[i] & bitmask;
if (cv != lv) {
if (cv) {
// Note On
chan = NORMAL_CHAN;
note = firstNote + keyIndex;
//printf("\x90%c\x40", note);
//fprintf(stderr, "Sending NoteOn(%d)\n", note);
if (keyIndex < DRUMMAP_NKEYS) {
if (drumMapOn) {
chan = DRUMMAP_CHAN;
note = drumMapNotes[keyIndex];
}
chansDown[keyIndex] = chan;
}
vel = DEFAULT_VEL;
if (numKeysDown < VELS_LEN && velsKeyIndex < VELS_LEN) {
// N.B. accepting a few strange but harmless
// (and unlikely) edge cases here in the
// interests of efficiency.
vel = (0x7f & curBuffer[VELS_OFFSET + velsKeyIndex]);
}
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_NOTEON | (MIDI_CHANMASK & chan),
note, vel));
notesDown[keyIndex] = note;
++numKeysDown;
} else {
// Note Off
//printf("\x80%c\x40", notesDown[keyIndex]);
//fprintf(stderr, "Sending NoteOff(%d)\n", notesDown[keyIndex]);
chan = NORMAL_CHAN;
if (keyIndex < DRUMMAP_NKEYS) {
chan = chansDown[keyIndex];
}
Pm_WriteShort(outStream, 0,
Pm_Message(MIDI_NOTEOFF | (MIDI_CHANMASK & chan),
notesDown[keyIndex], 0x40));
--numKeysDown;
}
//fflush(stdout);
}
if (cv != 0) {
++velsKeyIndex;
}
++keyIndex;
}
}
// Swap buffers
uint8_t *tmp = curBuffer;
curBuffer = lastBuffer;
lastBuffer = tmp;
}
transferred_len = 0;
}
return 0;
}