-
Notifications
You must be signed in to change notification settings - Fork 0
/
X32Reaper.c
2558 lines (2544 loc) · 84.9 KB
/
X32Reaper.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* X32Reaper.c
*
* Created on: 20 mars 2015
* Author: Patrick-Gilles Maillot
*
*
* Ver 1.7: first version being published
* Ver 1.8: added variable TrackSendOffset and associated code to deal with REAPER increasing the
* logical Track Send values when one also assigns a HW output to a REAPER track
* Ver 1.9: A user correctly reported a pb with several track being simultaneously selected... System
* behaves badly. This was due to my code trying to align the REAPER slider values to X32
* fader values. I don't send the X32 values back to REAPER anymore (code is commented, for now)
* Ver 1.91: For Windows, save interactive window size changes
* removed useless ioctl() call - use of select() is enough for non-blocking mode
* Ver 2.0: Introduce REAPER DCA tracks; multiple REAPER tracks can be controlled from X32 DCA
* channels
* Ver 2.1: Added bank selection (up/down) capability on X32 side, only controlling REAPER. For X32 Channels
* only, and by blocks of 32. optimized some Windows calls
* Ver 2.2: Updated with bank selection recall (up/down) capability, reflecting on X32 the corresponding
* REAPER bank data and updating REAPER selected channel to reflect X32 select button value (in
* Xselected); Xselected is also setup at init by reading the X32 state.
* Additionally, this version enables to set icon type and scribble colors with the track names
* by using the following syntax "<name>[ %icon[ %color]]" (spaces are optional.
* Ver 2.21 & 2.22: bug fixes around memory allocation logic when changing Channel Bank Select state
* Ver 2.3: removed /action commands 53808 and 53809 which seem to not exist anymore
* Ver 2.4: Added the capability to enable bank selection (up/down)without transport control, adding two
* values to the resource file.
* Ver 2.41: small optimizations in dealing with channel bank changes
* Ver 2.5: Updates for setting buttons added in 2.4 in the GUI - removed Master select flag (always ON)
* Modifications to preset and resource files contents and handling
* Ver 2.51: bug fix. Some channels could be set to > 32 for X32... which obviously was wrong
* Ver 2.52: small fix on rounding the fader values to X32 known ones
* also enables filtering what is sent to X32 from REAPER
* Ver 2.6: Adding capability to set REAPER markers from bank C buttons (exclusive from Transport)
* Ver 2.61: Fixed the fact that Marker insert request was sent twice, and added a possible bank C color :)
* Ver 2.62: Bank size can be less than 32 (8 or 16 are good options for X32).
* Also limits the actual number physical channels that can be used
* Ver 2.63: Small bug fixes to 2.62
* Ver 2.64: Added a specific delay for banks (Xdelayb)
* Ver 2.65: Fixed a bug where the first RDCA REAPER track would not update X32
* Ver 2.66: inclusion of eq handling for X32 (fx/1 for REAPER)
* Ver 2.67: inclusion a bitwise mask to prevent X32 changes to affect REAPER
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#define BNKSZ 32 // size (number of tracks) of a bank
#define BSIZE 512 // Buffer sizes (enough to take into account FX parameters)
#ifdef __WIN32
#include <windows.h>
#define MySleep(a) Sleep((a))
#define socklen_t int
#else
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define closesocket(s) close((s))
#define MySleep(a) usleep((a)*1000)
#define WSACleanup()
#define SOCKET_ERROR -1
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#endif
//
// X32 to communicate on X32_IP (192.168.0.64) and X32 port (10023)
// REAPER to communicate on Rea_IP (192.168.0.64) and REAPER ports
//
// Buffers: Xb_r, Xb_s, Xb_lr, Xb_ls - read, send and lengths for X32
// Buffers: Rb_r, Rb_s, Rb_lr, Rb_ls - read, send and lengths for Reaper
// Defines: XBsmax, RBsmax, XBrmax, RBrmax - maximum lengths for buffers
//
#define XBsmax BSIZE
#define RBsmax BSIZE
#define XBrmax BSIZE * 2
#define RBrmax BSIZE * 4
//
// defining bits for enabling sending certain commands to X32 following REAPER parsing
#define TRACKPAN 0x0001
#define TRACKFADER 0x0002
#define TRACKNAME 0x0004
#define TRACKMUTE 0x0008
#define TRACKSELECT 0x0010
#define TRACKSEND 0x0020
#define TRACKSOLO 0x0040
#define TRACKFX 0x0080
#define MASTERPAN 0x0100
#define MASTERVOLUME 0x0200
//
// defining bits for enabling sending certain commands to REAPER following X32 parsing
#define X32PAN 0x0001
#define X32FADER 0x0002
#define X32NAME 0x0004
#define X32MUTE 0x0008
#define X32SELECT 0x0010
#define X32SEND 0x0020
#define X32SOLO 0x0040
#define X32FX 0x0080
#define X32MPAN 0x0100
#define X32MFADER 0x0200
//
// Private functions
void X32UsrCtrlC();
int X32Connect();
void XRcvClean();
void Xlogf(char *header, char *buf, int len);
void X32_eqon(int Xb_i, int bank, int cnum, int cnum1);
void X32_eqfr(int Xb_i, int bank, int cnum, int cnum1);
void X32_eqgq(int Xb_i, int bank, int cnum, int cnum1, int index);
void X32ParseReaperMessage();
void X32ParseX32Message();
//
// External calls used
extern int Xsprint(char *bd, int index, char format, void *bs);
extern int Xfprint(char *bd, int index, char* text, char format, void *bs);
//
//
// Communication macros
//
#define SEND_TOX(delay) \
do { \
if (Xverbose) Xlogf("->X", Xb_s, Xb_ls); \
if (sendto(Xfd, Xb_s, Xb_ls, 0, XX32IP_pt, XX32IP_len) < 0) { \
fprintf(log_file, "Error sending data to X32\n"); \
exit(EXIT_FAILURE); \
} \
if (delay > 0) MySleep((delay)); \
} while (0);
//
//
#define SEND_TOR() \
do { \
if (Xverbose) Xlogf("->R", Rb_s, Rb_ls); \
if (sendto(Rfd, Rb_s, Rb_ls, 0, RHstIP_pt, RHstIP_len) < 0) { \
fprintf(log_file, "Error sending data to REAPER\n"); \
exit(EXIT_FAILURE); \
} \
} while (0);
//
// resource and log file data
FILE *res_file;
FILE *log_file;
//
// type cast union
union littlebig {
char cc[4];
int ii;
float ff;
} endian;
//
// reserve communication buffers
int Xb_ls;
char Xb_s[XBsmax];
int Xb_lr;
char Xb_r[XBrmax];
int Rb_lr;
char Rb_r[RBrmax];
int Rb_ls;
char Rb_s[RBsmax];
//
char *errorX32 = "Error sending data to X32\n";
char *errorRea = "Error sending data to REAPER\n";
int loop_toggle = 0x00; // toggles between 0x00 and 0x7f
//
char S_SndPort[8], S_RecPort[8], S_X32_IP[20], S_Hst_IP[20];
//char Xlogpath[LENPATH];
//
int zero = 0;
int one = 1;
int two = 2;
int four = 4;
int six4 = 64;
float fone = 1.0;
int option = 1;
int play = 0;
int play_1 = 0;
// Misc. flags
int MainLoopOn = 1; // main loop flag
int Xconnected = 0; // 1 when communication is running
int Xverbose; // verbose flag
int Xdelayb, Xdelayg; // OSC delay for banks control and generic OSC delay
int Xtransport_on = 0; // whether transport is enabled or not (bank C)
int XMkerbt_on = 0; // Marker Button number on or not (bank C)
int Xchbank_on = 0; // whether we use Channel bank select and not Loops in bank C
int Xchbkof = 0; // channel bank number
int Xselected = 1; // X32 channel currently selected
int Rselected = 1; // REAPER track currently selected
int Xmaster_on = 1; // whether master is enabled or not
int TrackSendOffset = 0;// offset to manage REAPER track sends logical numbering
int XbankCcol = 0; // Bank C color (is set when reading .ini file)
int Xeqctrl_on = 0; // Enable EQ UI Control (is set when reading .ini file)
//
int XMbankup; // user "bank up" selected button [5..12]
int XMbankdn; // user "bank down" selected button [5..12]
int XMkerbtn; // Marker Button number
//
int Xtrk_min = 0; // Input min track number for Reaper/X32
int Xtrk_max = 0; // Input max track number for Reaper/X32
int Xaux_min = 0; // Auxin min track number for Reaper/X32
int Xaux_max = 0; // Auxin max track number for Reaper/X32
int Xfxr_min = 0; // FXrtn min track number for Reaper/X32
int Xfxr_max = 0; // FXrtn max track number for Reaper/X32
int Xbus_min = 0; // Bus min track number for Reaper/X32
int Xbus_max = 0; // Bus max track number for Reaper/X32
int Xdca_min = 0; // DCA min track number for Reaper/X32
int Xdca_max = 0; // DCA max track number for Reaper/X32
int Rdca_min[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // REAPER 'dca' mins
int Rdca_max[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // REAPER 'dca' maxs
int XXmask = 0; // bit mask for commands sent to X32 following REAPER parsing
int Xxsend = -1; // bits for enabling sending commands sent to X32
int XRmask = 0; // bit mask for commands sent to REAPER following X32 parsing
int Xrsend = -1; // bits for enabling sending commands sent to REAPER
//
struct ifaddrs *ifa; // to get our own system's IP address
struct sockaddr_in XX32IP; // X socket IP we send/receive
struct sockaddr *XX32IP_pt = (struct sockaddr*) &XX32IP;// X socket IP pointer we send/receive
struct sockaddr_in RHstIP; // R socket IP we send to
struct sockaddr *RHstIP_pt = (struct sockaddr*) &RHstIP;// R socket IP pointer we send to
struct sockaddr_in RFrmIP; // R socket IP we received from
struct sockaddr *RFrmIP_pt = (struct sockaddr*) &RFrmIP;// R socket IP pointer we received from
int Xfd, Rfd, Mfd;// X32 and Reaper receive and send sockets; Mfd is max(Xfd, Rfd)+1 for select()
//
#ifdef __WIN32__
WSADATA wsa;
int XX32IP_len = sizeof(XX32IP); // length of X32 address
int RHstIP_len = sizeof(RHstIP); // length of Reaper send to address
int RFrmIP_len = sizeof(RFrmIP); // length of Reaper received from address
#else
unsigned int XX32IP_len = sizeof(XX32IP); // length of X32 address
unsigned int RHstIP_len = sizeof(RHstIP); // length of Reaper send to address
unsigned int RFrmIP_len = sizeof(RFrmIP); // length of Reaper received from address
#endif
//
fd_set fds; // socket file descriptor
struct timeval timeout; // UDP non-blocking Read timeout
int p_status; // UDP Read status flag
time_t before, now; // timers for Xremote controls
//
// structure definition for REAPER data backup (used for REAPER bank switches)
// we'll allocate memory according to the number of channels declared in the
// .X32Reaper.ini resource file
typedef struct bkch {
float fader; // volume
float pan; // panoramic
float mixbus[16]; // sends values
float mute; // mute
float solo; // solo
char scribble[16]; // scribble text; keep only 12 chars for X32
int color; // scribble color
int icon; // scribble icon
float eq[16]; // eq params
int eqon; // eq enabled
} S_bkch;
//
S_bkch *XMbanktracks = NULL;// Address to data array for saved channel banks data
int bkchsz = BNKSZ; // size of channel baks (32 or less)
//
//-------------------------------------------------------------------------
//
int main(int argc, char **argv) {
int i, j;
strcpy(S_X32_IP, "");
strcpy(S_Hst_IP, "");
printf("X32Reaper - v2.67 - (c)2015 Patrick-Gilles Maillot\n\n");
// load resource file
if ((res_file = fopen("./.X32Reaper.ini", "r")) != NULL) { // ignore Width and Height
fscanf(res_file, "%d %d %d %d %d %d %d\n", &i, &j, &Xverbose, &Xdelayb, &Xdelayg, &Xxsend, &Xrsend);
fgets(S_X32_IP, sizeof(S_X32_IP), res_file);
S_X32_IP[strlen(S_X32_IP) - 1] = 0;
fgets(S_Hst_IP, sizeof(S_Hst_IP), res_file);
S_Hst_IP[strlen(S_Hst_IP) - 1] = 0;
fgets(S_SndPort, sizeof(S_SndPort), res_file);
S_SndPort[strlen(S_SndPort) - 1] = 0;
fgets(S_RecPort, sizeof(S_RecPort), res_file);
S_RecPort[strlen(S_RecPort) - 1] = 0;
fscanf(res_file, "%d %d %d %d %d %d\n", &Xtransport_on, &Xchbank_on,
&XMkerbt_on, &XbankCcol, &Xeqctrl_on, &Xmaster_on);
fscanf(res_file, "%d %d %d %d %d %d %d %d %d %d %d\n", &Xtrk_min,
&Xtrk_max, &Xaux_min, &Xaux_max, &Xfxr_min, &Xfxr_max,
&Xbus_min, &Xbus_max, &Xdca_min, &Xdca_max, &TrackSendOffset);
for (i = 0; i < 8; i++)
fscanf(res_file, "%d %d\n", &Rdca_min[i], &Rdca_max[i]);
fscanf(res_file, "%d %d %d %d %d\n", &XMbankup, &XMbankdn, &XMkerbtn,
&Xchbkof, &bkchsz);
//
if (Xchbank_on) {
//
// allocate memory for maintaining REAPER data between banks
// We make the choice to allocate by blocks of 32, ensuring we cover all input tracks
// between Xtrk_max and Xtrk_min
if ((XMbanktracks = (S_bkch*) malloc(
((Xtrk_max - Xtrk_min + 1 + bkchsz - 1) / bkchsz) * bkchsz
* sizeof(S_bkch))) == NULL) {
exit(-1);
}
for (i = 0; i < Xtrk_max - Xtrk_min + 1; i++) {
XMbanktracks[i].fader = 0.0;
XMbanktracks[i].pan = 0.5;
XMbanktracks[i].mute = 0.0;
XMbanktracks[i].solo = 0.0;
XMbanktracks[i].color = 0;
XMbanktracks[i].icon = 1;
XMbanktracks[i].eqon = 0;
for (j = 0; j < 16; j++) {
XMbanktracks[i].scribble[j] = 0;// scribbles are 16 chars
XMbanktracks[i].mixbus[j] = 0.0; // 16 mixbus per track
XMbanktracks[i].eq[j] = 0.0; // 16 eq params per track
}
}
if (!Xtransport_on) {// If transport is on, buttons are pre-assigned.
//
// Read banks buttons assignments for up and down, from the resource file
fscanf(res_file, "%d %d\n", &XMbankup, &XMbankdn);
}
fclose(res_file);
}
} else {
printf("No resource file found\n\n");
exit(-1);
}
printf("X32 at IP %s\n", S_X32_IP);
printf("REAPER at IP %s\nreceives on port %s\nsends to port %s\n", S_Hst_IP,
S_RecPort, S_SndPort);
printf(
"Flags: verbose: %1d, delay bank: %dms, delay gen.: %dms,Transport: %1d, CHBank: %1d, EQ-UI: %1d, Master: %1d, Bank width: %2d\n",
Xverbose, Xdelayb, Xdelayg, Xtransport_on, Xchbank_on, Xeqctrl_on,
Xmaster_on, bkchsz);
printf(
"Map (min/max): Ch %d/%d, Aux %d/%d, FxR %d/%d, Bus %d/%d, DCA %d/%d, Bus Offset %d\n",
Xtrk_min, Xtrk_max, Xaux_min, Xaux_max, Xfxr_min, Xfxr_max,
Xbus_min, Xbus_max, Xdca_min, Xdca_max, TrackSendOffset);
printf("RDCA Map (min/max):");
for (i = 0; i < 8; i++)
printf(" %d: %d/%d-", i + 1, Rdca_min[i], Rdca_max[i]);
if (Xchbank_on) {
if (Xtransport_on) {
printf("\nCHbank [bankC] up/down: %d %d - bank size: %d\n\n", 9, 10,
bkchsz);
} else {
printf("\nCHbank [bankC] up/down: %d %d - bank size: %d\n\n",
XMbankup, XMbankdn, bkchsz);
}
}
fflush(stdout);
if ((log_file = fopen(".X32Reaper.log", "w")) != NULL) {
fprintf(log_file, "*\n*\n");
fprintf(log_file,
"* X32Reaper Log data - ©2015 - Patrick-Gilles Maillot\n");
fprintf(log_file, "*\n*\n");
MainLoopOn = 1;
// If connected/running, Consume X32 and REAPER messages
if ((Xconnected = X32Connect()) != 0) {
//
// Entering main loop
// run SW as long as needed - i.e. indefinitely in the case of the command line version of the tool
while (MainLoopOn) {
now = time(NULL); // get time in seconds
if (now > before + 9) { // need to keep xremote alive?
Xb_ls = Xsprint(Xb_s, 0, 's', "/xremote");
SEND_TOX(Xdelayg)
before = now;
}
//
// Update on the X32 or Reaper?
FD_ZERO(&fds);
FD_SET(Rfd, &fds);
FD_SET(Xfd, &fds);
Mfd = Rfd + 1;
if (Xfd > Rfd)
Mfd = Xfd + 1;
if (select(Mfd, &fds, NULL, NULL, &timeout) > 0) {
if (FD_ISSET(Rfd, &fds) > 0) {
if ((Rb_lr = recvfrom(Rfd, Rb_r, RBrmax, 0, RFrmIP_pt,
&RFrmIP_len)) > 0) {
// Parse Reaper Messages and send corresponding data to X32
// These can be simple or #bundle messages!
// Can result in several/many messages to X32
// printf("REAPER sent data\n"); fflush(stdout);
if (Xverbose)
Xlogf("R->", Rb_r, Rb_lr);
X32ParseReaperMessage();
}
}
if (FD_ISSET(Xfd, &fds) > 0) {
if ((Xb_lr = recvfrom(Xfd, Xb_r, XBrmax, 0, XX32IP_pt,
&XX32IP_len)) > 0) {
// X32 transmitted something
// Parse and send (if applicable) to REAPER
// printf("X32 sent data\n"); fflush(stdout);
if (Xverbose)
Xlogf("X->", Xb_r, Xb_lr);
X32ParseX32Message();
}
}
}
}
}
} else {
printf("Cannot create log file\n");
MySleep(5000);
}
WSACleanup();
return 0;
}
//---------------------------------------------------------------------------------
//
//
// Private functions:
//
//
int X32Connect() {
int i;
//
p_status = 0;
//
if (Xconnected) {
//
// Signing OFF
Xb_ls = Xsprint(Xb_s, 0, 's', "/unsubscribe");
if (Xverbose)
Xlogf("->X", Xb_s, Xb_ls);
if (sendto(Xfd, Xb_s, Xb_ls, 0, XX32IP_pt, XX32IP_len) < 0)
fprintf(log_file, "Error sending data to X32\n");
WSACleanup();
return 0;
} else {
//
// Initialize winsock / communication with X32 server at IP ip and PORT port
#ifdef __WIN32__
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
fprintf(log_file, "WSA Startup Error\n");
exit(EXIT_FAILURE);
}
#endif
//
// Load the X32 address we connect to; we're a client to X32, keep it simple.
// Create the UDP socket
if ((Xfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(log_file, "X32 socket creation error\n");
WSACleanup();
return 0; // Make sure we don't considered being connected
} else {
// Construct the server sockaddr_in structure
memset(&XX32IP, 0, sizeof(XX32IP)); // Clear struct
XX32IP.sin_family = AF_INET; // Internet/IP
XX32IP.sin_addr.s_addr = inet_addr(S_X32_IP); // IP address
XX32IP.sin_port = htons(atoi("10023")); // X32 port
//
// Non blocking mode; Check for X32 connectivity
Xb_ls = Xsprint(Xb_s, 0, 's', "/info");
if (Xverbose)
Xlogf("->X", Xb_s, Xb_ls);
if (sendto(Xfd, Xb_s, Xb_ls, 0, XX32IP_pt, XX32IP_len) < 0) {
fprintf(log_file, "Error sending data to X32\n");
WSACleanup();
return 0; // Make sure we don't considered being connected
} else {
timeout.tv_sec = 0;
timeout.tv_usec = 100000; //Set timeout for non blocking recvfrom(): 100ms
FD_ZERO(&fds);
FD_SET(Xfd, &fds);
// printf("before select\n"); fflush(stdout);
if ((p_status = select(Xfd + 1, &fds, NULL, NULL, &timeout))
!= -1) {
if (FD_ISSET(Xfd, &fds) > 0) {
// printf("after select\n"); fflush(stdout);
// We have received data - process it!
Xb_lr = recvfrom(Xfd, Xb_r, BSIZE, 0, 0, 0);
if (Xverbose)
Xlogf("X->", Xb_r, Xb_lr);
if (strcmp(Xb_r, "/info") != 0) {
fprintf(log_file,
"X32Connect: Unexpected answer from X32\n");
WSACleanup();
return 0;
}
} else {
// time out... Not connected or Not an X32
fprintf(log_file,
"X32Connect: X32 reception timeout\n");
WSACleanup();
return 0; // Make sure we don't considered being connected
}
} else {
fprintf(log_file, "X32Connect: Polling for data failed\n");
WSACleanup();
return 0; // Make sure we don't considered being connected
}
// Connected! Get the X32 channel ID that's currently selected
// to init the Xselected global variable.
// This is likely to be overwritten when loading REAPER template
Xb_ls = Xsprint(Xb_s, 0, 's', "/-stat/selidx");
SEND_TOX(Xdelayg)
// get data back
FD_ZERO(&fds);
FD_SET(Xfd, &fds);
if ((p_status = select(Xfd + 1, &fds, NULL, NULL, &timeout))
> 0) {
Xb_lr = recvfrom(Xfd, Xb_r, BSIZE, 0, 0, 0);
if (strcmp(Xb_r, "/-stat/selidx") == 0) {
for (i = 0; i < 4; i++)
endian.cc[i] = Xb_r[23 - i];
Xselected = endian.ii + 1;
}
} // ignore errors or timeout (leave Xselected = 1)
}
}
}
// printf("X32 Connected 1\n"); fflush(stdout);
//
// X32 connectivity OK. Set Connection with REAPER as Hst (HOST)
// Connect / Bind HOST
i = 0;
if ((Rfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) >= 0) {
i = 1;
if (setsockopt(Rfd, SOL_SOCKET, SO_REUSEADDR, (char*) &option,
sizeof(option)) >= 0) {
// Construct the server sockaddr_in structure
memset(&RHstIP, 0, sizeof(RHstIP)); /* Clear struct */
memset(&RFrmIP, 0, sizeof(RFrmIP)); /* Clear struct */
RHstIP.sin_family = RFrmIP.sin_family = AF_INET; /* Internet/IP */
RHstIP.sin_addr.s_addr = RFrmIP.sin_addr.s_addr = inet_addr(
S_Hst_IP); /* Reaper IP address */
RFrmIP.sin_port = htons(atoi(S_SndPort)); /* The Reaper port we receive from */
RHstIP.sin_port = htons(atoi(S_RecPort)); /* The Reaper port we send to */
i = 2;
if (bind(Rfd, RFrmIP_pt, sizeof(RFrmIP)) != SOCKET_ERROR) {
timeout.tv_sec = 0;
timeout.tv_usec = 1000; //Set/Change timeout for non blocking recvfrom(): 1ms for Reaper/X32 comm
//
// Init UserCtrl bank C based on Xtransport_on or Xchbank_on values
X32UsrCtrlC();
//
// Cleanup X32 buffers if needed
XRcvClean();
printf("X32 found and connected!\n");
fflush(stdout);
return 1; // We are connected!
}
}
}
// If we're here, there was an error
fprintf(log_file, "Reaper socket error %d\n", i);
WSACleanup();
return 0; // Make sure we don't considered being connected
}
//
// Empty any pending messages from X32 function
//
void XRcvClean() {
//
if (Xverbose)
fprintf(log_file, "X32 receive buffer cleanup if needed\n");
do {
FD_ZERO(&fds);
FD_SET(Xfd, &fds);
if ((p_status = select(Xfd + 1, &fds, NULL, NULL, &timeout)) > 0) {
if ((Xb_lr = recvfrom(Xfd, Xb_r, BSIZE, 0, 0, 0)) > 0) {
if (Xverbose)
Xlogf("X->", Xb_r, Xb_lr);
}
}
} while (p_status > 0); // read and ignore X32 incoming data until
return; // first timeout or error
}
//
//
//
void Xlogf(char *header, char *buf, int len) {
int i, k, n, j, l, comma = 0, data = 0, dtc = 0;
unsigned char c;
fprintf(log_file, "%s, %4d B: ", header, len);
for (i = 0; i < len; i++) {
c = (unsigned char) buf[i];
if (c < 32 || c == 127 || c == 255)
c = '~'; // Manage unprintable chars
fprintf(log_file, "%c", c);
if (c == ',') {
comma = i;
dtc = 1;
}
if (dtc && (buf[i] == 0)) {
data = (i + 4) & ~3;
for (dtc = i + 1; dtc < data; dtc++) {
if (dtc < len) {
fprintf(log_file, "~");
}
}
dtc = 0;
l = data;
while (++comma < l && data < len) {
switch (buf[comma]) {
case 's':
k = (strlen((char*) (buf + data)) + 4) & ~3;
for (j = 0; j < k; j++) {
if (data < len) {
c = (unsigned char) buf[data++];
if (c < 32 || c == 127 || c == 255)
c = '~'; // Manage unprintable chars
fprintf(log_file, "%c", c);
}
}
break;
case 'i':
for (k = 4; k > 0; endian.cc[--k] = buf[data++])
;
fprintf(log_file, "[%6d]", endian.ii);
break;
case 'f':
for (k = 4; k > 0; endian.cc[--k] = buf[data++])
;
if (endian.ff < 10.)
fprintf(log_file, "[%06.4f]", endian.ff);
else if (endian.ff < 100.)
fprintf(log_file, "[%06.3f]", endian.ff);
else if (endian.ff < 1000.)
fprintf(log_file, "[%06.2f]", endian.ff);
else if (endian.ff < 10000.)
fprintf(log_file, "[%06.1f]", endian.ff);
break;
case 'b':
// Get the number of bytes
for (k = 4; k > 0; endian.cc[--k] = buf[data++])
;
n = endian.ii;
// Get the number of data (floats or ints ???) in little-endian format
for (k = 0; k < 4; endian.cc[k++] = buf[data++])
;
if (n == endian.ii) {
// Display blob as string
fprintf(log_file, "%d chrs: ", n);
for (j = 0; j < n; j++)
fprintf(log_file, "%c ", buf[data++]);
} else {
// Display blob as floats
n = endian.ii;
fprintf(log_file, "%d flts: ", n);
for (j = 0; j < n; j++) {
//floats are little-endian format
for (k = 0; k < 4; endian.cc[k++] = buf[data++])
;
fprintf(log_file, "%06.2f ", endian.ff);
}
}
break;
default:
break;
}
}
i = data - 1;
}
}
fprintf(log_file, "\n");
fflush(log_file);
}
//
// XUpdateBkCh():
// This function is called only when switching REAPER channel banks. It maps the values stored
// in the XMbanktracks structure array to the X32 channels 1 to 32 to reflect the values of the
// selected REAPER bank of 32 tracks
void XUpdateBkCh() {
int i, j, src;
char tmp[64];
//
// manage channel select, only for strips 1...32
Rb_ls = Xsprint(Rb_s, 0, 's', "/action/40297"); // unselect all REAPER tracks
SEND_TOR()
if (Xselected < bkchsz && Xtrk_max > 0) {
// i = Xselected;
// Xb_ls = Xfprint(Xb_s, 0, "/-stat/selidx", 'i', &i); //set X32 selected channel
// SEND_TOX(Xdelayg)
Rselected = Xselected + Xchbkof * bkchsz + Xtrk_min;
} else if (Xselected < 32) {
Rselected = Xselected + Xtrk_min;
} else if (Xselected < 40) {
// Auxin section selected
Rselected = Xselected - 32 + Xaux_min;
} else if (Xselected < 48) {
// Fxrtn section selected
Rselected = Xselected - 40 + Xfxr_min;
} else if (Xselected < 64) {
// Mixbus section selected
Rselected = Xselected - 48 + Xbus_min;
}
sprintf(tmp, "/track/%d/select", Rselected);
Rb_ls = Xfprint(Rb_s, 0, tmp, 'f', &fone); // REAPER track select
SEND_TOR()
//
for (i = 1; i < bkchsz + 1; i++) {
sprintf(tmp, "/ch/%02d/", i);
// update the bkchsz channels of X32 upon REAPER bank change requested from X32
src = i - 1 + Xchbkof * bkchsz; // XMbanktracks index start at 0,channel and tracks start at index 1
strcpy(tmp + 7, "mix/fader");
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].fader);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 11, "pan"); // pan
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].pan);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 11, "on"); // mute
j = 1;
if (XMbanktracks[src].mute > 0.5)
j = 0;
Xb_ls = Xfprint(Xb_s, 0, tmp, 'i', &j);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 11, "00/level"); // sends
for (j = 1; j < 17; j++) { // 16 mixbus
tmp[12] = j / 10 + '0';
tmp[13] = j - ((j / 10) * 10) + '0';
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].mixbus[j]);
SEND_TOX(Xdelayb)
}
//
strcpy(tmp + 7, "config/name"); // scribble names
Xb_ls = Xfprint(Xb_s, 0, tmp, 's', XMbanktracks[src].scribble);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 14, "color"); // scribble colors
Xb_ls = Xfprint(Xb_s, 0, tmp, 'i', &XMbanktracks[src].color);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 14, "icon"); // scribble icons
Xb_ls = Xfprint(Xb_s, 0, tmp, 'i', &XMbanktracks[src].icon);
SEND_TOX(Xdelayb)
//
strcpy(tmp + 7, "eq/1/f"); // eq/1/f
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[0]);
SEND_TOX(Xdelayb)
tmp[12] = 'g'; // eq/1/g
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[1]);
SEND_TOX(Xdelayb)
tmp[12] = 'q'; // eq/1/q
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[2]);
SEND_TOX(Xdelayb)
strcpy(tmp + 10, "2/f"); // eq/2/f
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[3]);
SEND_TOX(Xdelayb)
tmp[12] = 'g'; // eq/2/g
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[4]);
SEND_TOX(Xdelayb)
tmp[12] = 'q'; // eq/2/q
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[5]);
SEND_TOX(Xdelayb)
strcpy(tmp + 10, "3/f"); // eq/3/f
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[6]);
SEND_TOX(Xdelayb)
tmp[12] = 'g'; // eq/3/g
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[7]);
SEND_TOX(Xdelayb)
tmp[12] = 'q'; // eq/3/q
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[8]);
SEND_TOX(Xdelayb)
strcpy(tmp + 10, "4/f"); // eq/4/f
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[9]);
SEND_TOX(Xdelayb)
tmp[12] = 'g'; // eq/4/g
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[10]);
SEND_TOX(Xdelayb)
tmp[12] = 'q'; // eq/4/q
Xb_ls = Xfprint(Xb_s, 0, tmp, 'f', &XMbanktracks[src].eq[11]);
SEND_TOX(Xdelayb)
strcpy(tmp + 7, "eq/on"); // eq/on
Xb_ls = Xfprint(Xb_s, 0, tmp, 'i', &XMbanktracks[src].eqon);
SEND_TOX(Xdelayb)
//
j = 0;
if (XMbanktracks[src].solo > 0.5)
j = 1;
sprintf(tmp, "/-stat/solosw/%02d", i); // solo
Xb_ls = Xfprint(Xb_s, 0, tmp, 'i', &j);
SEND_TOX(Xdelayg)
}
}
//
// This function initializes User Assign section C either with all transport options, or
// if channel banks are enabled, it sets two of the transport buttons to manage up/down.
// If transport is off and channel banks are enable, two user chosen buttons are used
// from User Assign section C to manage bank up/down
void X32UsrCtrlC() {
int i;
char* MP[4] = { "MP13000", "MP14000", "MP15000", "MP16000" };
char* MN[8] = { "MN16000", "MN16001", "MN16002", "MN16003", "MN16004",
"MN16005", "MN16006", "MN16007" };
//
if (Xtransport_on) {
//
// Encoders
for (i = 1; i < 5; i++) {
sprintf(Xb_r, "/config/userctrl/C/enc/%d", i);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 's', MP[i - 1]);
SEND_TOX(Xdelayg)
}
//
// Buttons
for (i = 5; i < 13; i++) {
sprintf(Xb_r, "/config/userctrl/C/btn/%d", i);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 's', MN[i - 5]);
SEND_TOX(Xdelayg)
}
//
// Set X32 Bank C Encoders to center "64" value
for (i = 33; i < 37; i++) {
sprintf(Xb_r, "/-stat/userpar/%2d/value", i);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 'i', &six4);
SEND_TOX(Xdelayg)
}
//
// Set X32 Bank C buttons to "0" value
for (i = 17; i < 25; i++) {
sprintf(Xb_r, "/-stat/userpar/%2d/value", i);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 'i', &zero);
SEND_TOX(Xdelayg)
}
} else {
if (XMkerbt_on) {
//
// update/change REAPER Marker button.
//
sprintf(Xb_r, "/config/userctrl/C/btn/%d", XMkerbtn);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 's', MN[XMkerbtn - 5]);
SEND_TOX(Xdelayg)
sprintf(Xb_r, "/-stat/userpar/%2d/value", 12 + XMkerbtn);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 'i', &zero);
SEND_TOX(Xdelayg)
}
//
if (Xchbank_on) {
//
// Only update/change bank up and bank down buttons.
//
// bank up
sprintf(Xb_r, "/config/userctrl/C/btn/%d", XMbankup);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 's', MN[XMbankup - 5]);
SEND_TOX(Xdelayg)
sprintf(Xb_r, "/-stat/userpar/%2d/value", 12 + XMbankup);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 'i', &zero);
SEND_TOX(Xdelayg)
//
// bank down
sprintf(Xb_r, "/config/userctrl/C/btn/%d", XMbankdn);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 's', MN[XMbankdn - 5]);
SEND_TOX(Xdelayg)
sprintf(Xb_r, "/-stat/userpar/%2d/value", 12 + XMbankdn);
Xb_ls = Xfprint(Xb_s, 0, Xb_r, 'i', &zero);
SEND_TOX(Xdelayg)
}
}
if (Xtransport_on || XMkerbt_on || Xchbank_on) {
// Color : XbankCcol
Xb_ls = Xfprint(Xb_s, 0, "/config/userctrl/C/color", 'i', &XbankCcol);
SEND_TOX(Xdelayg)
// Select X32 UserAssign Bank C
Xb_ls = Xfprint(Xb_s, 0, "/-stat/userbank", 'i', &two);
SEND_TOX(Xdelayg)
}
//
// Finally, if we're connected and the CH bank flag is set,
// set current bank values to X32
if (Xchbank_on)
XUpdateBkCh();
return;
}
// Manage X32 ...eq/on messages
void X32_eqon(int Xb_i, int bank, int cnum, int cnum1) {
int i;
char tmp[48];
//
Xb_i += 5; //skip "/eq/on" string
// /xxx/%02d/eq/on..,i..[0|1]
while (Xb_r[Xb_i] != ',')
Xb_i += 1;
Xb_i += 4;
for (i = 4; i > 0; endian.cc[--i] = Xb_r[Xb_i++])
;
sprintf(tmp, "/track/%d/fx/1/bypass", cnum1);
if (bank)
XMbanktracks[cnum - 1].eqon = endian.ii;
endian.ff = (float) endian.ii;
Rb_ls = Xfprint(Rb_s, 0, tmp, 'f', &endian.ff);
return;
}
//
// Manage X32 ...eq/x/f messages
void X32_eqfr(int Xb_i, int bank, int cnum, int cnum1) {
int i, j;
char tmp[64];
// get eq#
j = (int) (Xb_r[Xb_i + 3] - '1') * 3;
Xb_i += 5; //skip "/eq/x/f" string
// /ch/%02d/eq/*/f..,f..[float]
while (Xb_r[Xb_i] != ',')
Xb_i += 1;
Xb_i += 4;
for (i = 4; i > 0; endian.cc[--i] = Xb_r[Xb_i++])
;
// get Behringer freq value
endian.ff = exp(endian.ff * 6.907755279 + 2.995732274);
// set REAPER float value
endian.ff = log((endian.ff - 20.) * 400. / (23980.) + 1) / 5.991464547;
sprintf(tmp, "/track/%d/fx/1/fxparam/%d/value", cnum1, j + 1);
if (bank)
XMbanktracks[cnum - 1].eq[j] = endian.ff;
Rb_ls = Xfprint(Rb_s, 0, tmp, 'f', &endian.ff);
return;
}
//
// Manage X32 ...eq/x/g or ...eq/x/q messages
void X32_eqgq(int Xb_i, int bank, int cnum, int cnum1, int index) {
int i, j;
char tmp[64];
// get eq#
j = (int) (Xb_r[Xb_i + 3] - '1') * 3;
Xb_i += 5; //skip "/eq/x/g" string
// /.../%02d/eq/*/g|q..,f..[float]
while (Xb_r[Xb_i] != ',')
Xb_i += 1;
Xb_i += 4;
for (i = 4; i > 0; endian.cc[--i] = Xb_r[Xb_i++])
;
sprintf(tmp, "/track/%d/fx/1/fxparam/%d/value", cnum1, j + index);
if (bank)
XMbanktracks[cnum - 1].eq[j] = endian.ff;
Rb_ls = Xfprint(Rb_s, 0, tmp, 'f', &endian.ff);
return;
}
//--------------------------------------------------------------------
// X32 Messages data parsing
//
// Analysis of incoming X32 data, and sending respective commands to REAPER in order
// to synchronize X32 and REAPER. Data controlled includes fader, pan, mute, solo,
// select, scribble, icon, color, bus sends fader and pan.
//
void X32ParseX32Message() {
int Xb_i = 0;
int Xb_ls = 0;
int cnum, bus, dca, i;
int cnum1;
char tmp[32];
//
// What is the X32 message made of?
// X32 format is:
// /ch/%02d/mix/pan......,f..[float] %02d = 01..bkchsz
// /ch/%02d/mix/fader....,f..[float] %02d = 01..bkchsz
// /ch/%02d/mix/on.......,i..[0/1] %02d = 01..bkchsz
// /ch/%02d/config/name..,s..[string\0] %02d = 01..bkchsz
// /ch/%02d/mix/%02d/level...,f..[float] %02d = 01..bkchsz / %02d = 01..16
// /ch/%02d/eq/on........,i..[0/1] %02d = 01..bkchsz
// /ch/%02d/eq/x/f.......,f..[float] %02d = 01..bkchsz, x:1..4
// /ch/%02d/eq/x/g.......,f..[float] %02d = 01..bkchsz, x:1..4
// /ch/%02d/eq/x/q.......,f..[float] %02d = 01..bkchsz, x:1..4
//
// Same applies to /auxin and /fxrtn as for /ch above
//
// /-stat/selidx.........,i..[%d]
// /-stat/solosw/%02d....,i..[0/1]
//
// /main/st/mix/fader....,f..[float]
// /main/st/mix/pan......,f..[float]
//
// /bus/%02d/mix/pan......,f..[float] %02d = 01..16
// /bus/%02d/mix/fader....,f..[float] %02d = 01..16
// /bus/%02d/mix/on.......,i..[0/1] %02d = 01..16
// /bus/%02d/config/name..,s..[string\0] %02d = 01..16
//
// /dca/1..8/on...........,i..[0/1]
// /dca/1..8/fader........,f..[0..1.0]
// /dca/1..8/config/name..,s..[string\0]
//
// if transport is on:
// Bank C Actuators
// 33 34 35 36
// Beat Measure Marker Item
//
// Bank C Buttons
// /-stat/userpar/%2d/value~,i~~[0 or 127] %2d = 17..24
//
// 17 18 19 20
// REW PLAY PAUSE FF
//
// 21 22 23 24
// S/S loop Repeat STOP REC