-
Notifications
You must be signed in to change notification settings - Fork 1
/
elecmidi.c
1937 lines (1748 loc) · 50.1 KB
/
elecmidi.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#include "tables.h"
#include "elecmidi.h"
//#define PRINT_RESERVED
#define DELAY 16384
#define SILENCE 'x'
#define NOTE 'o'
#define SILENCE4 'X'
#define PROL '_'
#define NONOTE '.'
struct DataDumpType dd;
struct DataDumpType auxData;
char* device = "/dev/dmmidi1" ;
int channel=0;
int octave=4;
int sampler=0; //0 synth 1 sampler
FILE *midiFile;
int lineNumber=0;
#define TRACK_SIZE 18725 //max size of bytes received
#define MAX_LINE 500
unsigned char ioBuffer[TRACK_SIZE+10]; //10=room for the header + EOX
char lineBufferCopy[MAX_LINE];
void printError(char *e1,char *e2) {
printf("Error in line %d: %s\n",lineNumber,lineBufferCopy);
printf("%s %s\n",e1,e2);
exit(1);
}
void openMidi(char *mode) {
midiFile = fopen(device, mode);
if (midiFile == NULL) {
printError("Error: cannot open ", device);
}
}
void sendMidiChar(unsigned char m){
fputc(m,midiFile);
}
unsigned char readMidiChar() {
return fgetc(midiFile);
}
void readMidiBuffer(unsigned char *buffer, long int bl) {
fread(buffer, bl,1,midiFile);
}
/*
void writeMidiBuffer(unsigned char *buffer, long int bl) {
fwrite(buffer, bl,1,midiFile);
}
*/
void closeMidi(){
fclose(midiFile);
}
void sendRealtimeMessage(unsigned char m){
openMidi("w");
sendMidiChar(m);
closeMidi();
}
void sendChannelMessage(unsigned char m1, unsigned char m2, unsigned char m3){
openMidi("w");
sendMidiChar(m1);
sendMidiChar(m2);
if ((m1 & 0xf0) != 0xc0) {
sendMidiChar(m3);
}
closeMidi();
}
void sendCommand(unsigned char *command, long int cl, unsigned char *buffer, long int bl) {
openMidi("w");
for(int i=0;i<cl;i++) {
if(i%512==0) {
usleep(DELAY);
//usleep(1);
}
sendMidiChar(command[i]);
}
closeMidi();
//usleep(DELAY);
openMidi("r");
int tries=0;
buffer[0]=readMidiChar();
while(buffer[0]!=0xf0 && tries++ < 2) { //wait for the sys ex message
buffer[0]=readMidiChar();
}
if(buffer[0]==0xf0){
readMidiBuffer(buffer+1, bl-1);
}
closeMidi();
}
void jumpBlanks(char **line) {
if(!*line) return;
while (**line!=0 && isspace(**line)){
(*line)++;
}
}
char *nextToken(char **line) {
if(!*line) return "";
jumpBlanks(line);
return strsep(line," "); //if no more token returns "" and line=0;
}
int readInteger(char **line, int min, int max){
char *token;
int n;
jumpBlanks(line);
token=*line;
if(!token) {
printError("Error reading integer. Blank space or end of line found",lineBufferCopy);
}
if(!isdigit(token[0]) && token[0]!='+' && token[0]!='-') {
printError("Error reading integer. found ",token);
}
//n=atoi(token);
n=strtol(token,line,10);
if(!(min==0 && max==0) && (n<min || n>max)) {
printError("Error integer out of range ",token);
}
jumpBlanks(line);
return n;
}
void readNumber(char **line, char *separator, int *intPart, int *decPart){
char *token;
char *ip, *dp;
int n;
token=nextToken(line);
if(!token) {
printError("Error reading number. Blank space or end of line found",lineBufferCopy);
}
if(!isdigit(token[0])) {
printError("Error reading number. found ",token);
}
ip=strtok_r(token,separator,&dp);
*intPart=atoi(ip);
if(separator[0]==':' && strlen(dp)==0) { //special case for motion sequence
*decPart=-1;
} else {
*decPart=atoi(dp);
}
}
char* readString(char **line) {
char *token;
char stringEnd;
jumpBlanks(line);
if(*line==0) return 0;
if(*line[0]=='"' || *line[0]=='\'') {
stringEnd=*line[0];
(*line)++;
} else {
stringEnd=' ';
}
token=*line;
while(**line && **line !=stringEnd) (*line)++;
if(stringEnd!=' ' && !**line) {
printError("Error. Untermitated string.",lineBufferCopy);
}
**line=0;
(*line)++;
return token;
}
int readTable(char **line, char *table[], int tableLen) {
if(!*line || *line[0]==0) {
printError("Error reading value.","End of line found");
}
if(isdigit(*line[0])) {
int n=readInteger(line,1,tableLen);
return n-1;
} else {
char *token=readString(line);
for(int i=0; i<tableLen;i++) {
if(strcmp(token,table[i])==0) {
return i;
}
}
printf("Error reading table in line %d. Item not found, \"%s\"\n",lineNumber, token);
printf("Values allowed:\n");
for(int i=0; i<tableLen;i++) {
printf("%d \"%s\"\n",i,table[i]);
}
exit(1);
}
}
int readOnOffTable(char **line) {
if(!*line) {
printError("Error reading On Off value.","End of line found");
}
if(isdigit(*line[0])) {
return readInteger(line,0,1);
} else {
return readTable(line,onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]));
}
}
long int readRange(char *line, int min, int max){
char *token;
char *number1;
char *number2;
int n1;
int n2;
long int range=0;
token=strtok_r(line,",",&line);
while(token) {
number1=strtok_r(token,"-",&token);
number2=token;
n1=readInteger(&number1,min,max);
if(number2[0]==0) {
n2=n1;
} else {
n2=readInteger(&number2,min,max);
}
for(int i=n1;i<=n2;i++) {
range |= (1L<< (i-1));
}
token=strtok_r(line,",",&line);
}
return range;
}
int readPart(char **line){
char *token;
long int range;
token=nextToken(line);
if(*token==0) return -1;
range=readRange(token,1,16);
return range & 0xffff;
}
char *note2string(int noteNumber,char *buffer) {
int octave;
int note;
char sharp;
if(noteNumber==0) {
buffer[0]=NONOTE;
buffer[1]=0;
} else {
noteNumber--;
octave=noteNumber/12-1;
note=noteNumber%12;
buffer[0]=keyTable[note][0];
buffer[1]='0'+octave;
if(keyTable[note][1]=='#') {
buffer[2]='+';
} else {
buffer[2]=0;
}
buffer[3]=0;
}
return buffer;
}
char *printTableElement(char *table[], int tableLenght, int i) {
static char buffer[10];
if(i<0) return "XXXXXX";
if(i>= tableLenght) { //in case the osc is not in the table
sprintf(buffer,"%d",i);
return buffer;
}
return table[i];
}
//prints gateTime and velocity
void printRangeOfValues(char *name,int part,unsigned char *p) {
int value;
int flag=0;
long int range=0;
value=*p; //first value
for(int i=1; i<64; i++) {
if(*(p+sizeof(struct stepType )*i)!=value) {
flag=1;
}
}
if(!flag) { //all values are equal
printf("%s %d %d\n",name, part, value);
return;
}
while(flag) {
flag=0;
int pendingValue=-1000; //not possible value
for(int i=0; i<64; i++) {
if(!(range & (1L<<i))) {
value=*(p+sizeof(struct stepType)*i);
printf("%s %d @%d",name, part, i+1);
for(int j=i+1;j<64;j++) {
if(*(p+sizeof(struct stepType)*j)==value) {
if(*(p+sizeof(struct stepType)*(j-1))==value) { //same value that element before
pendingValue=j+1;
} else {
if(pendingValue!=-1000) {
printf("-%d",pendingValue);
pendingValue=-1000;
}
printf(",%d",j+1);
}
range |= (1L<<j);
}
}
if(pendingValue!=-1000) {
printf("-%d",pendingValue);
pendingValue=-1000;
}
printf(" %d\n",value);
}
}
}
}
void printPartReserved(int part, unsigned char* p, int size) {
int baseAddress= p-&dd.part[part].lastStep;
for(int i=0;i<size;i++) {
printf("#part %d reserved %d %d\n",part+1,baseAddress+i,*(p+i));
}
}
void printStepReserved(int part, int step, unsigned char* p, int size) {
int baseAddress= p-&dd.part[part].lastStep;
for(int i=0;i<size;i++) {
printf("#part %d step %d reserved %d %d\n",part+1,step+1,baseAddress+i,*(p+i));
}
}
void printReserved(char *type, unsigned char* p, int size, int offset) {
int baseAddress= p-&dd.header[0]-offset;
for(int i=0;i<size;i++) {
printf("%s %d %d\n",type,baseAddress+i,*(p+i));
}
}
int printPart(int part) {
printf("lastStep %d %d\n",part+1,dd.part[part].lastStep?dd.part[part].lastStep:16);
printf("mute %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].mute));
printf("voiceAssign %d \"%s\"\n",part+1,printTableElement(voiceAssignTable,sizeof(voiceAssignTable)/sizeof(voiceAssignTable[0]),dd.part[part].voiceAssign));
printf("motionSequence %d \"%s\"\n",part+1,printTableElement(motionSequenceTable,sizeof(motionSequenceTable)/sizeof(motionSequenceTable[0]),dd.part[part].motionSequence));
printf("padVelocity %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].padVelocity));
printf("scaleMode %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].scaleMode));
printf("partPriority %d \"%s\"\n",part+1,printTableElement(partPriorityTable,sizeof(partPriorityTable)/sizeof(partPriorityTable[0]),dd.part[part].partPriority));
if(sampler) {
printf("oscType %d \"%s\"\n",part+1,printTableElement(oscTypeTableSampler,sizeof(oscTypeTableSampler)/sizeof(oscTypeTableSampler[0]),dd.part[part].oscTypel+256*dd.part[part].oscTypeh));
} else {
printf("oscType %d \"%s\"\n",part+1,printTableElement(oscTypeTable,sizeof(oscTypeTable)/sizeof(oscTypeTable[0]),dd.part[part].oscTypel+256*dd.part[part].oscTypeh));
}
printf("oscEdit %d %d\n",part+1,dd.part[part].oscEdit);
printf("filterType %d \"%s\"\n",part+1,printTableElement(filterTypeTable,sizeof(filterTypeTable)/sizeof(filterTypeTable[0]),dd.part[part].filterType));
printf("filterCutoff %d %d\n",part+1,dd.part[part].filterCutoff);
printf("filterReso %d %d\n",part+1,dd.part[part].filterReso);
printf("filterEG %d %d\n",part+1,(signed char)dd.part[part].filterEG);
printf("modType %d \"%s\"\n",part+1,printTableElement(modulationTypeTable,sizeof(modulationTypeTable)/sizeof(modulationTypeTable[0]),dd.part[part].modulationType));
printf("modSpeed %d %d\n",part+1,dd.part[part].modulationSpeed);
printf("modDepth %d %d\n",part+1,dd.part[part].modulationDepth);
printf("EGAttack %d %d\n",part+1,dd.part[part].EGAttack);
printf("EGDecay %d %d\n",part+1,dd.part[part].EGDecay);
printf("ampLevel %d %d\n",part+1,dd.part[part].ampLevel);
printf("ampPan %d %d\n",part+1,(signed char)dd.part[part].ampPan);
printf("ampEG %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].EGOnOff));
printf("MFXSend %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].MFXSend));
printf("grooveType %d \"%s\"\n",part+1,printTableElement(grooveTypeTable,sizeof(grooveTypeTable)/sizeof(grooveTypeTable[0]),dd.part[part].grooveType));
printf("grooveDepth %d %d\n",part+1,dd.part[part].grooveDepth);
printf("IFXOnOff %d \"%s\"\n",part+1,printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.part[part].IFXOnOff));
printf("IFXType %d \"%s\"\n",part+1,printTableElement(IFXTypeTable,sizeof(IFXTypeTable)/sizeof(IFXTypeTable[0]),dd.part[part].IFXType));
printf("IFXEdit %d %d\n",part+1,dd.part[part].IFXEdit);
printf("oscPitch %d %d\n",part+1,(signed char)dd.part[part].oscPitch);
printf("oscGlide %d %d\n",part+1,dd.part[part].oscGlide);
printf("steps %d ",part+1);
for(int s=0; s<64; s++) {
if(s%4==0) printf(" ");
if(s%16==0) printf(" ");
if (dd.part[part].step[s].onOff==1) {
if(s>0 && dd.part[part].step[s-1].gateTime>=127) {
printf("%c",PROL);
} else {
printf("%c",NOTE);
}
} else {
printf("%c",SILENCE);
}
}
printf("\n");
printf("stepsTrigger %d ",part+1);
for(int s=0; s<64; s++) {
if(s%4==0) printf(" ");
if(s%16==0) printf(" ");
if (dd.part[part].step[s].triggerOnOff==1) {
printf("%c",NOTE);
} else {
printf("%c",SILENCE);
}
}
printf("\n");
//printf("notes %d ",part+1);
for(int s=0; s<64; s++) {
char buffer[4];
if(s%16==0) printf("\nnotes %d @%d ", part+1,s+1);
else if(s%4==0) printf(" ");
/*
for(int n=0;n<4;n++) {
//printf("%s %d",note2string(dd.part[part].step[s].note[n],buffer),dd.part[part].step[s].note[n]);
printf("%d",dd.part[part].step[s].note[n]);
}
printf(" ");
*/
if(dd.part[part].step[s].note[0]==0 && dd.part[part].step[s].note[1]==0 && dd.part[part].step[s].note[2]==0 && dd.part[part].step[s].note[3]==0) {
printf("%c ",NONOTE);
} else {
printf("%s",note2string(dd.part[part].step[s].note[0],buffer));
if(!(dd.part[part].step[s].note[1]==0 && dd.part[part].step[s].note[2]==0 && dd.part[part].step[s].note[3]==0)) {
printf("%s",note2string(dd.part[part].step[s].note[1],buffer));
if(!(dd.part[part].step[s].note[2]==0 && dd.part[part].step[s].note[3]==0)) {
printf("%s",note2string(dd.part[part].step[s].note[2],buffer));
if(!dd.part[part].step[s].note[3]==0) {
printf("%s",note2string(dd.part[part].step[s].note[3],buffer));
} else printf(" ");
} else printf(" ");
} else printf(" ");
}
printf(" ");
}
printf("\n");
printRangeOfValues("gateTime",part+1,&dd.part[part].step[0].gateTime);
printRangeOfValues("velocity",part+1,&dd.part[part].step[0].velocity);
#ifdef PRINT_RESERVED
printPartReserved(part,&dd.part[part].reserved0,sizeof(dd.part[part].reserved0));
printPartReserved(part,&dd.part[part].reserved1,sizeof(dd.part[part].reserved1));
printPartReserved(part,&dd.part[part].reserved2,sizeof(dd.part[part].reserved2));
printPartReserved(part,&dd.part[part].reserved3[0],sizeof(dd.part[part].reserved3));
printPartReserved(part,&dd.part[part].reserved4[0],sizeof(dd.part[part].reserved4));
printPartReserved(part,&dd.part[part].reserved5,sizeof(dd.part[part].reserved5));
printPartReserved(part,&dd.part[part].reserved6[0],sizeof(dd.part[part].reserved6));
for(int step=0;step<64;step++){
printStepReserved(part,step,&dd.part[part].step[step].reserved[0],sizeof(dd.part[part].step[step].reserved));
}
#endif
/*
unsigned char *p=(unsigned char*)ⅆ
for(int i=0;i<16384;i++){
printf("#dump %d %d %x\n",i,*(p+i),*(p+i));
}
printf("print\n");
*/
printf("\n\n");
}
void printSlot(int slot) {
if (dd.motionSequence.part[slot]==0) return; //Off
printf("motion %d %d \"%s\"",slot,dd.motionSequence.part[slot],printTableElement(destinationTable,sizeof(destinationTable)/sizeof(destinationTable[0]),dd.motionSequence.destination[slot]));
for(int i=0;i<64;i++) {
printf(" %3d",dd.motionSequence.motion[slot][i]);
}
printf("\n");
}
int printPattern(char *line) {
int partRange;
char *token;
int all=0;
//token=strtok_r(line," ",&remain); ///?????
token=nextToken(&line);
if(*token==0) {
all=1;
partRange=0xffff;
} else {
partRange=readPart(&token);
}
if(all) {
printf("name \"%s\"\n",dd.name);
int tt=dd.tempo1+256*dd.tempo2;
printf("tempo %d.%d\n", tt/10,tt%10);
int sw=(signed char)dd.swing;if(sw==48) sw=50; if(sw==-48) sw=-50;
printf("swing %d%%\n",sw);
printf("length %x\n",dd.length+1);
printf("beat \"%s\"\n",printTableElement(beatTable,sizeof(beatTable)/sizeof(beatTable[0]),dd.beat));
printf("key \"%s\"\n",printTableElement(keyTable,sizeof(keyTable)/sizeof(keyTable[0]),dd.key));
printf("scale \"%s\"\n",printTableElement(scaleTable,sizeof(scaleTable)/sizeof(scaleTable[0]),dd.scale));
printf("chordset %x\n",dd.chordset+1);
printf("level %d\n",127-dd.playlevel);
printf("alternate1314 \"%s\"\n",printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.alternate1314));
printf("alternate1516 \"%s\"\n",printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.alternate1516));
printf("gateArpPattern %d\n",dd.touchScale.gateArpPattern+1);
printf("gateArpSpeed %d\n",dd.touchScale.gateArpSpeed);
printf("gateArpTime %d\n",(signed char)dd.touchScale.gateArpTimel+(signed char)dd.touchScale.gateArpTimeh*256);
printf("MFXType \"%s\"\n",printTableElement(MFXTypeTable,sizeof(MFXTypeTable)/sizeof(MFXTypeTable[0]),dd.masterFX.type));
printf("MFXPadX %d\n",dd.masterFX.XYpadX);
printf("MFXPadY %d\n",dd.masterFX.XYpadY);
printf("MFXHold \"%s\"\n",printTableElement(onOffTable,sizeof(onOffTable)/sizeof(onOffTable[0]),dd.masterFX.MFXHold?1:0));
printf("chainTo %d\n",dd.chainTo);
printf("chainRepeat %d\n",dd.chainRepeat);
printf("\n");
for(int slot=0;slot<24;slot++) {
printSlot(slot);
}
printf("\n");
}
for(int part=0;part<16;part++) {
if(partRange & 1) {
printPart(part);
}
partRange = partRange>>1;
}
#ifdef PRINT_RESERVED
printf("#pattern header %hhd %hhd %hhd %hhd\n",dd.header[0],dd.header[1],dd.header[2],dd.header[3]);
printf("#pattern size %d\n",dd.size[0]+256*dd.size[1]+256*256*dd.size[2]+256*256*256*dd.size[3]);
printf("#pattern version %hhd %hhd %hhd %hhd\n",dd.version[0],dd.version[1],dd.version[2],dd.version[3]);
printReserved("#pattern reserved",&dd.reserved1[0],sizeof(dd.reserved1),0);
printReserved("#pattern reserved",&dd.reserved2,sizeof(dd.reserved2),0);
printReserved("#pattern reserved",&dd.reserved3[0],sizeof(dd.reserved3),0);
printReserved("#pattern reserved",&dd.reserved4[0],sizeof(dd.reserved4),0);
printReserved("#pattern reserved",&dd.reserved5[0],sizeof(dd.reserved5),0);
printReserved("#pattern reserved",&dd.reserved6,sizeof(dd.reserved6),0);
printReserved("#pattern reserved",&dd.reserved7[0],sizeof(dd.reserved7),0);
printf("#pattern footer %hhd %hhd %hhd %hhd\n",dd.footer[0],dd.footer[1],dd.footer[2],dd.footer[3]);
printReserved("#pattern reserved",&dd.reserved8[0],sizeof(dd.reserved8),0);
printReserved("#touchScale reserved",&dd.touchScale.reserved1[0],sizeof(dd.touchScale.reserved1),(unsigned char*)&dd.touchScale-&dd.header[0]);
printReserved("#touchScale reserved",&dd.touchScale.reserved2,sizeof(dd.touchScale.reserved2),(unsigned char*)&dd.touchScale-&dd.header[0]);
printReserved("#touchScale reserved",&dd.touchScale.reserved3[0],sizeof(dd.touchScale.reserved3),(unsigned char*)&dd.touchScale-&dd.header[0]);
printReserved("#masterFX reserved",&dd.masterFX.reserved1,sizeof(dd.masterFX.reserved1),(unsigned char*)&dd.masterFX-&dd.header[0]);
printReserved("#masterFX reserved",&dd.masterFX.reserved2,sizeof(dd.masterFX.reserved2),(unsigned char*)&dd.masterFX-&dd.header[0]);
printReserved("#masterFX reserved",&dd.masterFX.reserved3[0],sizeof(dd.masterFX.reserved3),(unsigned char*)&dd.masterFX-&dd.header[0]);
#endif
return 1;
}
int checkData(struct DataDumpType *dd){
if(dd->header[0]!=80 || dd->header[1]!=84 || dd->header[2]!=83 || dd->header[3]!=84 ) {
return -1;
}
if(dd->footer[0]!=80 || dd->footer[1]!=84 ||dd->footer[2]!=69 ||dd->footer[3]!=68) {
return -2;
}
return 0;
}
int decodeData(unsigned char *buffer,struct DataDumpType *dd){
unsigned char *dest=(unsigned char *)dd;
long int ni;
long int no=0;
unsigned char b7;
for(ni=0;ni<TRACK_SIZE;ni++) {
if(ni%8==0) {
b7=buffer[ni];
} else {
dest[no]=buffer[ni];
if(b7&1) {
dest[no] +=128;
}
b7=b7>>1;
no++;
}
}
int error=checkData(dd);
/*
if(error) {
printf("error %d in received data\n",error);
}
*/
return error;
}
long int encodeData(struct DataDumpType *dd, unsigned char *buffer){
unsigned char *orig=(unsigned char *)dd;
long int ni;
long int no=1;
unsigned char b7=0;
for(ni=0;ni<sizeof(*dd);ni++) {
buffer[no]=(orig[ni]&0x7F);
if(orig[ni] & 0x80) {
b7 = b7 | 0x80;
}
b7= b7>>1;
if(ni%7==6) {
buffer[no-7]=b7;
b7=0;
no++;
}
no++;
}
return no+1;
}
/*
void sendAckNack(int type) { //0x23=ack 0x24=nack
unsigned char data[8] = {0xf0, 0x42, 0x30 | channel, 0, 1, 0x23, type,0xf7};
openMidi("w");
for(int i=0;i<8;i++) {
sendMidiChar(data[i]);
}
closeMidi();
}
*/
void patternDataDump(int pattern) {
unsigned char data[10] = {0xf0, 0x42, 0x30 | channel, 0, 1, 0x23, 0x1c,
pattern & 0x7f, pattern>>7, 0xf7};
struct currentPattern {
unsigned char header[7];
unsigned char patternLSB;
unsigned char patternMSB;
unsigned char buffer[TRACK_SIZE];
} input;
sendCommand(data,sizeof(data),(char *)&input, sizeof(input));
//sendAckNack(0x23);
decodeData(input.buffer,&auxData);
}
void currentPatternDataDump() {
unsigned char data[8] = {0xf0, 0x42, 0x30 | channel, 0, 1, 0x23, 0x10,0xf7};
struct currentPattern {
unsigned char header[7];
unsigned char buffer[TRACK_SIZE];
unsigned char eox;
} *input=(struct currentPattern *)ioBuffer;
int ok=0;
int tries=0;
while(!ok && tries++ <3) {
sendCommand(data,sizeof(data),(char *)input, sizeof(*input));
if(input->header[6]==0x40) {
int res=decodeData(input->buffer,&dd);
ok=(res==0);
} else {
sleep(1);
}
/*
if(input->header[6]==0x24) {
printf("NACK received\n");
} else if(input->header[6]==0) {
printf("null data received\n");
sleep(1);
printf("p1\n");
} else {
printf("%x received\n",input->header[6]);
int res=decodeData(input->buffer,&dd);
ok=(res==0);
}
*/
}
if(!ok) {
printf("error receiving data %x\n",input->header[6]);
exit(1);
}
}
void currentPatternDataSend() {
unsigned char data[8];
struct currentPattern {
unsigned char header[7];
unsigned char buffer[TRACK_SIZE];
unsigned char EOX;
} *output=(struct currentPattern *)ioBuffer;
int ok=0;
int tries=0;
long int l;
output->header[0]=0xf0;
output->header[1]=0x42;
output->header[2]=0x30 | channel;
output->header[3]=0;
output->header[4]=1;
output->header[5]=0x23;
output->header[6]=0x40;
output->EOX=0xF7;
l=encodeData(&dd,output->buffer);
l+=8; //header + EOX
while(!ok && tries++ <5) {
sendCommand(ioBuffer,l,data,sizeof(data));
if(data[6]==0x23){
ok=1;
} else {
sleep(1);
}
/*
if(data[6]==0x24) {
printf("NACK received\n");
} else if(data[6]==0x23){
ok=1;
} else if(data[6]==0x26) {
printf("data format error\n");
} else {
printf("error sending data %x\n",data[6]);
}
*/
}
if(!ok) {
printf("error sending data %x\n",data[6]);
if(data[6]==0x26) {
printf("data format error\n");
}
}
}
void deleteSpaces(char *s){
char *p=s;
while(*s) {
if(*s!=' ' && *s!='\t') {
*p++=*s;
}
s++;
}
*p=0;
}
int readStep0(char **line){
char *token;
if(*line && *line[0]=='@') {
token=nextToken(line);
token++;
return readInteger(&token,1,64);
} else {
return 1;
}
}
int note2midi(char note, int octave, int alt) {
int n;
if(note==NONOTE) return 0;
if(note>'B') octave--;
n=(note-'A')*2;
if(note>='C') n--;
if(note>='F') n--;
return 21 + n + alt + octave * 12;
}
//silence 'x'
//4 silences 'X'
//note 'o'
//prol '_'
//no note '.'
void putSilence(int part, int step) {
dd.part[part].step[step].onOff=0;
if(dd.part[part].step[step].gateTime>96) {
dd.part[part].step[step].gateTime=96;
}
}
void putTie(int part, int step) {
step=(step-1)%64;
dd.part[part].step[step].gateTime=127;
step=(step+1)%64;
dd.part[part].step[step].onOff=1;
}
int putSymbolInPattern(int part, int step, char symbol) {
switch(symbol) {
case SILENCE: putSilence(part,step++);break;
case SILENCE4: putSilence(part,step++);
if(step<64) putSilence(part,step++);
if(step<64) putSilence(part,step++);
if(step<64) putSilence(part,step++);
break;
case NOTE: dd.part[part].step[step++].onOff=1 ;break;
case PROL: putTie(part,(step)%64);step++;break;
case 0: step=64; break;
}
return step;
}
int readStepNotes(char *notes,int part,int step, int trig){
int n=0;
int note;
int alt;
char c;
int nextStep=step;
while(n<4) {
alt=0;
note=toupper(*notes);
if(note==0 || (trig &¬e==PROL)) { //end of string. The remainder notes are put to 0
while(n<4) {
dd.part[part].step[step].note[n++]=0;
}
break;
}
nextStep=step+1;
if((note<'A' || note>'G') && note!=NONOTE) {
printError("Error reading note",notes);
}
c=*++notes;
if(isdigit(c)) {
octave=c-'0';
c=*++notes;
} else if(c=='v') {
if(octave>0) {
octave--;
c=*notes++;
} else {
printError("Error octave too low",notes);
}
} else if(c=='^') {
if(octave<9) {
octave++;
c=*notes++;
} else {
printError("Error octave too high",notes);
}
}
if(c=='+') alt=1;
if(c=='-') alt=-1;
if(alt) notes++;
int midiNote=note2midi(note,octave,alt);
if(midiNote)
midiNote++; //// why??
if(midiNote>127) {
printError("Error note too high",notes);
}
if(step<64) {
dd.part[part].step[step].note[n]=midiNote;
}
n++;
}
if(trig) {
dd.part[part].step[step].onOff=0;
dd.part[part].step[step].triggerOnOff=0;
for(int n=0; n<4;n++) {
if(dd.part[part].step[step].note[n]!=0) {
dd.part[part].step[step].onOff=1;
dd.part[part].step[step].triggerOnOff=1;
}
}
}
step=nextStep;
if(*notes==PROL && trig) {
while(*notes==PROL) {
for(int n=0;n<4;n++) dd.part[part].step[step].note[n]=dd.part[part].step[(step-1)%64].note[n];
dd.part[part].step[step].triggerOnOff=0;
step=putSymbolInPattern(part,step,PROL);
notes++;
}
}
return step;
}
int readExpression(char *expr, char **buff,int len) {
char *buffer=*buff;
char *start=buffer;
char *end=buffer;
while(expr && *expr) { //readInteger can make expr null
if(*expr=='(') { // '('
//find matching right parenthesis
int n=1;
char *rightp =expr;
expr++; //after the parenthesis
*buffer++=' ';
start=buffer;
while(*rightp && n>0) {
rightp++;
if(*rightp=='(') n++;
if(*rightp==')') n--;
}
if(n!=0) {
printError("Unclosed parenthesis","");
}
*rightp=0;
len=readExpression(expr,&buffer,len);
expr=rightp+1;
end=buffer;
} else if (*expr==')') {
printError("Too many right parenthesis","");
} else if (*expr=='*') { // '*'
*buffer++=' '; //
end=buffer;
expr++;
int n=readInteger(&expr,0,0);
if(n<=0) {
printError("Integer must be positive","");
}
n--;
while(n--) {
for(char *p=start;p<end;p++) {
len--;
if(!len) {
printError("expresion too long","");
}
*buffer++=*p;
}
}
start=buffer-1;
} else { //all other characters
len--;
if(len<2) {
printError("expresion too long","");
}
if((isalnum(*expr)||*expr=='+'||*expr=='.') && (isblank(*(expr-1))||*(expr-1)==0)) { //*(expr-1)==0 former ')'
start=buffer;
}
*buffer++=*expr++;
}
}
*buffer=0;
*buff=buffer;
return len;
}
int readTriggers(char *line, int type) { //type 0 onOff 1 triggerOnOff
char *token;
int partRange;
int step0=1;
int step;
char buffer[MAX_LINE*2]; //just in case
char *buff=buffer;
partRange=readPart(&line);
if(partRange==-1) {
return -1;
}
step0=readStep0(&line);
if(step0==-1) {
return -2;
}
readExpression(line,&buff,sizeof(buffer));
line=buffer;
deleteSpaces(line);
for(int part=0;part<16;part++) {
if(partRange&1) {
step=step0-1;
for(int i=0;i<strlen(line);i++) {
if(type==0) {
step=putSymbolInPattern(part, step, line[i]);
} else {
if(line[i]==NOTE) {
dd.part[part].step[step++].triggerOnOff=1;
} else if(line[i]==SILENCE) {
dd.part[part].step[step++].triggerOnOff=0;
} else {
printError("Error, no valid character",line);
}
}
}
if(step>=64) {
break;
}
}
partRange=partRange>>1;
}
return 0;
}
int readNotes(char *line,int trig) {
char *token;
int partRange;
int step0=1;
int step[16];
char buffer[MAX_LINE*2]; //just in case
char *buff=buffer;