-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
5555 lines (4685 loc) · 301 KB
/
main.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
/* ########################################################### */
/* Programmer: Stephan Allene <[email protected]> */
/* */
/* Project: Ui MPC interface Midi Controler */
/* */
/* ########################################################### */
/* standard include */
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netdb.h> /* gethostbyname */
/* special include */
#include <alsa/asoundlib.h> /* Interface to the ALSA system */
#include <netinet/in.h>
#include <arpa/inet.h>
#include "includes/version.h"
#include "includes/ui.h"
#include "includes/b64.h"
#include "includes/readconfig.h"
/* define */
#define PORT 80
#define SOCKET_ERROR -1
#define TRUE = 1
#define FALSE = 0
#define KORG
#define FILENAME "/home/pi/UI24r-Midi/ui2mcp/config.conf"
//#define MAXBUF 1024
#define DELIM "="
int debug = 0;
int autoshutdown = 0;
unsigned short stop = 0; // Flag of program stop, default value is 0.
FILE *hfErr; // Declaration for log file.
char sa_LogMessage[262145] = ""; // Variable for log message.
static char *send_hex; // Variable for SysEx message in hexa.
static char *send_data; // Variable for SysEx message.
static int send_data_length; // Variable for len of SysEx message.
static int sysex_interval;
static snd_rawmidi_t* midiout = NULL;
static snd_rawmidi_t* midiin = NULL;
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* Function */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* get signal -- stop =1 when SIGINT Ctl^c. */
void signalHandler(int signal){
if(signal==SIGINT || signal==SIGKILL){
static unsigned short nbSignal = 0;
nbSignal++;
printf("Signal Ctrl^c n°%i reçu\n",nbSignal);
if(nbSignal==1) stop=1;
}
}
/* error -- Print an error message. */
void errormessage(const char *format, ...){
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
putc('\n', stderr);
}
/* Function for export message. */
void LogTrace(FILE *p_File, int loglevel, int debug, char *p_Trace, ...){
if( debug == 0 ){
if( loglevel == 0 ){
printf("%s",p_Trace);
}
}
else if ( debug > 0 ){
if( loglevel <= debug && loglevel != 0 ){
if( loglevel == 0 ){fprintf(p_File,"%s", p_Trace);}
if( loglevel == 1 ){fprintf(p_File,"%s", p_Trace);}
if( loglevel == 2 ){fprintf(p_File,"%s", p_Trace);}
if( loglevel == 3 ){fprintf(p_File,"%s", p_Trace);}
if( loglevel == 4 ){fprintf(p_File,"%s", p_Trace);}
if( loglevel == 5 ){fprintf(p_File,"%s", p_Trace);}
}
else if ( loglevel == 0 ){
fprintf(p_File,"%s", p_Trace);
printf("%s",p_Trace);
}
}
return ;
}
/* give time in miliseconds. */
long currentTimeMillis(){
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec * 1000 + time.tv_usec / 1000;
}
/* split char with demiliter like Perl function. */
char** split(char* chaine,const char* delim,int vide){
char** tab=NULL; //tableau de chaine, tableau resultat
char *ptr; //pointeur sur une partie de
int sizeStr; //taille de la chaine à recupérer
int sizeTab=0; //taille du tableau de chaine
char* largestring; //chaine à traiter
int sizeDelim=strlen(delim); //taille du delimiteur
largestring = chaine; //comme ca on ne modifie pas le pointeur d'origine
//(faut ke je verifie si c bien nécessaire)
while( (ptr=strstr(largestring, delim))!=NULL ){
sizeStr=ptr-largestring;
//si la chaine trouvé n'est pas vide ou si on accepte les chaine vide
if(vide==1 || sizeStr!=0){
//on alloue une case en plus au tableau de chaines
sizeTab++;
tab= (char**) realloc(tab,sizeof(char*)*sizeTab);
//on alloue la chaine du tableau
tab[sizeTab-1]=(char*) malloc( sizeof(char)*(sizeStr+1) );
strncpy(tab[sizeTab-1],largestring,sizeStr);
tab[sizeTab-1][sizeStr]='\0';
}
//on decale le pointeur largestring pour continuer la boucle apres le premier elément traiter
ptr=ptr+sizeDelim;
largestring=ptr;
}
//si la chaine n'est pas vide, on recupere le dernier "morceau"
if(strlen(largestring)!=0){
sizeStr=strlen(largestring);
sizeTab++;
tab= (char**) realloc(tab,sizeof(char*)*sizeTab);
tab[sizeTab-1]=(char*) malloc( sizeof(char)*(sizeStr+1) );
strncpy(tab[sizeTab-1],largestring,sizeStr);
tab[sizeTab-1][sizeStr]='\0';
}
else if(vide==1){ //si on fini sur un delimiteur et si on accepte les mots vides,on ajoute un mot vide
sizeTab++;
tab= (char**) realloc(tab,sizeof(char*)*sizeTab);
tab[sizeTab-1]=(char*) malloc( sizeof(char)*1 );
tab[sizeTab-1][0]='\0';
}
//on ajoute une case à null pour finir le tableau
sizeTab++;
tab= (char**) realloc(tab,sizeof(char*)*sizeTab);
tab[sizeTab-1]=NULL;
return tab;
}
/* slice char with begin and len like Perl function. */
char* substring(const char* str, size_t begin, size_t len)
{
if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin+len))
return 0;
return strndup(str + begin, len);
}
/* Memory allocation. */
static void *my_malloc(size_t size){
void *p = malloc(size);
if (!p) {
errormessage("out of memory");
exit(EXIT_FAILURE);
}
return p;
}
/* Send SysEx function. */
static int send_midi_interleaved(void){
int err;
char *data = send_data;
size_t buffer_size;
snd_rawmidi_params_t *param;
snd_rawmidi_status_t *st;
snd_rawmidi_status_alloca(&st);
snd_rawmidi_params_alloca(¶m);
snd_rawmidi_params_current(midiout, param);
buffer_size = snd_rawmidi_params_get_buffer_size(param);
while (data < (send_data + send_data_length)) {
int len = send_data + send_data_length - data;
char *temp;
if (data > send_data) {
snd_rawmidi_status(midiout, st);
do {
/* 320 µs per byte as noted in Page 1 of MIDI spec */
usleep((buffer_size - snd_rawmidi_status_get_avail(st)) * 320);
snd_rawmidi_status(midiout, st);
} while(snd_rawmidi_status_get_avail(st) < buffer_size);
usleep(sysex_interval * 1000);
}
/* find end of SysEx */
if ((temp = memchr(data, 0xf7, len)) != NULL)
len = temp - data + 1;
if ((err = snd_rawmidi_write(midiout, data, len)) < 0)
return err;
data += len;
}
return 0;
}
/* Convert ui[].MidiMix to db. */
static float linear_to_db(float c){
float db0 = .7647058823529421;
if (c == db0) return 0;
else if (c > db0) return (pow(10,c) - pow(10,db0)) / 0.42;
else if (c > 0.26 && c < db0) return (65.7*log10(c) + (db0*10))+0.6;
else if (c < 0.16 && c < db0) return (65.7*log10(c) + (db0*10))+5.0;
else if (c < 0.26 && c < db0) return (65.7*log10(c) + (db0*10))+0.8;
errormessage("invalid value %f", c);
return -1;
}
/* Convert char to hexa value. */
static int hex_value(char c){
if ('0' <= c && c <= '9')
return c - '0';
if ('A' <= c && c <= 'F')
return c - 'A' + 10;
if ('a' <= c && c <= 'f')
return c - 'a' + 10;
errormessage("invalid character %c", c);
return -1;
}
/* Create value for SysEx message. */
static void parse_data(void){
const char *p;
int i, value;
send_data = my_malloc(strlen(send_hex)); /* guesstimate */
i = 0;
value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
for (p = send_hex; *p; ++p) {
int digit;
if (isspace((unsigned char)*p)) {
if (value >= 0) {
send_data[i++] = value;
value = -1;
}
continue;
}
digit = hex_value(*p);
if (digit < 0) {
send_data = NULL;
return;
}
if (value < 0) {
value = digit;
} else {
send_data[i++] = (value << 4) | digit;
value = -1;
}
}
if (value >= 0)
send_data[i++] = value;
send_data_length = i;
}
/* Concate data for SysEx massage. */
static void add_send_hex_data(const char *str){
sprintf(sa_LogMessage,"UI2MCP --> MIDI : STR Hex : (%s)\n", str);
LogTrace(hfErr, 5, debug, sa_LogMessage);
int length;
char *s;
length = (send_hex ? strlen(send_hex) + 1 : 0) + strlen(str) + 1;
//printf("lenght %i %i\n", strlen(str), length);
s = my_malloc(length);
if (send_hex) {
strcpy(s, send_hex);
strcat(s, " ");
} else {
s[0] = '\0';
}
strcat(s, str);
free(send_hex);
send_hex = s;
}
/* Send midi out SysEx message. */
static void SendSysExTextOut(snd_rawmidi_t *hwMidi, char *SysExHdr, char *SysExCmd, char *Text){
int status;
char SysExEox[3] = "F7";
char Buff[1024];
Buff[0] = '\0';
unsigned BuffH[1024];
BuffH[0] = '\0';
int i,j;
strcpy(Buff, SysExHdr);
strcat(Buff, SysExCmd);
//printf("%s\n", Text);
if( strlen(Text) != 0 ){
/*set strH with nulls*/
memset(BuffH,0,sizeof(BuffH));
/*converting str character into Hex and adding into strH*/
for(i=0,j=0;i<strlen(Text);i++,j+=2)
{
sprintf((char*)BuffH+j,"%02X",Text[i]);
}
BuffH[j]='\0'; /*adding NULL in the end*/
strcat(Buff, (char*)BuffH);
}
strcat(Buff, SysExEox);
add_send_hex_data(Buff);
parse_data();
send_hex[0] = '\0';
//send_data[0] = '\0';
//send_data_length = 0;
if ((status = send_midi_interleaved()) < 0) {
errormessage("cannot send data: %s", snd_strerror(status));
exit(1);
}
}
/* Send midi out message. */
void SendMidiOut(snd_rawmidi_t *hwMidi, char *MidiOut){
int status;
sprintf(sa_LogMessage,"UI2MCP --> MIDI : Midi Out : %02X %02X %02X\n", MidiOut[0], MidiOut[1], MidiOut[2]);
LogTrace(hfErr, 5, debug, sa_LogMessage);
if ((status = snd_rawmidi_write(hwMidi, MidiOut, sizeof(MidiOut))) < 0){
errormessage("Problem writing to MIDI output: %s", snd_strerror(status));
exit(1);
}
usleep(320);
}
/* Send message to LCD in text mode. */
void SendLCDTxt(snd_rawmidi_t *hwMidi, char *SysExHdr, int Inv, int i_RowLCD, char *Text){
char Cmd[32] = "";
char c_Canal[32] = "";
int col = 0;
int i_SliceStep = 13;
//int i_RowLCD = 1;
for (int j = 0; j < strlen(Text); j +=i_SliceStep){
strcpy(Cmd, "12");
sprintf(c_Canal, "%02X", col);
strcat(Cmd, c_Canal);
sprintf(c_Canal, "%02X", i_RowLCD);
strcat(Cmd, c_Canal);
if( Inv == 0 ){
strcat(Cmd, "01");
}
if( Inv == 1 ){
strcat(Cmd, "05");
}
char* substr;
if ( strlen(Text) - j <= i_SliceStep ){
substr = substring(Text, j, strlen(Text) - j);
}
else{
substr = substring(Text, j, i_SliceStep);
}
SendSysExTextOut(midiout, SysExHdr, Cmd, substr);
col++;
free(substr);
}
}
/* LCD function for row navigation on explorer. */
void LcdExplorerUpdate(snd_rawmidi_t *hwMidi, char *SysExHdr, int Index, char (*List)[2048][256]){
int Row = 0;
//printf("--------------------------------------------------------------------------------\n");
for (int c = 0 + 6 * (int)floor((double)Index/6); c < 6 + 6 * (int)floor((double)Index/6); c++){
if( Index != c ){
SendLCDTxt(hwMidi, SysExHdr, 0, Row, (*List)[c]);
//printf("LCD Explorer %i %i [%s]\n", c, Index, (*List)[c]);
}
else{
SendLCDTxt(hwMidi, SysExHdr, 1, Row, (*List)[c]);
//printf("LCD Explorer %i %i [\"%s\"]\n", c, Index, (*List)[c]);
}
Row++;
}
//printf("--------------------------------------------------------------------------------\n");
}
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
/* main code */
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
int main(int argc, char *argv[]) {
//argv = malloc(sizeof(argc));
//argv = malloc(argv sizeof argv);
for (int x = 0; x < argc; ++x){
if(strcmp(argv[x],"-h") == 0 || strcmp(argv[x],"--help") == 0){
printf ("Usage: ui2mcp options\n\n");
printf ("-h, --help this help\n");
printf ("-v, --version print current version\n");
printf ("-s, --shutdown shutdown hardware after shutdown softwaren\n");
printf ("-d, --debug [level] debug information in the ui2mcp.log file\n");
printf (" - 1 Action information\n");
printf (" - 2 Translated information\n");
printf (" - 3 Transmit message information\n");
printf (" - 4 Low level information\n");
printf (" - 5 Very low level information like websocket\n");
exit(0);
}else if (strcmp(argv[x],"-v") == 0 || strcmp(argv[x],"--version") == 0){
printf ("Version %s\n", FULLVERSION_STRING);
exit(0);
}else if (strcmp(argv[x],"-s") == 0 || strcmp(argv[x],"--shutdown") == 0){
printf ("Shutdown actived\n");
autoshutdown++;
}else if(strcmp(argv[x],"-d") == 0 || strcmp(argv[x],"--debug") == 0){
printf ("Debug actived\n");
debug++;
if ( argc >= 3 ){
if ( strlen(argv[x+1]) != 0 ){
debug = atoi(argv[x+1]);
printf ("Log level: %i\n", debug);
}
}
}
}
/* time variable for led bliking Tap button */
int status = 0;
int current_time_init;
int current_time;
/* variable for Tap function */
int tap[255];
int i_NbTap = 0;
/* variable for watchdog function */
int i_Watchdog_init;
int i_Watchdog;
/* variable for Update timer function */
int i_UpdateListTimer;
/* variable for Vu meter function */
int i_VuMeter_init;
int i_VuMeter;
char c_StxBuffer[1024] = "";
int init = -1;
/* file open for log information */
if ((hfErr = fopen("/tmp/ui2mcp.log","w" )) == (FILE *)NULL){
errormessage("\nError opening log file\n");
exit(1);
}
LogTrace(hfErr, 0, debug, "### Starting to UI to Midi Controler... ###\n" );
struct config ControlerConfig;
ControlerConfig = get_config(FILENAME);
char *ControlerName = ControlerConfig.ControlerName;
char *ControlerMode = ControlerConfig.ControlerMode;
int Lcd = atoi(ControlerConfig.Lcd);
sprintf(sa_LogMessage,"UI2MCP <-- CONFIG : Controler Midi : (%s) Protocol : (%s)\n", ControlerName, ControlerMode);
LogTrace(hfErr, 0, debug, sa_LogMessage);
// Midi connexion variable
const char* portname = "";
portname = ControlerConfig.MidiPort;
sprintf(sa_LogMessage,"UI2MCP <-- CONFIG : Midi Port (%s)\n", portname);
LogTrace(hfErr, 0, debug, sa_LogMessage);
int mode = SND_RAWMIDI_NONBLOCK; // SND_RAWMIDI_SYNC
//snd_rawmidi_t* midiout = NULL;
//snd_rawmidi_t* midiin = NULL;
// unsigned char Midibuffer[2048] = ""; // Storage for input buffer received
unsigned char Midibuffer[3] = ""; // Storage for input buffer received
int InMidi, MidiValue, MidiCC;
// Network websocket
//struct sockaddr_in address;
int sock = 0;
struct sockaddr_in serv_addr;
//typedef struct in_addr IN_ADDR;
char *ack = "GET /raw HTTP1.1\n\n";
char *command = NULL;
char buffer[1024] = "";
int AdrSplitComa, ArraySplitDot;
char** SplitArrayComa = NULL;
char** SplitArrayDot = NULL;
/* UI device variable for number per type of channel */
// TODO (pi#1#12/28/18): Improve automaticly with UI model
int UIChannel = 24;
int UILineIn = 2;
int UIMedia = 2;
int UIFx = 4;
int UISubGroup = 6;
int UIAux = 10;
int UIVca = 6;
int UIMaster = 2;
int UIAllStrip = 56;
/* structure variable for UI */
struct Ui ui[UIAllStrip];
// Test ui[0].Aux[0].Mute = 1;
// Test ui[0].Fx[3].MixMidi = 0.341;
/* Configuration of default color for UI channel */
int Numb = 0;
for(int c = 0; c < UIChannel; c++){
ui[c].Color = 0;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "i");
}
Numb = 0;
for(int c = UIChannel; c < UIChannel+UILineIn; c++){
ui[c].Color = 3;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "l");
}
Numb = 0;
for(int c = UIChannel+UILineIn; c < UIChannel+UILineIn+UIMedia; c++){
ui[c].Color = 11;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "p");
}
Numb = 0;
for(int c = UIChannel+UILineIn+UIMedia; c < UIChannel+UILineIn+UIMedia+UIFx; c++){
ui[c].Color = 7;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "f");
}
Numb = 0;
for(int c = UIChannel+UILineIn+UIMedia+UIFx; c < UIChannel+UILineIn+UIMedia+UIFx+UISubGroup; c++){
ui[c].Color = 8;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "s");
}
Numb = 0;
for(int c = UIChannel+UILineIn+UIMedia+UIFx+UISubGroup; c < UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux; c++){
ui[c].Color = 5;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "a");
}
Numb = 0;
for(int c = UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux; c < UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca; c++){
ui[c].Color = 6;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "v");
}
Numb = 0;
for(int c = UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca; c < UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca+UIMaster; c++){
ui[c].Color = 2;
ui[c].Position = c;
ui[c].Numb = Numb++;
strcpy(ui[c].Type, "m");
strcpy(ui[c].Name, "MASTER");
}
/* variable for UI websocket message */
char UImsg[64] = "";
char UIio[64] = "";
char UIfunc[64] = "";
char UI1func[64] = "";
char UI2func[64] = "";
char UIval[64] = "";
char UIchan[24] = "";
//float zeroDbPos = .7647058823529421; // 0 db
//float maxDb = 1; // +10 db
char c_SyncId[256];
sscanf(ControlerConfig.SyncId, "%s", c_SyncId);
sprintf(sa_LogMessage,"UI2MCP <-- CONFIG : UI SyncId (%s)\n", c_SyncId);
LogTrace(hfErr, 0, debug, sa_LogMessage);
char *UIModel = NULL;
char UIFirmware[32] = "";
char *UICommand = NULL;
//UI MuteMask
char UIMuteMask[16] = "";
unsigned GroupMaskMute[6];
unsigned GroupMaskMuteAll;
unsigned GroupMaskMuteFx;
//UI MTK Session
char UIMtkSessionList [2048][256];
int MtkSessionIndex = 0;
int MtkSessionMax = 0;
char MtkSessionCurrent[256] = "";
//UI Directories Player
char UIDirPlayerList [2048][256];
int DirPlayerIndex = 0;
int DirPlayerMax = 0;
char DirPlayerCurrent[256] = "";
//UI Files Player
char UIFilesPlayerList [2048][256];
int FilesPlayerIndex = 0;
int FilesPlayerMax = 0;
char FilesPlayerCurrent[256] = "";
//UI Shows
char UIShowsList [2048][256];
int ShowsIndex = 0;
int ShowsMax = 0;
char ShowsCurrent[256] = "";
//UI SnapShot
char UISnapShotList [2048][256];
int SnapShotIndex = 0;
int SnapShotMax = 0;
char SnapShotCurrent[256] = "";
//UI Cues
char UICuesList [2048][256];
int CuesIndex = 0;
int CuesMax = 0;
char CuesCurrent[256] = "";
// Other Setting variable
int SoloMode = 0;
int UIBpm = 120;
int delay = 60000/UIBpm;
// Common variable
int AddrMidiTrack = 0; // value of translation fader
int Canal;
int MtkCurrentState = -1; // Value for Mtk STOP/PLAY
int MediaPlayerCurrentState = -1; // Value for Media Player STOP/PLAY
int MtkRecCurrentState = -1; // Value for Mtk REC
int MediaRecCurrentState = 0; //-1; // Value for MEDIA REC
int DimMaster; // Value for Dim Master
int SoundCheck = -1; // varaible for Sound Chack function
// Parameter of MIDI device
//int MidiValueOn = 0x7F;
//int MidiValueOff = 0x00;
// Parameter of MIDI device
int NbMidiFader = atoi(ControlerConfig.NbMidiFader);
int NbMidiTrack = UIAllStrip/NbMidiFader; // Number by modulo of number of Midi channel
int MixPressed = 1; // Variable activate All mode
int PanPressed = 0; // Variable activate Pan mode
int AuxPressed = 0; // Variable activate Aux mode
int FxPressed = 0; // Variable activate Fx mode
int InputsPressed = 0; // Variable activate Inputs mode
int ShiftLeftPressed = 0;
int ShiftRightPressed = 0;
int SelectButtonPressed = -1;
int ModeShowsPressed = 0;
int ModeSnapShotsPressed = 0;
int ModeCuesPressed = 0;
int ModeMasterPressed = 0;
int ModeDirPlayerPressed = 0;
int ModeFilesPlayerPressed = 0;
int ModePlayer = 0;
int ModeMtkSessionsPressed = 0;
//mapping midi controler to UI for fader 0
// Fader = Ex ll hh
int AddrMidiMix = 0;
sscanf(ControlerConfig.AddrMidiMix, "%x", &AddrMidiMix);
// Encoder = B0 10 xx
int AddrMidiEncoder = 0; //0xB0;
sscanf(ControlerConfig.AddrMidiEncoder, "%x", &AddrMidiEncoder);
int AddrMidiEncoderSession = 0;
sscanf(ControlerConfig.AddrMidiEncoderSession, "%x", &AddrMidiEncoderSession);
int AddrMidiEncoderPan = 0; //0x10;
sscanf(ControlerConfig.AddrMidiEncoderPan, "%x", &AddrMidiEncoderPan);
int NbPanButton = atoi(ControlerConfig.NbPanButton);
int NbPanStep[UIAllStrip];
sprintf(sa_LogMessage,"UI2MCP <-- CONFIG : Pan type : (%s)\n", ControlerConfig.TypePan);
LogTrace(hfErr, 0, debug, sa_LogMessage);
// Button/Led 90 ID CC
int AddrMidiButtonLed = 0; //0x90;
sscanf(ControlerConfig.AddrMidiButtonLed, "%x", &AddrMidiButtonLed);
int AddrMidiRec = -1; //0x00;
sscanf(ControlerConfig.AddrMidiRec, "%x", &AddrMidiRec);
int NbRecButton = atoi(ControlerConfig.NbRecButton);
int ArmRecAll = 0;
int AddrMidiMute = 0; //0x10;
sscanf(ControlerConfig.AddrMidiMute, "%x", &AddrMidiMute);
int AddrMidiSolo = 0; //0x08;
sscanf(ControlerConfig.AddrMidiSolo, "%x", &AddrMidiSolo);
int AddrMidiMaster = 0;
sscanf(ControlerConfig.AddrMidiMaster, "%x", &AddrMidiMaster);
int AddrMidiSelect = 0; //0x18;
sscanf(ControlerConfig.AddrMidiSelect, "%x", &AddrMidiSelect);
//printf("%02X\n",AddrMidiSelect);
int AddrMidiParamButton = 0; //0x20;
sscanf(ControlerConfig.AddrMidiParamButton, "%x", &AddrMidiParamButton);
int AddrMidiSessionButton = 0; //0x20;
sscanf(ControlerConfig.AddrMidiSessionButton, "%x", &AddrMidiSessionButton);
int AddrMuteClear = 0; //0x02;
sscanf(ControlerConfig.AddrMuteClear, "%x", &AddrMuteClear);
int AddrMuteSolo = 0; //0x01;
sscanf(ControlerConfig.AddrMuteSolo, "%x", &AddrMuteSolo);
int AddrShiftLeft = 0; //0x46;
sscanf(ControlerConfig.AddrShiftLeft, "%x", &AddrShiftLeft);
int AddrShiftRight = 0; //0x06;
sscanf(ControlerConfig.AddrShiftRight, "%x", &AddrShiftRight);
int AddrMidiTouch = 0;// = 0x68;
sscanf(ControlerConfig.AddrMidiTouch, "%x", &AddrMidiTouch);
//printf("%02X\n",AddrMidiButtonLed);
int IdTrackPrev = 0; //0x2E; /* Generic ID */
sscanf(ControlerConfig.IdTrackPrev, "%x", &IdTrackPrev);
int IdTrackNext = 0; //0x2F; /* Generic ID */
sscanf(ControlerConfig.IdTrackNext, "%x", &IdTrackNext);
int IdLoop = 0; //0x56; /* Generic ID */
sscanf(ControlerConfig.IdLoop, "%x", &IdLoop);
int IdMarkerSet = 0; //0x59;
sscanf(ControlerConfig.IdMarkerSet, "%x", &IdMarkerSet);
int IdMarkerLeft = 0; //0x58;
sscanf(ControlerConfig.IdMarkerLeft, "%x", &IdMarkerLeft);
int IdMarkerRight = 0; //0x5A;
sscanf(ControlerConfig.IdMarkerRight, "%x", &IdMarkerRight);
int IdRewind = 0; //0x5B; /* Generic ID */
sscanf(ControlerConfig.IdRewind, "%x", &IdRewind);
int IdForward = 0; //0x5C; /* Generic ID */
sscanf(ControlerConfig.IdForward, "%x", &IdForward);
int IdStop = 0; //0x5D; /* Generic ID */
sscanf(ControlerConfig.IdStop, "%x", &IdStop);
int IdPlay = 0; //0x5E; /* Generic ID */
sscanf(ControlerConfig.IdPlay, "%x", &IdPlay);
int IdRec = 0; //0x5F; /* Generic ID */
sscanf(ControlerConfig.IdRec, "%x", &IdRec);
// Value Bar for FaderPort
int AddrMidiBar = 0; //0xB0;
sscanf(ControlerConfig.AddrMidiBar, "%x", &AddrMidiBar);
int AddrMidiValueBar = 0; //0x30;
sscanf(ControlerConfig.AddrMidiValueBar, "%x", &AddrMidiValueBar);
// SysExHdr for FaderPort
char SysExHdr[11] = "";
strcpy(SysExHdr, ControlerConfig.SysExHdr);
// printf("%s",ControlerConfig.i_Tap);
// printf("%s",ControlerConfig.i_Dim);
// printf("%s",ControlerConfig.i_SnapShotNavUp);
// printf("%s",ControlerConfig.i_SnapShotNavDown);
// printf("\n");
/* Link between button & led and ID */
int i_Tap = 0; //IdLoop;
sscanf(ControlerConfig.i_Tap, "%x", &i_Tap);
int i_Dim = 0; //IdMarkerSet;
sscanf(ControlerConfig.i_Dim, "%x", &i_Dim);
int i_SnapShotNavUp = 0; //IdMarkerLeft;
sscanf(ControlerConfig.i_SnapShotNavUp, "%x", &i_SnapShotNavUp);
int i_SnapShotNavDown = 0; //IdMarkerRight;
sscanf(ControlerConfig.i_SnapShotNavDown, "%x", &i_SnapShotNavDown);
int i_StopUI2Mcp = 0; //IdRewind;
sscanf(ControlerConfig.i_StopUI2Mcp, "%x", &i_StopUI2Mcp);
int i_Validation = 0; //IdForward;
sscanf(ControlerConfig.i_Validation, "%x", &i_Validation);
int AddrSoundCheck = 0; //IdForward;
sscanf(ControlerConfig.AddrSoundCheck, "%x", &AddrSoundCheck);
int AddrShowsSelect = 0;
sscanf(ControlerConfig.AddrShowsSelect, "%x", &AddrShowsSelect);
int AddrSnapShotsSelect = 0;
sscanf(ControlerConfig.AddrSnapShotsSelect, "%x", &AddrSnapShotsSelect);
int AddrCuesSelect = 0;
sscanf(ControlerConfig.AddrCuesSelect, "%x", &AddrCuesSelect);
int AddrMediaSelect = 0;
sscanf(ControlerConfig.AddrMediaSelect, "%x", &AddrMediaSelect);
int AddrSessionSelect = 0;
sscanf(ControlerConfig.AddrSessionSelect, "%x", &AddrSessionSelect);
int AddrTransportModeSelect = 0;
sscanf(ControlerConfig.AddrTransportModeSelect, "%x", &AddrTransportModeSelect);
int AddrPanSelect = 0;
sscanf(ControlerConfig.AddrPanSelect, "%x", &AddrPanSelect);
int AddrMixSelect = 0;
sscanf(ControlerConfig.AddrMixSelect, "%x", &AddrMixSelect);
int AddrAuxSelect = 0;
sscanf(ControlerConfig.AddrAuxSelect, "%x", &AddrAuxSelect);
int AddrFxSelect = 0;
sscanf(ControlerConfig.AddrFxSelect, "%x", &AddrFxSelect);
int AddrGainSelect = 0;
sscanf(ControlerConfig.AddrGainSelect, "%x", &AddrGainSelect);
/* Configure LCD in text mode. */
void ConfigLCDTxtMode(){
char Cmd[32] = "";
char c_Canal[32] = "";
for (int j = 0; j < NbMidiFader; j++){
strcpy(Cmd, "13");
sprintf(c_Canal, "%02X", j);
strcat(Cmd, c_Canal);
strcat(Cmd, "19");
SendSysExTextOut(midiout, SysExHdr, Cmd, "");
}
}
/* Configure LCD in mixer mode. */
void ConfigLCDMixerMode(){
char Cmd[32] = "";
char c_Canal[32] = "";
for (int j = 0; j < NbMidiFader; j++){
strcpy(Cmd, "13");
sprintf(c_Canal, "%02X", j);
strcat(Cmd, c_Canal);
strcat(Cmd, "18");
SendSysExTextOut(midiout, SysExHdr, Cmd, "");
char BarSet[3] = {0xB0, 0x38+j, 0x01};
SendMidiOut(midiout, BarSet);
char Bar[3] = {0xB0, 0x30+j, 0x3F};
SendMidiOut(midiout, Bar);
}
}
/* Update the Midi controler light, button, LCD with struture UI variable. */
void UpdateMidiControler(){
if(init >= 1){
// Select
for( int j=0; j < NbMidiFader; j++ ){
if( ui[j].Color == 0 ){ // BUG a Verifier Canal change par j
char MidiArray[3] = {AddrMidiButtonLed, AddrMidiSelect+j, 0x00};
SendMidiOut(midiout, MidiArray);
}
else{
char MidiArray[3] = {AddrMidiButtonLed, AddrMidiSelect+j, 0x7F};
SendMidiOut(midiout, MidiArray);
}
}
SelectButtonPressed = -1;
if ( ModeMasterPressed == 1 ){
//printf("Mode Master [%i]\n", NbMidiFader);
// Mix for Master Mode
int MidiValue = 0;
MidiValue = (127 * ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].MixMidi);
char MidiArray[3] = {AddrMidiMix+NbMidiFader, MidiValue, MidiValue};
SendMidiOut(midiout, MidiArray);
// Color for Master Mode
char MidiArrayOn[3] = {AddrMidiButtonLed, AddrMidiSelect+NbMidiFader , 0x7F};
SendMidiOut(midiout, MidiArrayOn);
char MidiArrayR[3] = {AddrMidiButtonLed+1, AddrMidiSelect+NbMidiFader , (int)floor((double)127/(double)255*139)};
SendMidiOut(midiout, MidiArrayR);
char MidiArrayG[3] = {AddrMidiButtonLed+2, AddrMidiSelect+NbMidiFader , (int)floor((double)127/(double)255*0)};
SendMidiOut(midiout, MidiArrayG);
char MidiArrayB[3] = {AddrMidiButtonLed+3, AddrMidiSelect+NbMidiFader , (int)floor((double)127/(double)255*0)};
SendMidiOut(midiout, MidiArrayB);
// Name to LCD for Master Mode
if ( Lcd == 1 ){
char Cmd[32] = "";
char c_Canal[32] = "";
char c_CanalText[32] = "";
strcpy(Cmd, "12");
sprintf(c_Canal, "%02X", NbMidiFader);
sprintf(c_CanalText, "R/L");
strcat(Cmd, c_Canal);
strcat(Cmd, "0100");
SendSysExTextOut(midiout, SysExHdr, Cmd, c_CanalText);
strcpy(Cmd, "12");
sprintf(c_Canal, "%02X", NbMidiFader);
strcat(Cmd, c_Canal);
strcat(Cmd, "0000");
SendSysExTextOut(midiout, SysExHdr, Cmd, ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].Name);
//char BarSet[3] = {0xB0, 0x38+NbMidiFader, 0x01};
//SendMidiOut(midiout, BarSet);
//printf("Pan Master %i [%f]\n", UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca, ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi);
char Bar[3] = {0xB0, 0x30+NbMidiFader, ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi*0x7F};
SendMidiOut(midiout, Bar);
strcpy(Cmd, "12");
sprintf(c_Canal, "%02X", NbMidiFader);
strcat(Cmd, c_Canal);
strcat(Cmd, "0200");
if( ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi == 0.5 ){
sprintf(c_CanalText, "<C>");
}
else if( ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi < 0.5 ){
sprintf(c_CanalText, "L%i", (int)(-200*ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi+100));
}
else if( ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi > 0.5 ){
sprintf(c_CanalText, "R%i", (int)(200*ui[UIChannel+UILineIn+UIMedia+UIFx+UISubGroup+UIAux+UIVca].PanMidi-100));
}
SendSysExTextOut(midiout, SysExHdr, Cmd, c_CanalText);
}
}
for (int Canal = NbMidiFader*AddrMidiTrack; Canal < (NbMidiFader*AddrMidiTrack)+NbMidiFader; Canal++){
// Mute
int i_OrMute = (ui[Canal].MaskMute | ui[Canal].Mute) & ( ! (ui[Canal].ForceUnMute));
char MidiArrayM[3] = {AddrMidiButtonLed, AddrMidiMute+(Canal % NbMidiFader), i_OrMute*0x7F};
SendMidiOut(midiout, MidiArrayM);
// Pan BAR LCD
if ( Lcd == 1 ){
if( strcmp(ui[Canal].Type,"a") == 0 || strcmp(ui[Canal].Type,"v") == 0 ){
char BarSet[3] = {0xB0, 0x38+(Canal % NbMidiFader), 0x04};
SendMidiOut(midiout, BarSet);
char Cmd[32] = "";
char c_Canal[32] = "";
strcpy(Cmd, "12");
sprintf(c_Canal, "%02X", Canal % NbMidiFader);
strcat(Cmd, c_Canal);
strcat(Cmd, "0200");
SendSysExTextOut(midiout, SysExHdr, Cmd, "");
}
else{
char BarSet[3] = {0xB0, 0x38+(Canal % NbMidiFader), 0x01};