-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquasimodo.cpp
More file actions
1232 lines (1078 loc) · 48 KB
/
quasimodo.cpp
File metadata and controls
1232 lines (1078 loc) · 48 KB
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
/*
quasimodo - a command line tool for Schulmerich Generation 4 DSP based electronic carillons
A quick hack to read, parse, and extract data from AutoBel Card images.
*/
#include <iostream>
#include <fstream>
#include <filesystem>
#include <algorithm>
#include <iterator>
#include <regex>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <ctime>
#include <random>
#include <unistd.h>
// For Serial I/O Mess
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <signal.h>
#include <pthread.h>
// Linux-specific
#include <linux/serial.h>
#include <linux/ioctl.h>
#include <asm/ioctls.h>
int loglevel = 0; // No logging, just shut up already.
bool debug = false; // No debugging
bool showhelp = false; // show detailed help
float tempo = 24; // Number of delay byte counts / second
float transpose = 0; // value to add to MIDI note numbers
float bps = 38400; // baud rate for playback
bool play = false; // Play the specified ( card, collection, song )
bool listcolls = false; // list collections
bool listsongs = false; // list songs
bool tty = false; // Send MIDI out TTY port
bool delimited = true; //
int delimiter = 0xFF; //
int collection_id = -1; // the specified collection ID used for playback / extract
int song_id = -1; // the specified song ID used for playback / extract
bool cardloaded = false; // Set to true if the buffer contains a valis AutoBel Card image.
int cardsize = 0; // size of data in cardimage
std::string cardbuffer ; // buffer for holding the card image
bool patchload = true; // Load patch table into memory
bool patchtest = false; // Loop through various patches to discover stuff
std::string inFile = "";
std::string outFile = "";
std::string ttyFile = "/dev/ttyUSB0";
std::string patchFile = "./quasimodo.patches";
std::vector<std::string> patchdata ; // raw patch strings from the patch table file
std::vector<std::string> patchname ; // name of the patch
std::vector<std::string> patchin; // series of patch names from command line for leadin...
std::vector<std::string> patches; // playing...
std::vector<std::string> patchout; // leadout...
std::vector<int> tryvalue; // A set of series of values to substitute for a variable
int currenttry; // The current value to substitute for variables in patches
struct termios oldtio, newtio;
struct serial_struct ser_info;
int serial; // Awaken the old gods
using namespace std;
#define OPTIONBOOL(O,V,X) \
if ( ! parsed && j < argc && 0 == strcmp(argv[j], #O) ) \
{ \
(V) = (X); \
if( loglevel > 6 ) cout << "Parsed argv[" << j << "]='" << argv[j] \
<< "' option ( " << #O << " ). Setting " << #V << " to " << (V) << std::endl; \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
parsed = true; \
}
#define OPTIONINT(O,V,MIN,MAX) \
if ( ! parsed && j < argc && 0 == strcmp(argv[j], #O) ) \
{ \
std::string theoption = argv[j]; \
std::string theparm; \
int thenum = 0; \
bool inbounds = false; \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) ) \
{ \
theparm = argv[j]; \
thenum = stoi( theparm ); \
if( thenum >= (MIN) && thenum <= MAX ) \
{ \
(V) = thenum; \
if( loglevel > 6 ) cout << "Parsed argv[" << j << "]='" << theoption << "' with value '" << theparm \
<< "' option ( " << #O << " ). Range [" << (MIN) << "," << (MAX) << "]" \
<< " Setting " << #V << " to " << (V) << std::endl; \
} \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
} \
if( thenum < (MIN) || thenum > (MAX) ) \
{ \
cout << "ERROR: option " << theoption << " should be between " << (MIN) << " and " << (MAX) << std::endl; \
return 1; \
} \
parsed = true; \
}
#define OPTIONINTCSV(O,V,MIN,MAX) \
if ( ! parsed && j < argc && 0 == strcmp(argv[j], #O) ) \
{ \
std::string theoption = argv[j]; \
std::string theparm; \
std::vector<int> thenums; \
bool inbounds = false; \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) ) \
{ \
theparm = argv[j]; \
if( theparm == "all" ) theparm = to_string(MIN) + "-" + to_string((MAX) - 1); \
if( theparm.size() > 0 && theparm[theparm.size()-1] == '-' ) theparm += to_string((MAX) - 1); \
if( theparm.size() > 0 && theparm[0] == '-' ) theparm.insert(0, to_string((MIN))); \
thenums = findIntList( theparm ); \
if( thenums.size() >= (MIN) && thenums.size() <= MAX ) \
{ \
(V) = thenums; \
if( loglevel > 6 ) cout << "Parsed argv[" << j << "]='" << theoption << "' with value '" << theparm \
<< "' option ( " << #O << " ). Range [" << (MIN) << "," << (MAX) << "]" \
<< " Setting " << #V << " to " << vi2csv(V) << std::endl; \
} \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
} \
if( thenums.size() < (MIN) || thenums.size() > (MAX) ) \
{ \
cout << "ERROR: option " << theoption << " list size should be between " << (MIN) << " and " << (MAX) << std::endl; \
return 1; \
} \
parsed = true; \
}
#define OPTIONFLOAT(O,V,MIN,MAX) \
if ( ! parsed && j < argc && 0 == strcmp(argv[j], #O) ) \
{ \
std::string theoption = argv[j]; \
std::string theparm; \
float thenum = 0; \
bool inbounds = false; \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) ) \
{ \
theparm = argv[j]; \
thenum = stof( theparm ); \
if( thenum >= (MIN) && thenum <= MAX ) \
{ \
(V) = thenum; \
if( loglevel > 6 ) cout << "Parsed argv[" << j << "]='" << theoption << "' with value '" << theparm \
<< "' option ( " << #O << " ). Range [" << (MIN) << "," << (MAX) << "]" \
<< " Setting " << #V << " to " << (V) << std::endl; \
} \
for(int i=1; i<argc; ++i) \
argv[i] = argv[i+1]; \
--argc; \
} \
if( thenum < (MIN) || thenum > (MAX) ) \
{ \
cout << "ERROR: option " << theoption << " should be between " << (MIN) << " and " << (MAX) << std::endl; \
return 1; \
} \
parsed = true; \
}
//
// Allow output of vectors in the format {_,_,_,....,_}
//
template <typename S>
ostream& operator<<(ostream& os,
const vector<S>& vector)
{
// Printing the vector as {_,_,_,...,}
os << "{";
for ( int i=0; i < vector.size(); i++ )
{
auto element = vector[i];
os << element;
if( i+1 != vector.size() ) os << ","; // add comma if not final element
}
os << "}";
return os;
}
struct song { int collection_offset; // Member (int variable)
int song_offset; // Member (string variable)
int data_length;
}; // Structure variable
const int MIN_CARD_SIZE = 1048576; // From actual cards seen so far
const int MAX_CARD_SIZE = 1048576;
const int MAX_COLLECTIONS = 100;
const int MAX_SONGS_PER_COLLECTION = 32;
const int MAX_MIDI_LENGTH = 10000;
const int MAX_CMD_LENGTH = 256;
const int SONG_HEADER_LENGTH = 2 * 16;
const int COLLECTION_HEADER_LENGTH = 7 * 16;
song songs[MAX_SONGS_PER_COLLECTION * MAX_COLLECTIONS];
int num_songs = 0;
//
// findIntList() - takes string containing a comma separated list of INTs and returns a vector of ints
//
std::vector<int> findIntList ( std::string s )
{
std::vector<int> vect;
std::string recon;
std::stringstream ss(s); // get string stream of input string
// reads an int from ss into i, until it cannot.
for (int i=0; ss >> i;)
{
// handle 'N-M' case like ...,4-21,... 4 is added on prior itteration so add N+1 thru M
if( i < 0 )
{
if( vect.size() == 0 ) vect.push_back( 0 ); // add a leading 0 to the list if list empty
for( int j = vect.back() + 1; j < abs(i); j++ ) vect.push_back( j ); // for N+1...M-1
i = abs(i); // "remove" the '-'
}
vect.push_back(i);
if (ss.peek() == ',')
ss.ignore();
}
// sort and deduplicate
sort( vect.begin(), vect.end() );
vect.erase( unique( vect.begin(), vect.end() ), vect.end() );
if( loglevel > 8 ) cout << "findIntList( '" << s << "' ) = " << vect << std::endl;
return vect;
}
// Return a comma separated string of ranges, e.g. {1,3,5,7,8,9,10,...18,100,106} --> "1,3,5,7-18,100,106"
std::string vi2csv( std::vector<int> vi )
{
// handle edge cases
if( vi.size() == 0 ) return "";
if( vi.size() == 1 ) return to_string( vi[0] );
if( vi.size() == 2 ) return to_string( vi[0] ) + "," + to_string( vi[1] );
std::string retval = "";
int idx = 0;
int cur = 0;
while( idx < vi.size() )
{
retval += to_string(vi[idx] ); // add vi[idx]
cur = idx;
// while vi[idx+1] INCREMENTS by one
while( idx + 1 < vi.size() && vi[idx] == vi[idx+1] - 1 )
{
idx++;
}
if( idx > cur )
{
retval += "-" + to_string( vi[idx] );
}
idx++;
if( idx < vi.size() ) retval += ",";
}
return retval;
}
std::string HexToBytes(const std::string &hex)
{
// Return value is a string. Caller will use string:data() to get the byte stream
// If a variable ?? is encountered, substitute the current try value
std::string bytes;
char byte;
for(unsigned int i = 0; i < hex.length(); i += 2)
{
std::string byteString = hex.substr(i, 2);
if( byteString == "??" )
{
byte = (char) currenttry;
}
else
{
byte = (char) strtol(byteString.c_str(), NULL, 16);
}
bytes.push_back(byte);
}
return bytes;
}
//
// Play AutoBel Card track data ( and quasimodo patch data )
// returning the computed length of the track. The getsize
// option silently scans the data only and returns the computed
// length, performing any error checking, but without actually
// sending the notes.
//
int autobelplayer( char buffer[], bool getsize )
{
char spinner[] = " > ";
char bytes[4];
unsigned int spinner_count = 3;
unsigned int col_offset = 0; // for direct stream playing
unsigned int song_offset = 0; // for direct stream playing
unsigned int midi_offset = song_offset; // Removed +32 offset as we now accept the stream
unsigned int midi_length = 0;
int duration = 0; // the elapsed time ( sum of all delay operations in microseconds )
if( ! getsize ) printf( "\n" );
if( debug )
{
std::cout << "Data: ";
bool done = false;
for (int i = 0; ! done; ++i)
{
for (int j = 0; ! done; ++j)
{
printf("%02X ", (unsigned char)buffer[(i*16) + j]);
if( (i*16) + j > 1
&& (unsigned char)buffer[(i*16) + j - 1] == 0xFF
&& (unsigned char)buffer[(i*16) + j] == 0x2F ) done = true;
}
printf("\n ");
}
printf("\n");
fflush(stdout);
}
while( midi_offset + midi_length < song_offset + MAX_MIDI_LENGTH )
{
// detect FF 2F end of MIDI data signature and terminate.
if( (int)(unsigned char)buffer[col_offset+midi_offset+midi_length] == 0xFF
&& (int)(unsigned char)buffer[col_offset+midi_offset+midi_length+1] == 0x2F )
{
if( getsize ) break; // be silent if only computing the duration
printf( " End of Song %05d: ", midi_length );
printf( "%02X %02X ",
(unsigned char)buffer[col_offset+midi_offset+midi_length],
(unsigned char)buffer[col_offset+midi_offset+midi_length+1]);
printf( " <-- END OF SONG " );
fflush(stdout);
break;
} // If END
if( debug && ! getsize ) { printf("."); fflush(stdout); }
// TODO: Merge with section for sysexec patches
// detect MIDI note off 8n XX XX DD / note 9n XX XX DD / control Bn XX XX DD
if( ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xF0 ) == 0x80
|| ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xF0 ) == 0x90
|| ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xF0 ) == 0xB0 )
{
if( ! getsize ) printf( " Playing %05d: ", midi_length );
if( ! getsize ) printf( "%02X %02X %02X ",
(unsigned char)buffer[col_offset+midi_offset+midi_length],
(unsigned char)buffer[col_offset+midi_offset+midi_length+1],
(unsigned char)buffer[col_offset+midi_offset+midi_length+2]);
bytes[0]=(unsigned char)buffer[col_offset+midi_offset+midi_length];
bytes[1]=(unsigned char)buffer[col_offset+midi_offset+midi_length+1];
bytes[2]=(unsigned char)buffer[col_offset+midi_offset+midi_length+2];
bytes[3]=(unsigned char)delimiter;
midi_length = midi_length + 3; // Move offset to the "delay" byte
int delay = 0;
// if delay byte > 0x80, it is a MSB ( less 0x80 ) for a 2 byte delay value, I guess
if( (int)(unsigned char)buffer[col_offset+midi_offset+midi_length] > 0x80 )
{
delay = (128 * ((int)(unsigned char)buffer[col_offset+midi_offset+midi_length] - 0x80));
midi_length++;
}
delay = delay + (int)(unsigned char)buffer[col_offset+midi_offset+midi_length];
midi_length++;
if( ! getsize ) printf( "with delay %5d ", delay );
// Calculate delay specified, then subtract propigation delay for 40 bits @ line speed
float usdelay = ( ( delay * 1000000 ) / tempo ) - ( ( 1000000 * 4 * 10 ) / bps );
if( ! getsize ) printf( " ( %7d us ) ", (int)usdelay );
if( tty && ! getsize )
{
int msg_len = 3;
if( delimited ) msg_len++;
write(serial, &bytes[0], msg_len);
printf( "--> %02X %02X %02X %02X ",
(unsigned char)bytes[0],
(unsigned char)bytes[1],
(unsigned char)bytes[2],
(unsigned char)bytes[3] ); // <-- This 4th byte is not needed if not delimited
printf( "--> TTY " );
}
if( ! getsize ) printf( "\r" );
if( debug && ! getsize ) printf( "\n" );
fflush(stdout);
// if( usdelay > 100 ) usleep( (int) usdelay );
while(usdelay > 100)
{
int thedelay;
if( usdelay > 250000 ) { thedelay = 250000; } else { thedelay = usdelay; }
if( ! getsize ) printf( " %.4s\r", &spinner[ spinner_count % 4 ] );
if( ! getsize ) fflush(stdout);
duration = duration + thedelay;
if( ! getsize ) usleep( thedelay );
usdelay = usdelay - thedelay;
spinner_count--;
} // while Delay spinner
} // If 9n XX XX DD / Bn XX XX DD / F0 XX XX DD
else
// Fn XX ... XX FF where n=0,1,2, and 6
if( ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xFF ) == 0xF0
|| ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xFF ) == 0xF1
|| ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xFF ) == 0xF2
|| ((unsigned char)buffer[col_offset+midi_offset+midi_length] & 0xFF ) == 0xF6 )
{
// All Fn XX ... XX sequences are delimited with 0xFF ( my us, in the patch file
// no Fn XX ... XX sequences are observed in AutoBel card data, it is sent by the
// keyboard or the console. These sequences do not appear to be any standard
// MIDI commands.
int cmdlen = 0;
while( cmdlen < MAX_CMD_LENGTH
&& midi_offset + midi_length + cmdlen < song_offset + MAX_MIDI_LENGTH
&& ((unsigned char)buffer[col_offset+midi_offset+midi_length+cmdlen] & 0xFF ) != 0xFF
) cmdlen++;
// The above does not include the 0xFF delimiter, copy it anyway eve if not used later.
bytes[cmdlen]=(unsigned char)buffer[col_offset+midi_offset+midi_length+cmdlen];
if( ! getsize ) printf( " Patching %05d: ", midi_length );
for( int i = 0; i < cmdlen; i++ )
{
if( ! getsize ) printf( "%02X ",
(unsigned char)buffer[col_offset+midi_offset+midi_length+i]);
bytes[i]=(unsigned char)buffer[col_offset+midi_offset+midi_length+i];
}
midi_length = midi_length + cmdlen + 1; // advance offset past this command AND the 0xFF delimiter
// Fn XX ... XX commands do not have a delay ( yet )
int delay = 0;
/*
// if delay byte > 0x80, it is a MSB ( less 0x80 ) for a 2 byte delay value, I guess
if( (int)(unsigned char)buffer[col_offset+midi_offset+midi_length] > 0x80 )
{
delay = (128 * ((int)(unsigned char)buffer[col_offset+midi_offset+midi_length] - 0x80));
midi_length++;
}
delay = delay + (int)(unsigned char)buffer[col_offset+midi_offset+midi_length];
midi_length++;
printf( " with delay %5d ", delay );
// Calculate delay specified, then subtract propigation delay for 40 bits @ line speed
float usdelay = ( ( delay * 1000000 ) / tempo ) - ( ( 1000000 * 4 * 10 ) / bps );
printf( " ( %7d us )", (int)usdelay );
*/
float usdelay = 0; // set to zero since we are not doing a delay
if( tty && ! getsize )
{
// Fn XX ... XX commands are delimited with 0xFF, drop it if needed
if( delimited ) cmdlen++;
printf( "--> " );
for( int i = 0; i < cmdlen; i++ )
{
write(serial, &bytes[i], 1);
printf( "%02X ",
(unsigned char)bytes[i]);
}
printf( "--> TTY " );
}
if( ! getsize ) printf( "\r" );
if( debug ) printf( "\n" );
fflush(stdout);
// if( usdelay > 100 ) usleep( (int) usdelay );
while(usdelay > 100)
{
int thedelay;
if( usdelay > 250000 ) { thedelay = 250000; } else { thedelay = usdelay; }
if( ! getsize ) printf( " %.4s\r", &spinner[ spinner_count % 4 ] );
if( ! getsize ) fflush(stdout);
usleep( thedelay );
usdelay = usdelay - thedelay;
spinner_count--;
} // while Delay spinner
} // F0 XX ... XX FF
else
{
printf( " Error %05d: ", midi_length );
printf( "%02X <-- UNKNOWN COMMAND",
(unsigned char)buffer[col_offset+midi_offset+midi_length]);
midi_length++; // Advance 1 byte.
printf( "\r" );
if( debug ) printf( "\n" );
fflush(stdout);
}
} // while Playing
// Clear the Player line
fflush(stdout);
if( ! getsize ) cout << "\r \r";
cout.flush();
return duration;
} // if play_song
bool autobelplayer( char buffer[] )
{
int duration = autobelplayer( &buffer[0], false ); // actually play the song
return true;
}
bool patchplayer( std::string name )
{
bool retval = false;
int idx = 0;
if( debug ) cout << "patchplayer( '" << name << "' ):" << std::endl;
while( idx < patchname.size() )
{
if( patchname[idx] == name )
{
if( debug )
{
cout << "Patch `" << patchname[idx] << "' = '" << patchdata[idx] << std::endl;
std::cout << "Data: ";
for (int i = 0; (i*16) < HexToBytes( patchdata[idx].data() ).size(); ++i)
{
for (int j = 0; j < 16 && (i*16)+j < HexToBytes( patchdata[idx].data() ).size(); ++j)
printf("%02X ", (unsigned char)HexToBytes( patchdata[idx].data() ).data()[(i*16) + j]);
printf("\n ");
}
printf("\n");
fflush(stdout);
}
if( debug ) cout << "autobelplayer():" << std::endl;
retval = autobelplayer(HexToBytes( patchdata[idx].data() ).data());
break;
}
idx++;
}
return retval;
}
int main(int argc, char** argv) {
std::string cmd = argv[0];
// - - - - Parse CLI Arguments - - - -
if( argc > 0 )
{
std::string current_option;
int j;
j = 1;
while( j < argc )
{
bool parsed = false;
if ( ! parsed && j < argc ) current_option = argv[j];
OPTIONBOOL(--debug,debug,true);
if( debug ) loglevel = 7;
OPTIONBOOL(--help,showhelp,true);
OPTIONBOOL(-h,showhelp,true);
OPTIONBOOL(--play,play,true);
OPTIONBOOL(--tty,tty,true);
OPTIONBOOL(--delimited,delimited,true);
OPTIONBOOL(--play,play,true);
OPTIONBOOL(--listcolls,listcolls,true);
OPTIONBOOL(--listcollections,listcolls,true);
OPTIONBOOL(--lc,listcolls,true);
OPTIONBOOL(--listsongs,listsongs,true);
OPTIONBOOL(--ls,listsongs,true);
OPTIONINT(--collection,collection_id,1,10000);
OPTIONINT(--song,song_id,0,31);
OPTIONFLOAT(--tempo,tempo,1.0f,10000.0f);
OPTIONFLOAT(--transpose,transpose,-88.0f,88.0f);
OPTIONINTCSV(--try,tryvalue,0,256);
// is it -- ( i.e. stop parsing options )
if ( ! parsed && j < argc && ( 0 == strcmp(argv[j], "--") || 0 == strcmp(argv[j], "--ignore_rest") ) )
{
if( loglevel > 6 ) cout << "Parsed " << argv[j] << " option ( -- )" << std::endl;
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
parsed = true;
j = argc + 1; // set end condition for command line parsing
}
// is it -o output ?
if ( ! parsed && j < argc && 0 == strcmp(argv[j], "-o") )
{
std::string theoption = argv[j];
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
// look for required parameter and ensure it does not begin with a "-"
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) )
{
outFile = argv[j];
if( loglevel > 6 ) cout << "Parsed " << theoption << " " << outFile << std::endl;
// remove the file name
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
}
else
{
cout << "ERROR: option "<< theoption << " requires a filename." << std::endl;
return 1;
}
parsed = true;
}
// is it --patch
if ( ! parsed && j < argc && 0 == strcmp(argv[j], "--patchin") )
{
std::string theoption = argv[j];
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
// look for required parameter and ensure it does not begin with a "-"
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) )
{
patchin.push_back( argv[j] );
if( loglevel > 6 ) cout << "Parsed " << theoption << " " << patchin[patchin.size() -1] << std::endl;
// remove the patch name
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
}
else
{
cout << "ERROR: option "<< theoption << " requires a patch name." << std::endl;
return 1;
}
parsed = true;
}
// is it --patchout
if ( ! parsed && j < argc && 0 == strcmp(argv[j], "--patchout") )
{
std::string theoption = argv[j];
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
// look for required parameter and ensure it does not begin with a "-"
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) )
{
patchout.push_back( argv[j] );
if( loglevel > 6 ) cout << "Parsed " << theoption << " " << patchout[patchout.size() -1] << std::endl;
// remove the patch name
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
}
else
{
cout << "ERROR: option "<< theoption << " requires a patch name." << std::endl;
return 1;
}
parsed = true;
}
// is it --patch
if ( ! parsed && j < argc && 0 == strcmp(argv[j], "--patch") )
{
std::string theoption = argv[j];
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
// look for required parameter and ensure it does not begin with a "-"
if( j < argc && 0 != strncmp(argv[j], "-", (size_t) 1) )
{
patches.push_back( argv[j] );
if( loglevel > 6 ) cout << "Parsed " << theoption << " " << patches[patches.size() -1] << std::endl;
// remove the patch name
for(int i=1; i<argc; ++i)
argv[i] = argv[i+1];
--argc;
}
else
{
cout << "ERROR: option "<< theoption << " requires a patch name." << std::endl;
return 1;
}
parsed = true;
}
// if an option was not parsed AND there are more items, its likely a bad option.
if( ! parsed && argc > 2 )
{
cout << "ERROR: command line option '" << current_option << "' not valid." << std::endl;
cout << " use " << argv[0] << " -h for help." << std::endl;
}
if( ! parsed ) j++; // If an option was not found, advance the
}
}
// At this point argv[0] is the original argv[0] e.g. ./cvfind
// and argv[1] should be the last and final argument e.g. the input.pto file name
// Check argument count
// Don't report an error patches are being played
if( argc < 2 && patchin.size() + patches.size() + patchout.size() == 0 )
{
cout << "After parsing options, no input file name specified." << std::endl << std::endl;
}
else
{
// But do ensure there is actually an argument for the filename
if( argc > 1 ) inFile = argv[1];
}
// Check argument count
if( argc > 2 )
{
cout << "After parsing options, there were extraneous arguments sprcified." << std::endl << std::endl;
// display any effective arguments ( excluding popped ones )
cout << "Extraneous arguments:" << std::endl;
// Print arguments because I have not coded in 20 years
int i = 2;
while ( i < argc )
{
cout << "[ " << i << " ]" << argv[i] << std::endl;
i++;
}
cout << std::endl;
}
// Check argument count if there is no file name specified, and no patches specified, report error and help
if( argc != 2 && patchin.size() + patches.size() + patchout.size() == 0 )
{
if( showhelp )
{
cout << "Description: " << std::endl;
cout << " This is a swiss army knife for Schulmerich brand Gen 4 ( possibly earlier )" << std::endl;
cout << " Electronic Carillon systems. It can read AutoBel Card image files, display their" << std::endl;
cout << " contents, and sequence song data to a serial port for playback. The MIDI data by" << std::endl;
cout << " default includes 0xFF delimiters required by the Gen 4 system. Serial output is intended" << std::endl;
cout << " to be sent out a USB to TTL adapter which itself is attached to an easily constructed" << std::endl;
cout << " fiber optic transciever that injects data into the system." << std::endl;
cout << std::endl;
}
cout << "Usage: " << argv[0] << " [-d] [-h] [--option ...] [ -o output] [ input.dd ]" << std::endl;
cout << std::endl;
cout << " Ex: " << argv[0] << " --ls myautobelcard.bin Lists card contents" << std::endl;
cout << " " << argv[0] << " --play --ttl --song 7 --collection 1152 card.dd Plays collection 1152, song 07" << std::endl;
cout << " " << argv[0] << " --play --ttl --collection 908 songs.dd Plays all songs in collection 908" << std::endl;
if( showhelp )
{
cout << " " << argv[0] << " --lc encode_backup.dat Lists card collections" << std::endl;
cout << " " << argv[0] << " --tempo 100 --play --ttl --collection 908 songs.dd Plays them 4x faster!" << std::endl;
cout << " " << argv[0] << " --ls --tempo 100 songs.dd Lists songs, showing expected run times at 4x" << std::endl;
}
cout << std::endl;
cout << " Args: --help / -h Show help and additional info for all options" << std::endl;
cout << " --ls / --listsongs Lists songs on the specified AutoBel Card image file" << std::endl;
cout << " --lc Lists collections on the specified AutoBel Card image file" << std::endl;
if( showhelp )
{
cout << " --lp List patches in the quasimodo.patches file" << std::endl;
}
cout << " --song {list} Specifies a list of song numbers, as seen in --ls" << std::endl;
cout << " --collection {list} Specifies a list of collection numbers, as seen in --ls" << std::endl;
cout << " {list} is a comma separated list of integers. e.g. 5 or 2,9,3" << std::endl;
cout << " ranges can also be specified, e.g. 990,998-1007,1152" << std::endl;
cout << " Specifying only a song or collection list acts as a filter." << std::endl;
cout << " --play Plays AutoBel Card Songs and / or patches specified" << std::endl;
cout << " This is essentiall a dry-run for testing playability, see --ttl" << std::endl;
cout << " --tty Used with --play to actually send MIDI" << std::endl;
if( showhelp )
{
cout << " --tempo Number of delay units per second. 25 = default" << std::endl;
cout << " --patchin Plays the specified patch as a leadin" << std::endl;
cout << " --patches Plays the specified patch as if it were a song" << std::endl;
cout << " --patchout Plays the specified patch as a leadout" << std::endl;
cout << " Patches can be played alone when no input.dd file is specified" << std::endl;
cout << " --try {list} Specifies a list of integers to substitute for patch variables" << std::endl;
cout << " ( \"??\" in the .patches file ) to automate trying different value" << std::endl;
cout << " e.g. for patch \"90 ?? 7F 18\" with --try 64-96, the patch is played" << std::endl;
cout << " for each note value from 64 thru 96. A ocatonic series can be easily" << std::endl;
cout << " sequenced by using --try 40,41,43,44,46,47,49,50 or it can be used to" << std::endl;
cout << " try different parameters in control commands like \"B0 01 ?? 00\" etc." << std::endl;
cout << " --debug Vomits forth additional details" << std::endl;
cout << " -o output Saves output, if any to specified file" << std::endl;
}
cout << " input.dd Input file, a binary image file of the AutoBel / Encode Backup Card" << std::endl;
return 1;
}
// - - - - - Display "try values" list
if( tryvalue.size() != 0 ) cout << " Will Try Values: " << vi2csv( tryvalue ) << std::endl;
// - - - - - Show leadin, play, leadout patching
if( debug )
{
cout << " Lead-in Patch Sequence: " << patchin << std::endl;
cout << " Play Patch Sequence: " << patches << std::endl;
cout << "Lead-out Patch Sequence: " << patchout << std::endl;
}
// - - - - - Read Patch Table Into Memory - - - - -
//
// Patch file format:
//
// # Comment
// patch_name:patch_data
// :more_patch_data
//
// # Super Bells Are The Best Bells
// SUPER BELL : B0 01 03 10 B0 05 3F 10 # Setup channel one
// B1 01 03 10 B1 05 3F 10 # setup channel two
//
// All spaces are removed. All data after a # is ignored. A name before a ':'
// starts a new patch entry. Data after ':' is added to the current patch.
//
if( patchload )
{
std::string str ;
int line = 0;
std::fstream file;
file.open(patchFile,std::ios::in);
while(getline(file, str))
{
line++;
if( debug ) cout << patchFile << " : " << line << " : '" << str << "'" << std::endl;
// str = std::regex_replace(std::regex_replace(str, std::regex("^ +| +$|(\\S+)"), "$1"), std::regex(" {3,}"), " ");
// str.erase(std::remove_if(str.begin(), str.end(), std::isspace),str.end());
str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());
if( debug ) cout << " ( Remove Spaces ) --> '" << str << "'" << std::endl;
// If string is too short, or is a comment, skip it.
if( str.size() < 4 || str[0] == '#' ) continue;
// Remove comment and anything after
int idx_pound = str.find("#");
if( idx_pound != -1 ) str.erase(str.find ("#"), str[str.length() - 1]);
if( debug ) cout << " ( Remove Comment ) --> '" << str << "'" << std::endl;
// If string is too short, or is a comment, skip it.
if( str.length() < 2 ) continue;
// Does string contain a colon?
int idx_colon = str.find(":");
// If there is a colon, parse into patch name and patch data and start a new patch
if (idx_colon != -1)
{
// TODO: Fails when data is colon prefixed, but has no name
if (str.length() > idx_colon)
patchdata.push_back(str.substr(idx_colon + 1));
str.erase(str.find (":"), str[str.length() - 1]); // Erase the colon and everything after
if( str.length() > 0 ) patchname.push_back(str);
if( debug ) cout << "Patch '" << patchname[patchname.size()-1] << "' = '"
<< patchdata[patchdata.size()-1] << "'" << std::endl;
}
else
{
// When no colon ( patch name prefix is not present ) add string to the last patch
if( patchname.size() < 1 )
{
// If there was no last patch, error out.
if( debug ) cout << "Error: " << patchFile << " : " << line << " : No prior patch name for '"
<< str << "'" << std::endl;
continue;
}
else
{
patchdata[patchdata.size()-1] = patchdata[patchdata.size()-1] + str;
if( debug ) cout << "Patch '" << patchname[patchname.size()-1] << "' = '"
<< patchdata[patchdata.size()-1] << "'" << std::endl;
}
}
}
if( debug ) cout << "Info: Loaded " << patchname.size() << " MIDI patches from " << patchFile << std::endl;
if( debug ) cout << "Loaded Patches: " << patchname << std::endl;
}
// - - - - - Open Input File - - - - -
if( inFile.size() > 0 )
{
std::ifstream infile(inFile, std::ifstream::binary);
if (! infile )
{
std::cout << "Error: could not open file " << inFile << std::endl;
return -1;
}
else
{
// We have a open file!
cardsize = std::filesystem::file_size(inFile);
if( debug ) std::cout << "Card Size is supposedly " << cardsize << " bytes." << std::endl;
if( cardsize < MIN_CARD_SIZE )
{
std::cout << "Error: Card Size " << cardsize << " is less than " << MIN_CARD_SIZE << std::endl;
return -1;
}
if( cardsize > MAX_CARD_SIZE )
{
std::cout << "Error: Card Size " << cardsize << " greater than " << MAX_CARD_SIZE << std::endl;
return -1;
}
// Allocate a string, make it large enough to hold the input
cardbuffer.resize(cardsize);
// read the text into the string. This is totally stupid as read() does not return the number of bytes read.
// we just have to "assume" it worked.
infile.read(&cardbuffer[0], cardbuffer.size() );
infile.close();
if( debug ) std::cout << "Read " << cardsize << " from " << inFile << std::endl;
}
}
else
{
// return 0; // Continue anyway
}
/*
std::cout << "Header:" << std::endl;
for (int i = 0; i < 16; ++i)
{
for (int j = 0; j < 16; ++j)
printf("%02X ", (unsigned char)cardbuffer[(i*16) + j]);
printf("\n");
}
*/
// - - - - - - - - - - - - Serial Comms Setup - - - - - - - - - - - -
// c++ Forbidden...
// char* modem_device = "/dev/ttyS0";
serial = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY );
if (serial < 0)