-
Notifications
You must be signed in to change notification settings - Fork 14
/
rcp2mid.c
2298 lines (2045 loc) · 62.7 KB
/
rcp2mid.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
// RCP -> Midi Converter
// ---------------------
// Written by Valley Bell
// based on FMP -> Midi Converter
// Thanks a lot to https://github.com/shingo45endo/rcm2smf/
// for being a great reference for all the more exotic commands.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stdtype.h"
#ifndef INLINE
#if defined(_MSC_VER)
#define INLINE static __inline
#elif defined(__GNUC__)
#define INLINE static __inline__
#else
#define INLINE static inline
#endif
#endif // INLINE
#ifdef _MSC_VER
#define stricmp _stricmp
#else
#define stricmp strcasecmp
#endif
#include "midi_funcs.h"
typedef struct _file_data
{
UINT32 len;
UINT8* data;
} FILE_DATA;
typedef struct _rcp_string
{
UINT16 maxSize; // maximum size
UINT16 length; // actual length (after trimming spaces)
const char* data;
} RCP_STR;
typedef struct _user_sysex_data
{
RCP_STR name;
UINT16 dataLen;
const UINT8* data;
} USER_SYX_DATA;
typedef struct _rcp_info
{
UINT8 fileVer;
UINT16 trkCnt;
UINT16 tickRes;
UINT16 tempoBPM;
UINT8 beatNum;
UINT8 beatDen;
UINT8 keySig;
INT8 gblTransp;
RCP_STR songTitle;
UINT16 cmntLineSize;
RCP_STR comments;
RCP_STR cm6File;
RCP_STR gsdFile1;
RCP_STR gsdFile2;
USER_SYX_DATA usrSyx[8];
} RCP_INFO;
typedef struct _track_info
{
UINT32 startOfs;
UINT32 trkLen;
UINT32 loopOfs;
UINT32 tickCnt;
UINT32 loopTick;
UINT16 loopTimes;
} TRK_INF;
typedef struct _cm6_info
{
UINT8 deviceType; // 0 - MT-32, 3 - CM-64
RCP_STR comment;
// MT-32 (LA) data
const UINT8* laSystem;
const UINT8* laChnVol;
const UINT8* laPatchTemp;
const UINT8* laRhythmTemp;
const UINT8* laTimbreTemp;
const UINT8* laPatchMem;
const UINT8* laTimbreMem;
// CM-32P (PCM) data
const UINT8* pcmPatchTemp;
const UINT8* pcmPatchMem;
const UINT8* pcmSystem;
const UINT8* pcmChnVol;
} CM6_INFO;
typedef struct _gsd_info
{
const UINT8* sysParams;
const UINT8* reverbParams;
const UINT8* chorusParams;
const UINT8* partParams;
const UINT8* drumSetup;
const UINT8* masterTune;
} GSD_INFO;
#define RUNNING_NOTES
#define BALANCE_TRACK_TIMES
#include "midi_utils.h"
#define MCMD_INI_EXCLUDE 0x00 // exclude initial command
#define MCMD_INI_INCLUDE 0x01 // include initial command
#define MCMD_RET_CMDCOUNT 0x00 // return number of commands
#define MCMD_RET_DATASIZE 0x02 // return number of data bytes
#define SYXOPT_DELAY 0x01
static UINT8 ReadFileData(FILE_DATA* fData, const char* fileName);
static UINT8 WriteFileData(const FILE_DATA* fData, const char* fileName);
static const char* GetFileTitle(const char* filePath);
static const char* GetFileExt(const char* fileName);
static UINT8 GetFileVer(const FILE_DATA* rcpFile);
UINT8 Rcp2Mid(const FILE_DATA* rcpFile, FILE_DATA* midFile);
static UINT8 RcpTrk2MidTrk(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32* rcpInPos, TRK_INF* trkInf, FILE_INF* fInf, MID_TRK_STATE* MTS);
static UINT8 PreparseRcpTrack(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32 startPos, TRK_INF* trkInf);
static UINT16 GetMultiCmdDataSize(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32 startPos, UINT8 flags);
static UINT16 ReadMultiCmdData(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32* rcpInPos, UINT32 bufSize, UINT8* buffer, UINT8 flags);
static UINT16 GetTrimmedLength(UINT16 dataLen, const char* data, char trimChar, UINT8 leaveLast);
static UINT32 ReadRcpStr(RCP_STR* strInfo, UINT16 maxlen, const UINT8* data);
static UINT32 ReadRcpStr0(RCP_STR* strInfo, UINT16 maxlen, const UINT8* data);
INLINE UINT32 Tempo2Mid(UINT16 bpm, UINT8 scale);
static void RcpTimeSig2Mid(UINT8 buffer[4], UINT8 beatNum, UINT8 beatDen);
static void RcpKeySig2Mid(UINT8 buffer[2], UINT8 rcpKeySig);
static UINT8 val2shift(UINT32 value);
static UINT16 ProcessRcpSysEx(UINT16 syxMaxLen, const UINT8* syxData, UINT8* syxBuffer,
UINT8 param1, UINT8 param2, UINT8 midChn);
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay);
static void WriteRolandSyxData(FILE_INF* fInf, MID_TRK_STATE* MTS, const UINT8* syxHdr,
UINT32 address, UINT32 len, const UINT8* data, UINT8 opts);
static void WriteRolandSyxBulk(FILE_INF* fInf, MID_TRK_STATE* MTS, const UINT8* syxHdr,
UINT32 address, UINT32 len, const UINT8* data, UINT32 bulkSize, UINT8 opts);
static void WriteMetaEventFromStr(FILE_INF* fInf, MID_TRK_STATE* MTS, UINT8 metaType, const char* text);
static UINT8 Cm62MidTrk(const CM6_INFO* cm6Inf, FILE_INF* fInf, MID_TRK_STATE* MTS, UINT8 mode);
static UINT8 ParseCM6File(const FILE_DATA* cm6File, CM6_INFO* cm6Inf);
static UINT8 ParseGSDFile(const FILE_DATA* gsdFile, GSD_INFO* gsdInf);
static void Bytes2NibblesHL(UINT32 bytes, UINT8* nibData, const UINT8* byteData);
static void GsdPartParam2BulkDump(UINT8* bulkData, const UINT8* partData);
static UINT8 Gsd2MidTrk(const GSD_INFO* gsdInf, FILE_INF* fInf, MID_TRK_STATE* MTS, UINT8 mode);
UINT8 Control2Mid(const FILE_DATA* ctrlFile, FILE_DATA* midFile, UINT8 fileType, UINT8 outMode);
INLINE UINT32 MulDivCeil(UINT32 val, UINT32 mul, UINT32 div);
INLINE UINT32 MulDivRound(UINT32 val, UINT32 mul, UINT32 div);
INLINE UINT16 ReadLE16(const UINT8* data);
INLINE UINT32 ReadLE32(const UINT8* data);
static const UINT8 MT32_SYX_HDR[4] = {0x41, 0x10, 0x16, 0x12};
static const UINT8 SC55_SYX_HDR[4] = {0x41, 0x10, 0x42, 0x12};
static const UINT8 MT32_PATCH_CHG[0x07] = {0xFF, 0xFF, 0x18, 0x32, 0x0C, 0x00, 0x01};
#define MAX_RUN_NOTES 0x20 // should be more than enough even for the MIDI sequences
static UINT16 RunNoteCnt;
static RUN_NOTE RunNotes[MAX_RUN_NOTES];
static UINT16 midiTickRes = 0;
static UINT32 midiTickCount = 0;
static UINT32 midiTempoTicks = 500000;
static const char* inputFilePath = NULL;
static UINT16 NUM_LOOPS = 2;
static UINT8 NO_LOOP_EXT = 0;
static UINT8 BAR_MARKERS = 0;
static UINT8 WOLFTEAM_LOOP = 0;
static UINT8 KEEP_DUMMY_CH = 0;
static UINT8 INCLUDE_CTRL_DATA = 1;
int main(int argc, char* argv[])
{
int argbase;
int result;
UINT8 retVal;
UINT8 fileType;
FILE_DATA inFile;
FILE_DATA outFile;
printf("RCP -> Midi Converter\n---------------------\n");
if (argc < 3)
{
printf("Usage: rcp2mid.exe [options] input.bin output.mid\n");
printf("Input file formats:\n");
printf(" RCP/R36/G36 Recomposer sequence file\n");
printf(" CM6 Recomposer MT-32/CM-64 control file\n");
printf(" GSD Recomposer SC-55 control file\n");
printf("Output file formats:\n");
printf(" Sequence files are converted into MIDIs.\n");
printf(" Control files can be converted to raw SYX or MIDI.\n");
printf(" The file extension of the output file specifies the format.");
printf("\n");
printf("Options:\n");
printf(" -Loops n Loop each track at least n times. (default: 2)\n");
printf(" -NoLpExt No Loop Extension\n");
printf(" Do not fill short tracks to the length of longer ones.\n");
printf(" -WtLoop Wolfteam Loop mode (loop from measure 2 on)\n");
printf(" -KeepDummyCh convert data with MIDI channel set to -1\n");
printf(" channel -1 is invalid, some RCPs use it for muting\n");
return 0;
}
MidiDelayCallback = MidiDelayHandler;
argbase = 1;
while(argbase < argc && argv[argbase][0] == '-')
{
if (! stricmp(argv[argbase] + 1, "Loops"))
{
argbase ++;
if (argbase < argc)
{
NUM_LOOPS = (UINT16)strtoul(argv[argbase], NULL, 0);
if (! NUM_LOOPS)
NUM_LOOPS = 2;
}
}
else if (! stricmp(argv[argbase] + 1, "NoLpExt"))
NO_LOOP_EXT = 1;
else if (! stricmp(argv[argbase] + 1, "WtLoop"))
WOLFTEAM_LOOP = 1;
else if (! stricmp(argv[argbase] + 1, "KeepDummyCh"))
KEEP_DUMMY_CH = 1;
else
break;
argbase ++;
}
if (argc < argbase + 2)
{
printf("Not enough arguments.\n");
return 0;
}
inFile.data = NULL;
outFile.data = NULL;
inputFilePath = argv[argbase + 0];
retVal = ReadFileData(&inFile, inputFilePath);
if (retVal)
return 1;
result = 9;
fileType = GetFileVer(&inFile);
if (fileType < 0x10)
{
retVal = Rcp2Mid(&inFile, &outFile);
if (! retVal)
{
WriteFileData(&outFile, argv[argbase + 1]);
printf("Done.\n");
result = 0;
}
}
else if (fileType < 0x20)
{
const char* inFileExt;
UINT8 outMode;
inFileExt = GetFileExt(argv[argbase + 1]);
if (! stricmp(inFileExt, "mid"))
outMode = 0x01; // MIDI
else if (! stricmp(inFileExt, "syx"))
outMode = 0x00; // SYX
else
outMode = 0xFF;
if (outMode == 0xFF)
{
printf("Unknown output format \"%s\"!\n", inFileExt);
result = 3;
}
else
{
retVal = Control2Mid(&inFile, &outFile, fileType, outMode);
if (! retVal)
{
WriteFileData(&outFile, argv[argbase + 1]);
printf("Done.\n");
result = 0;
}
}
}
else
{
printf("Unknown file type!\n");
result = 2;
}
free(inFile.data);
free(outFile.data);
#ifdef _DEBUG
//getchar();
#endif
return result;
}
static UINT8 ReadFileData(FILE_DATA* fData, const char* fileName)
{
FILE* hFile;
hFile = fopen(fileName, "rb");
if (hFile == NULL)
{
printf("Error reading %s!\n", fileName);
return 0xFF;
}
fseek(hFile, 0x00, SEEK_END);
fData->len = ftell(hFile);
if (fData->len > 0x100000) // 1 MB
fData->len = 0x100000;
fseek(hFile, 0x00, SEEK_SET);
fData->data = (UINT8*)malloc(fData->len);
fread(fData->data, 0x01, fData->len, hFile);
fclose(hFile);
return 0x00;
}
static UINT8 WriteFileData(const FILE_DATA* fData, const char* fileName)
{
FILE* hFile;
hFile = fopen(fileName, "wb");
if (hFile == NULL)
{
printf("Error writing %s!\n", fileName);
return 0xFF;
}
fwrite(fData->data, 0x01, fData->len, hFile);
fclose(hFile);
return 0x00;
}
static const char* GetFileTitle(const char* filePath)
{
const char* dirSep;
const char* dirSepW;
dirSep = strrchr(filePath, '/');
dirSepW = strrchr(filePath, '\\');
if (dirSep == NULL)
dirSep = dirSepW;
else if (dirSepW != NULL && dirSepW > dirSep)
dirSep = dirSepW;
return (dirSep != NULL) ? (dirSep + 1) : filePath;
}
static const char* GetFileExt(const char* fileName)
{
const char* ext = strrchr(GetFileTitle(fileName), '.');
return (ext != NULL) ? (ext + 1) : "";
}
static UINT8 GetFileVer(const FILE_DATA* rcpFile)
{
const char* rcpHdr = (const char*)rcpFile->data;
if (rcpFile->len < 0x20)
return 0xFE; // incomplete header
if (! strcmp(rcpHdr, "RCM-PC98V2.0(C)COME ON MUSIC\r\n"))
return 2;
else if (! strcmp(rcpHdr, "COME ON MUSIC RECOMPOSER RCP3.0"))
return 3;
if (! strcmp(rcpHdr, "COME ON MUSIC"))
{
if (! memcmp(&rcpHdr[0x0E], "\0\0R ", 0x04))
return 0x10; // CM6 file
else if (! strcmp(&rcpHdr[0x0E], "GS CONTROL 1.0"))
return 0x11; // GSD file
}
return 0xFF; // unknown file
}
UINT8 Rcp2Mid(const FILE_DATA* rcpFile, FILE_DATA* midFile)
{
const UINT8* rcpData = rcpFile->data;
UINT8 tempArr[0x20];
RCP_INFO rcpInf;
TRK_INF* trkInf;
TRK_INF* tempTInf;
UINT16 curTrk;
UINT32 inPos;
UINT32 tempLng;
UINT8 retVal;
FILE_INF midFInf;
MID_TRK_STATE MTS;
UINT8 ctrlTrkCnt;
UINT32 initDelay;
FILE_DATA cm6FData;
CM6_INFO cm6Inf;
FILE_DATA gsd1FData;
GSD_INFO gsd1Inf;
FILE_DATA gsd2FData;
GSD_INFO gsd2Inf;
rcpInf.fileVer = GetFileVer(rcpFile);
if (rcpInf.fileVer >= 0x10)
return 0x10;
printf("RCP file version %u.\n", rcpInf.fileVer);
midFInf.alloc = 0x20000; // 128 KB should be enough
midFInf.data = (UINT8*)malloc(midFInf.alloc);
midFInf.pos = 0x00;
inPos = 0x00;
if (rcpInf.fileVer == 2)
{
ReadRcpStr(&rcpInf.songTitle, 0x40, &rcpData[inPos + 0x020]);
rcpInf.cmntLineSize = 28;
ReadRcpStr(&rcpInf.comments, 0x150, &rcpData[inPos + 0x060]);
rcpInf.tickRes = rcpData[inPos + 0x1C0];
rcpInf.tempoBPM = rcpData[inPos + 0x1C1];
rcpInf.beatNum = rcpData[inPos + 0x1C2];
rcpInf.beatDen = rcpData[inPos + 0x1C3];
rcpInf.keySig = rcpData[inPos + 0x1C4];
rcpInf.gblTransp = (INT8)rcpData[inPos + 0x1C5];
// names of additional files
ReadRcpStr0(&rcpInf.cm6File, 0x10, &rcpData[inPos + 0x1C6]);
ReadRcpStr0(&rcpInf.gsdFile1, 0x10, &rcpData[inPos + 0x1D6]);
rcpInf.gsdFile2.maxSize = rcpInf.gsdFile2.length = 0;
rcpInf.gsdFile2.data = NULL;
rcpInf.trkCnt = rcpData[inPos + 0x1E6];
rcpInf.tickRes |= (rcpData[inPos + 0x1E7] << 8);
// somewhere here it also stores the TONENAME.TB file path
inPos += 0x206;
inPos += 0x20 * 0x10; // skip rhythm definitions
}
else //if (rcpInf.fileVer == 3)
{
ReadRcpStr(&rcpInf.songTitle, 0x80, &rcpData[inPos + 0x020]);
rcpInf.cmntLineSize = 30;
ReadRcpStr(&rcpInf.comments, 0x168, &rcpData[inPos + 0x0A0]);
rcpInf.trkCnt = ReadLE16(&rcpData[inPos + 0x0208]);
rcpInf.tickRes = ReadLE16(&rcpData[inPos + 0x020A]);
rcpInf.tempoBPM = ReadLE16(&rcpData[inPos + 0x020C]);
rcpInf.beatNum = rcpData[inPos + 0x020E];
rcpInf.beatDen = rcpData[inPos + 0x020F];
rcpInf.keySig = rcpData[inPos + 0x0210];
rcpInf.gblTransp = (INT8)rcpData[inPos + 0x0211];
// names of additional files
ReadRcpStr0(&rcpInf.gsdFile1, 0x10, &rcpData[inPos + 0x298]);
ReadRcpStr0(&rcpInf.gsdFile2, 0x10, &rcpData[inPos + 0x2A8]);
ReadRcpStr0(&rcpInf.cm6File, 0x10, &rcpData[inPos + 0x2B8]);
inPos += 0x318;
inPos += 0x80 * 0x10; // skip rhythm definitions
}
if (rcpInf.trkCnt == 0)
rcpInf.trkCnt = 18; // early RCP files have the value set to 0 and assume always 18 tracks
trkInf = (TRK_INF*)calloc(rcpInf.trkCnt, sizeof(TRK_INF));
{
UINT32 pos = inPos;
pos += 0x30 * 8; // skip User SysEx data
for (curTrk = 0; curTrk < rcpInf.trkCnt; curTrk ++)
{
tempTInf = &trkInf[curTrk];
retVal = PreparseRcpTrack(rcpFile->len, rcpFile->data, &rcpInf, pos, tempTInf);
if (retVal)
break;
tempTInf->loopTimes = tempTInf->loopOfs ? NUM_LOOPS : 0;
pos += tempTInf->trkLen;
}
}
if (! NO_LOOP_EXT)
BalanceTrackTimes(rcpInf.trkCnt, trkInf, rcpInf.tickRes / 4, 0xFF);
ctrlTrkCnt = 0;
initDelay = 0;
cm6FData.data = NULL;
if (INCLUDE_CTRL_DATA)
{
const char* fileTitle = GetFileTitle(inputFilePath);
size_t baseLen = fileTitle - inputFilePath;
size_t maxPathLen = baseLen + 0x20;
char* ctrlFilePath;
ctrlFilePath = (char*)malloc(maxPathLen);
strncpy(ctrlFilePath, inputFilePath, baseLen);
if (rcpInf.cm6File.length > 0)
{
memcpy(&ctrlFilePath[baseLen], rcpInf.cm6File.data, rcpInf.cm6File.length);
ctrlFilePath[baseLen + rcpInf.cm6File.length] = '\0';
retVal = ReadFileData(&cm6FData, ctrlFilePath);
if (! retVal)
{
retVal = ParseCM6File(&cm6FData, &cm6Inf);
if (retVal)
{
printf("CM6 Control file: %.*s - Invalid file type\n",
rcpInf.cm6File.length, rcpInf.cm6File.data);
}
else
{
printf("CM6 Control File: %.*s, %s mode\n",
rcpInf.cm6File.length, rcpInf.cm6File.data, cm6Inf.deviceType ? "CM-64" : "MT-32");
ctrlTrkCnt ++;
}
}
}
if (rcpInf.gsdFile1.length > 0)
{
memcpy(&ctrlFilePath[baseLen], rcpInf.gsdFile1.data, rcpInf.gsdFile1.length);
ctrlFilePath[baseLen + rcpInf.gsdFile1.length] = '\0';
retVal = ReadFileData(&gsd1FData, ctrlFilePath);
if (! retVal)
{
retVal = ParseGSDFile(&gsd1FData, &gsd1Inf);
if (retVal)
{
printf("GSD Control file: %.*s - Invalid file type\n",
rcpInf.gsdFile1.length, rcpInf.gsdFile1.data);
}
else
{
printf("GSD Control file: %.*s\n", rcpInf.gsdFile1.length, rcpInf.gsdFile1.data);
ctrlTrkCnt ++;
}
}
}
if (rcpInf.gsdFile2.length > 0)
{
memcpy(&ctrlFilePath[baseLen], rcpInf.gsdFile2.data, rcpInf.gsdFile2.length);
ctrlFilePath[baseLen + rcpInf.gsdFile2.length] = '\0';
retVal = ReadFileData(&gsd2FData, ctrlFilePath);
if (! retVal)
{
retVal = ParseGSDFile(&gsd2FData, &gsd2Inf);
if (retVal)
{
printf("GSD Control file (Port B): %.*s - Invalid file type\n",
rcpInf.gsdFile1.length, rcpInf.gsdFile1.data);
}
else
{
printf("GSD Control file (Port B): %.*s\n", rcpInf.gsdFile2.length, rcpInf.gsdFile2.data);
ctrlTrkCnt ++;
}
}
}
}
WriteMidiHeader(&midFInf, 0x0001, 1 + ctrlTrkCnt + rcpInf.trkCnt, rcpInf.tickRes);
midiTickRes = rcpInf.tickRes;
WriteMidiTrackStart(&midFInf, &MTS);
midiTickCount = 0;
// song title
if (rcpInf.songTitle.length > 0)
WriteMetaEvent(&midFInf, &MTS, 0x03, rcpInf.songTitle.length, rcpInf.songTitle.data);
// comments
if (rcpInf.comments.length > 0)
{
// The comments section consists of 12 lines with 28 or 30 characters each.
// Lines are padded with spaces, so we have to split them manually.
UINT16 lineStart;
for (lineStart = 0; lineStart < rcpInf.comments.length; lineStart += rcpInf.cmntLineSize)
{
const char* lineData = &rcpInf.comments.data[lineStart];
// Note: Even though comments.length might tell otherwise, I can assume that the buffer
// contains cmntLineSize bytes on the last line. (works fine with all RCP files)
UINT16 lineLen = GetTrimmedLength(rcpInf.cmntLineSize, lineData, ' ', 0);
if (lineLen == 0)
lineLen = 1; // some sequencers remove empty events, so keep at least 1 space
WriteMetaEvent(&midFInf, &MTS, 0x01, lineLen, lineData);
}
}
// tempo
tempLng = Tempo2Mid(rcpInf.tempoBPM, 0x40);
midiTempoTicks = tempLng; // save in global variable for use with CM6/GSD initialization block
WriteBE32(tempArr, tempLng);
WriteMetaEvent(&midFInf, &MTS, 0x51, 0x03, &tempArr[0x01]);
if (rcpInf.beatNum > 0) // time signature being 0/0 happened in AB_AFT32.RCP
{
// time signature
RcpTimeSig2Mid(tempArr, rcpInf.beatNum, rcpInf.beatDen);
WriteMetaEvent(&midFInf, &MTS, 0x58, 0x04, tempArr);
}
// key signature
RcpKeySig2Mid(tempArr, rcpInf.keySig);
WriteMetaEvent(&midFInf, &MTS, 0x59, 0x02, tempArr);
WriteEvent(&midFInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFInf, &MTS);
if (ctrlTrkCnt > 0)
{
if (rcpInf.cm6File.length > 0)
{
WriteMidiTrackStart(&midFInf, &MTS);
midiTickCount = 0;
WriteMetaEvent(&midFInf, &MTS, 0x03, rcpInf.cm6File.length, rcpInf.cm6File.data);
WriteRolandSyxData(&midFInf, &MTS, MT32_SYX_HDR, 0x7F0000, 0x00, NULL, 0x00); // MT-32 Reset
// (N ms / 1000 ms) / (tempoTicks / 1 000 000)
MTS.curDly += MulDivRound(400, midiTickRes * 1000, midiTempoTicks); // add delay of ~400 ms
Cm62MidTrk(&cm6Inf, &midFInf, &MTS, 0x11);
initDelay += midiTickCount;
WriteEvent(&midFInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFInf, &MTS);
}
if (rcpInf.gsdFile1.length > 0)
{
WriteMidiTrackStart(&midFInf, &MTS);
midiTickCount = 0;
WriteMetaEvent(&midFInf, &MTS, 0x03, rcpInf.gsdFile1.length, rcpInf.gsdFile1.data);
if (rcpInf.gsdFile2.length > 0)
{
tempArr[0x00] = 0x00; // Port A
WriteMetaEvent(&midFInf, &MTS, 0x21, 0x01, tempArr);
}
Gsd2MidTrk(&gsd1Inf, &midFInf, &MTS, 0x11);
initDelay += midiTickCount;
WriteEvent(&midFInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFInf, &MTS);
}
if (rcpInf.gsdFile2.length > 0)
{
WriteMidiTrackStart(&midFInf, &MTS);
midiTickCount = 0;
WriteMetaEvent(&midFInf, &MTS, 0x03, rcpInf.gsdFile2.length, rcpInf.gsdFile2.data);
tempArr[0x00] = 0x01; // Port B
WriteMetaEvent(&midFInf, &MTS, 0x21, 0x01, tempArr);
Gsd2MidTrk(&gsd2Inf, &midFInf, &MTS, 0x11);
initDelay += midiTickCount;
WriteEvent(&midFInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFInf, &MTS);
}
}
// user SysEx data
for (curTrk = 0; curTrk < 8; curTrk ++)
{
USER_SYX_DATA* usrSyx = &rcpInf.usrSyx[curTrk];
inPos += ReadRcpStr(&usrSyx->name, 0x18, &rcpData[inPos]);
usrSyx->data = &rcpData[inPos];
usrSyx->dataLen = GetTrimmedLength(0x18, (const char*)usrSyx->data, (char)0xF7, 0x01);
inPos += 0x18;
}
if (initDelay > 0)
{
UINT32 barTicks;
if (rcpInf.beatNum == 0 || rcpInf.beatDen == 0)
barTicks = 4 * midiTickRes; // assume 4/4 time signature
else
barTicks = rcpInf.beatNum * 4 * midiTickRes / rcpInf.beatDen;
// round initDelay up to a full bar
initDelay = (initDelay + barTicks - 1) / barTicks * barTicks;
}
retVal = 0x00;
for (curTrk = 0; curTrk < rcpInf.trkCnt; curTrk ++)
{
WriteMidiTrackStart(&midFInf, &MTS);
midiTickCount = 0;
MTS.curDly = initDelay;
retVal = RcpTrk2MidTrk(rcpFile->len, rcpFile->data, &rcpInf, &inPos, &trkInf[curTrk], &midFInf, &MTS);
WriteEvent(&midFInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFInf, &MTS);
if (retVal)
{
if (retVal == 0x01)
{
printf("Early EOF when trying to read track %u!\n", 1 + curTrk);
retVal = 0x00; // assume that early EOF is not an error (trkCnt may be wrong)
}
break;
}
}
midFile->data = midFInf.data;
midFile->len = midFInf.pos;
midFInf.pos = 0x00;
WriteMidiHeader(&midFInf, 0x0001, 1 + ctrlTrkCnt + curTrk, rcpInf.tickRes);
free(trkInf);
return retVal;
}
static UINT8 RcpTrk2MidTrk(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32* rcpInPos, TRK_INF* trkInf, FILE_INF* fInf, MID_TRK_STATE* MTS)
{
UINT32 inPos;
UINT32 trkBasePos;
UINT32 trkEndPos;
UINT32 trkLen;
UINT32 parentPos;
UINT16 repMeasure;
UINT8 trkID;
UINT8 rhythmMode;
UINT8 midiDev;
UINT8 midChn;
INT8 transp;
INT32 startTick;
UINT8 trkMute;
UINT8 tempArr[0x40];
RCP_STR trkName;
UINT16 measPosAlloc;
UINT16 measPosCount;
UINT32* measurePos;
UINT16 curBar;
UINT8 trkEnd;
UINT8 cmdType;
UINT8 cmdP1;
UINT8 cmdP2;
UINT16 cmdP0Delay;
UINT16 cmdDurat;
UINT8 loopIdx;
UINT32 loopPPos[8];
UINT32 loopPos[8];
UINT16 loopCnt[8];
UINT8 gsParams[6]; // 0 device ID, 1 model ID, 2 address high, 3 address low
UINT8 xgParams[6]; // 0 device ID, 1 model ID, 2 address high, 3 address low
UINT32 txtBufSize;
UINT8* txtBuffer;
inPos = *rcpInPos;
if (inPos >= rcpLen)
return 0x01;
trkBasePos = inPos;
if (rcpInf->fileVer == 2)
{
trkLen = ReadLE16(&rcpData[inPos]);
// Bits 0/1 are used as 16/17, allowing for up to 256 KB per track.
// This is used by some ItoR.x conversions.
trkLen = (trkLen & ~0x03) | ((trkLen & 0x03) << 16);
inPos += 0x02;
}
else if (rcpInf->fileVer == 3)
{
trkLen = ReadLE32(&rcpData[inPos]);
inPos += 0x04;
}
trkEndPos = trkBasePos + trkLen;
if (trkEndPos > rcpLen)
trkEndPos = rcpLen;
if (inPos + 0x2A > rcpLen)
return 0x01; // not enough bytes to read the header
trkID = rcpData[inPos + 0x00]; // track ID
rhythmMode = rcpData[inPos + 0x01]; // rhythm mode
midChn = rcpData[inPos + 0x02]; // MIDI channel
if (midChn & 0x80)
{
// When the KeepDummyCh option is off, prevent events from being
// written to the MIDI by setting midiDev to 0xFF.
midiDev = KEEP_DUMMY_CH ? 0x00 : 0xFF;
midChn = 0x00;
}
else
{
midiDev = midChn >> 4;
midChn &= 0x0F;
}
transp = rcpData[inPos + 0x03]; // transposition
startTick = (INT8)rcpData[inPos + 0x04]; // start tick
trkMute = rcpData[inPos + 0x05]; // mute
ReadRcpStr(&trkName, 0x24, &rcpData[inPos + 0x06]); // track name
inPos += 0x2A;
parentPos = MTS->curDly;
MTS->curDly = 0; // enforce tick 0 for track main events
if (trkName.length > 0)
WriteMetaEvent(fInf, MTS, 0x03, trkName.length, trkName.data);
if (trkMute && ! KEEP_DUMMY_CH)
{
// just ignore muted tracks
*rcpInPos = trkBasePos + trkLen;
return 0x00;
}
if (midiDev != 0xFF)
{
WriteMetaEvent(fInf, MTS, 0x21, 1, &midiDev); // Meta Event: MIDI Port Prefix
WriteMetaEvent(fInf, MTS, 0x20, 1, &midChn); // Meta Event: MIDI Channel Prefix
}
// For rhythmMode, values 0 (melody channel) and 0x80 (rhythm channel) are common.
// Some songs use different values, but the actual meaning of the value is unknown.
if (rhythmMode != 0 && rhythmMode != 0x80)
printf("Track %u: Rhythm Mode 0x%02X\n", trkID, rhythmMode);
if (transp & 0x80)
{
// bit 7 set = rhythm channel -> ignore transposition setting
transp = 0;
}
else
{
transp = (transp & 0x40) ? (-0x80 + transp) : transp; // 7-bit -> 8-bit sign extension
transp += rcpInf->gblTransp; // add global transposition
}
MTS->curDly = parentPos;
measPosAlloc = 0x100;
measPosCount = 0x00;
measurePos = (UINT32*)malloc(measPosAlloc * sizeof(UINT32));
txtBufSize = 0x00;
txtBuffer = NULL;
memset(gsParams, 0x00, 6);
memset(xgParams, 0x00, 6);
trkEnd = 0;
parentPos = 0x00;
repMeasure = 0xFFFF;
RunNoteCnt = 0;
MTS->midChn = midChn;
loopIdx = 0x00;
curBar = 0;
// add "startTick" offset to initial delay
if (startTick >= 0 || -startTick <= (INT32)MTS->curDly)
{
MTS->curDly += startTick;
startTick = 0;
}
else
{
startTick += MTS->curDly;
MTS->curDly = 0;
}
measurePos[measPosCount] = inPos;
measPosCount ++;
while(inPos < trkEndPos && ! trkEnd)
{
UINT32 prevPos = inPos;
if (rcpInf->fileVer == 2)
{
cmdType = rcpData[inPos + 0x00];
cmdP0Delay = rcpData[inPos + 0x01];
cmdP1 = rcpData[inPos + 0x02];
cmdDurat = cmdP1;
cmdP2 = rcpData[inPos + 0x03];
inPos += 0x04;
}
else if (rcpInf->fileVer == 3)
{
cmdType = rcpData[inPos + 0x00];
cmdP2 = rcpData[inPos + 0x01];
cmdP0Delay = ReadLE16(&rcpData[inPos + 0x02]);
cmdP1 = rcpData[inPos + 0x04];
cmdDurat = ReadLE16(&rcpData[inPos + 0x04]);
inPos += 0x06;
}
if (cmdType < 0x80)
{
UINT8 curNote;
UINT8 curRN;
CheckRunningNotes(fInf, &MTS->curDly, &RunNoteCnt, RunNotes);
curNote = (cmdType + transp) & 0x7F;
for (curRN = 0; curRN < RunNoteCnt; curRN ++)
{
if (RunNotes[curRN].note == curNote)
{
// note already playing - set new length
RunNotes[curRN].remLen = MTS->curDly + cmdDurat;
cmdDurat = 0; // prevent adding note below
break;
}
}
// duration == 0 -> no note
if (cmdDurat > 0 && midiDev != 0xFF)
{
WriteEvent(fInf, MTS, 0x90, curNote, cmdP2);
AddRunningNote(MAX_RUN_NOTES, &RunNoteCnt, RunNotes, MTS->midChn, curNote, 0x80, cmdDurat);
}
}
else switch(cmdType)
{
case 0x90: case 0x91: case 0x92: case 0x93: // send User SysEx (defined via header)
case 0x94: case 0x95: case 0x96: case 0x97:
if (midiDev == 0xFF)
break;
{
const USER_SYX_DATA* usrSyx = &rcpInf->usrSyx[cmdType & 0x07];
UINT16 syxLen = ProcessRcpSysEx(usrSyx->dataLen, usrSyx->data, tempArr, cmdP1, cmdP2, midChn);
// append F7 byte (may be missing with UserSysEx of length 0x18)
if (syxLen > 0 && tempArr[syxLen - 1] != 0xF7)
{
tempArr[syxLen] = 0xF7;
syxLen ++;
}
if (usrSyx->name.length > 0 && 0)
WriteMetaEvent(fInf, MTS, 0x01, usrSyx->name.length, usrSyx->name.data);
if (syxLen > 1)
WriteLongEvent(fInf, MTS, 0xF0, syxLen, tempArr);
else
printf("Warning Track %u: Using empty User SysEx command %u at 0x%04X\n", trkID, cmdType & 0x07, prevPos);
}
break;
case 0x98: // send SysEx
{
UINT16 syxLen;
// at first, determine the size of the required buffer
syxLen = GetMultiCmdDataSize(rcpLen, rcpData, rcpInf, inPos, MCMD_INI_EXCLUDE | MCMD_RET_DATASIZE);
if (txtBufSize < (UINT32)syxLen)
{
txtBufSize = (syxLen + 0x0F) & ~0x0F; // round up to 0x10
txtBuffer = (UINT8*)realloc(txtBuffer, txtBufSize);
}
// then read input data
syxLen = ReadMultiCmdData(rcpLen, rcpData, rcpInf, &inPos, txtBufSize, txtBuffer, MCMD_INI_EXCLUDE);
if (midiDev == 0xFF)
break;
syxLen = ProcessRcpSysEx(syxLen, txtBuffer, txtBuffer, cmdP1, cmdP2, midChn);
WriteLongEvent(fInf, MTS, 0xF0, syxLen, txtBuffer);
}
break;
//case 0x99: // execute external command
case 0xC0: // DX7 Function
case 0xC1: // DX Parameter
case 0xC2: // DX RERF
case 0xC3: // TX Function
case 0xC5: // FB-01 P Parameter
case 0xC7: // TX81Z V VCED
case 0xC8: // TX81Z A ACED
case 0xC9: // TX81Z P PCED
case 0xCC: // DX7-2 R Remote SW
case 0xCD: // DX7-2 A ACED
case 0xCE: // DX7-2 P PCED
case 0xCF: // TX802 P PCED
if (midiDev == 0xFF)
break;
{
static const UINT8 DX_PARAM[0x10] = {
0x08, 0x00, 0x04, 0x11, 0xFF, 0x15, 0xFF, 0x12,
0x13, 0x10, 0xFF, 0xFF, 0x1B, 0x18, 0x19, 0x1A,
};