forked from ValleyBell/MidiConverters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcp2mid.c
1480 lines (1322 loc) · 37.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
#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 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;
UINT8 playBias;
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 running_note
{
UINT8 midChn;
UINT8 note;
UINT16 remLen;
} RUN_NOTE;
typedef struct _track_info
{
UINT32 startOfs;
UINT32 loopOfs;
UINT32 tickCnt;
UINT32 loopTick;
//UINT16 loopTimes;
} TRK_INFO;
#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
static UINT8 WriteFileData(UINT32 DataLen, const UINT8* Data, const char* FileName);
UINT8 Rcp2Mid(UINT32 rcpLen, const UINT8* rcpData);
static UINT8 RcpTrk2MidTrk(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32* rcpInPos, FILE_INF* fInf, MID_TRK_STATE* MTS);
static UINT8 PreparseRcpTrack(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32 startPos, TRK_INFO* 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);
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 void CheckRunningNotes(FILE_INF* fInf, UINT32* delay);
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay);
static void FlushRunningNotes(FILE_INF* fInf, MID_TRK_STATE* MTS);
static UINT16 ReadLE16(const UINT8* data);
static UINT32 ReadLE32(const UINT8* data);
static const UINT8 MT32_PATCH_CHG[0x10] =
{0x41, 0x10, 0x16, 0x12, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0x18, 0x32, 0x0C, 0x00, 0x01, 0xCC, 0xF7};
static UINT32 ROMLen;
static UINT8* ROMData;
static UINT32 MidLen;
static UINT8* MidData;
#define MAX_RUN_NOTES 0x20 // should be more than enough even for the MIDI sequences
static UINT8 RunNoteCnt;
static RUN_NOTE RunNotes[MAX_RUN_NOTES];
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;
int main(int argc, char* argv[])
{
int argbase;
FILE* hFile;
UINT8 retVal;
printf("RCP -> Midi Converter\n---------------------\n");
if (argc < 3)
{
printf("Usage: rcp2mid.exe [options] input.bin output.mid\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;
}
hFile = fopen(argv[argbase + 0], "rb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return 1;
}
fseek(hFile, 0x00, SEEK_END);
ROMLen = ftell(hFile);
if (ROMLen > 0x100000) // 1 MB
ROMLen = 0x100000;
fseek(hFile, 0x00, SEEK_SET);
ROMData = (UINT8*)malloc(ROMLen);
fread(ROMData, 0x01, ROMLen, hFile);
fclose(hFile);
retVal = Rcp2Mid(ROMLen, ROMData);
if (! retVal)
WriteFileData(MidLen, MidData, argv[argbase + 1]);
free(MidData); MidData = NULL;
printf("Done.\n");
free(ROMData); ROMData = NULL;
#ifdef _DEBUG
getchar();
#endif
return 0;
}
static UINT8 WriteFileData(UINT32 DataLen, const UINT8* Data, const char* FileName)
{
FILE* hFile;
hFile = fopen(FileName, "wb");
if (hFile == NULL)
{
printf("Error opening %s!\n", FileName);
return 0xFF;
}
fwrite(Data, 0x01, DataLen, hFile);
fclose(hFile);
return 0;
}
UINT8 Rcp2Mid(UINT32 rcpLen, const UINT8* rcpData)
{
UINT8 tempArr[0x20];
RCP_INFO rcpInf;
UINT16 curTrk;
UINT32 inPos;
UINT32 tempLng;
UINT8 retVal;
FILE_INF midFInf;
MID_TRK_STATE MTS;
rcpInf.fileVer = 0xFF;
inPos = 0x00;
if (! strcmp((const char*)&rcpData[inPos], "RCM-PC98V2.0(C)COME ON MUSIC\r\n"))
rcpInf.fileVer = 2;
else if (! strcmp((const char*)&rcpData[inPos], "COME ON MUSIC RECOMPOSER RCP3.0"))
rcpInf.fileVer = 3;
if (rcpInf.fileVer == 0xFF)
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;
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.playBias = rcpData[inPos + 0x1C5];
// names of additional files
ReadRcpStr(&rcpInf.cm6File, 0x10, &rcpData[inPos + 0x1C6]);
ReadRcpStr(&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.playBias = rcpData[inPos + 0x0211];
// names of additional files
ReadRcpStr(&rcpInf.gsdFile1, 0x10, &rcpData[inPos + 0x298]);
ReadRcpStr(&rcpInf.gsdFile2, 0x10, &rcpData[inPos + 0x2A8]);
ReadRcpStr(&rcpInf.cm6File, 0x10, &rcpData[inPos + 0x2B8]);
inPos += 0x318;
inPos += 0x80 * 0x10; // skip rhythm definitions
}
// In MDPlayer/RCP.cs, allowed values for trkCnt are 18 and 36.
// For RCP files, trkCnt == 0 is also allowed and makes it assume 36 tracks.
// Invalid values result in undefined behaviour due to not setting the rcpVer variable.
// For G36 files, invalid values fall back to 18 tracks.
if (rcpInf.trkCnt == 0)
rcpInf.trkCnt = 36;
WriteMidiHeader(&midFInf, 0x0001, 1 + rcpInf.trkCnt, rcpInf.tickRes);
WriteMidiTrackStart(&midFInf, &MTS);
// 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);
WriteBE32(tempArr, tempLng);
WriteMetaEvent(&midFInf, &MTS, 0x51, 0x03, &tempArr[0x01]);
// 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);
if (rcpInf.playBias)
printf("Warning: PlayBIAS == %u!\n", rcpInf.playBias);
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;
}
retVal = 0x00;
for (curTrk = 0; curTrk < rcpInf.trkCnt; curTrk ++)
{
WriteMidiTrackStart(&midFInf, &MTS);
retVal = RcpTrk2MidTrk(rcpLen, rcpData, &rcpInf, &inPos, &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;
}
}
MidData = midFInf.data;
MidLen = midFInf.pos;
midFInf.pos = 0x00;
WriteMidiHeader(&midFInf, 0x0001, 1 + curTrk, rcpInf.tickRes);
return retVal;
}
static UINT8 RcpTrk2MidTrk(UINT32 rcpLen, const UINT8* rcpData, const RCP_INFO* rcpInf,
UINT32* rcpInPos, FILE_INF* fInf, MID_TRK_STATE* MTS)
{
UINT32 inPos;
UINT32 trkBasePos;
UINT32 trkEndPos;
UINT32 trkLen;
TRK_INFO trkInf;
UINT32 parentPos;
UINT16 repMeasure;
UINT8 trkID;
UINT8 rhythmMode;
UINT8 midiDev;
UINT8 midChn;
UINT8 transp;
INT8 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
PreparseRcpTrack(rcpLen, rcpData, rcpInf, trkBasePos, &trkInf);
trkID = rcpData[inPos + 0x00]; // track ID
rhythmMode = rcpData[inPos + 0x01]; // rhythm mode
midChn = rcpData[inPos + 0x02]; // MIDI channel
if (midChn == 0xFF)
{
// 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;
if (trkName.length > 0)
WriteMetaEvent(fInf, MTS, 0x03, trkName.length, trkName.data);
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
}
if (rhythmMode != 0)
printf("Warning Track %u: Rhythm Mode %u!\n", trkID, rhythmMode);
if (transp > 0x80)
{
// known values are: 0x00..0x3F (+0 .. +63), 0x40..0x7F (-64 .. -1), 0x80 (drums)
printf("Warning Track %u: Key 0x%02X!\n", trkID, transp);
transp = 0x00;
}
if (startTick != 0)
printf("Warning Track %u: Start Tick %+d!\n", trkID, startTick);
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 = 0x00;
MTS->midChn = midChn;
MTS->curDly = 0;
loopIdx = 0x00;
curBar = 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);
curNote = (cmdType + transp) & 0x7F;
for (curRN = 0; curRN < RunNoteCnt; curRN ++)
{
if (RunNotes[curRN].note == curNote)
{
// note already playing - set new length
RunNotes[curRN].remLen = (UINT16)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);
if (RunNoteCnt < MAX_RUN_NOTES)
{
RunNotes[RunNoteCnt].midChn = MTS->midChn;
RunNotes[RunNoteCnt].note = curNote;
RunNotes[RunNoteCnt].remLen = cmdDurat;
RunNoteCnt ++;
}
}
}
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);
//WriteMetaEvent(fInf, MTS, 0x01, usrSyx->name.length, usrSyx->name.data);
WriteLongEvent(fInf, MTS, 0xF0, syxLen, tempArr);
}
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: // "OutsideProcessExec"?? (according to MDPlayer)
//case 0xC0: // DX7 Function
//case 0xC1: // DX Parameter
//case 0xC2: // DX RERF
//case 0xC3: // TX Function
//case 0xC5: // FB-01 P Parameter
//case 0xC6: // FB-01 S System
//case 0xC7: // TX81Z V VCED
//case 0xC8: // TX81Z A ACED
//case 0xC9: // TX81Z P PCED
//case 0xCA: // TX81Z S System
//case 0xCB: // TX81Z E EFFECT
//case 0xCC: // DX7-2 R Remote SW
//case 0xCD: // DX7-2 A ACED
//case 0xCE: // DX7-2 P PCED
//case 0xCF: // TX802 P PCED
case 0xD0: // YAMAHA Base Address
xgParams[2] = cmdP1;
xgParams[3] = cmdP2;
break;
case 0xD1: // YAMAHA Device Data
xgParams[0] = cmdP1;
xgParams[1] = cmdP2;
break;
case 0xD2: // YAMAHA Address / Parameter
xgParams[4] = cmdP1;
xgParams[5] = cmdP2;
if (midiDev == 0xFF)
break;
tempArr[0] = 0x43; // YAMAHA ID
memcpy(&tempArr[1], &xgParams[0], 6);
tempArr[7] = 0xF7;
WriteLongEvent(fInf, MTS, 0xF0, 8, tempArr);
break;
case 0xD3: // YAMAHA XG Address / Parameter
xgParams[4] = cmdP1;
xgParams[5] = cmdP2;
if (midiDev == 0xFF)
break;
tempArr[0] = 0x43; // YAMAHA ID
tempArr[1] = 0x10; // Parameter Change
tempArr[2] = 0x4C; // XG
memcpy(&tempArr[3], &xgParams[2], 4);
tempArr[7] = 0xF7;
WriteLongEvent(fInf, MTS, 0xF0, 8, tempArr);
break;
//case 0xDC: // MKS-7
case 0xDD: // Roland Base Address
gsParams[2] = cmdP1;
gsParams[3] = cmdP2;
break;
case 0xDE: // Roland Parameter
gsParams[4] = cmdP1;
gsParams[5] = cmdP2;
if (midiDev == 0xFF)
break;
{
UINT8 chkSum;
UINT8 curParam;
tempArr[0] = 0x41; // Roland ID
tempArr[1] = gsParams[0];
tempArr[2] = gsParams[1];
tempArr[3] = 0x12;
chkSum = 0x00; // initialize checksum
for (curParam = 0; curParam < 4; curParam ++)
{
tempArr[4 + curParam] = gsParams[2 + curParam];
chkSum += gsParams[2 + curParam]; // add to checksum
}
tempArr[8] = (0x100 - chkSum) & 0x7F;
tempArr[9] = 0xF7;
WriteLongEvent(fInf, MTS, 0xF0, 10, tempArr);
}
break;
case 0xDF: // Roland Device
gsParams[0] = cmdP1;
gsParams[1] = cmdP2;
break;
case 0xE2: // set GS instrument
if (midiDev == 0xFF)
break;
WriteEvent(fInf, MTS, 0xB0, 0x00, cmdP2);
WriteEvent(fInf, MTS, 0xC0, cmdP1, 0x00);
break;
case 0xE5: // "Key Scan"
printf("Warning Track %u: Key Scan command found at 0x%04X\n", trkID, prevPos);
break;
case 0xE6: // MIDI channel
//printf("Warning Track %u: Set MIDI Channel command found at 0x%04X\n", trkID, prevPos);
cmdP1 --; // It's same as in the track header, except 1 added.
if (cmdP1 == 0xFF)
{
// When the KeepDummyCh option is off, ignore the event.
// Else set midiDev to 0xFF to prevent events from being written.
if (! KEEP_DUMMY_CH)
{
midiDev = 0xFF;
midChn = 0x00;
}
}
else
{
midiDev = cmdP1 >> 4; // port ID
midChn = cmdP1 & 0x0F; // channel ID
WriteMetaEvent(fInf, MTS, 0x21, 1, &midiDev); // Meta Event: MIDI Port Prefix
WriteMetaEvent(fInf, MTS, 0x20, 1, &midChn); // Meta Event: MIDI Channel Prefix
}
MTS->midChn = midChn;
break;
case 0xE7: // Tempo Modifier
{
UINT32 tempoVal;
if (cmdP2)
printf("Warning Track %u: Interpolated Tempo Change at 0x%04X!\n", trkID, prevPos);
tempoVal = (UINT32)(60000000.0 / (rcpInf->tempoBPM * cmdP1 / 64.0) + 0.5);
tempArr[0] = (tempoVal >> 16) & 0xFF;
tempArr[1] = (tempoVal >> 8) & 0xFF;
tempArr[2] = (tempoVal >> 0) & 0xFF;
WriteMetaEvent(fInf, MTS, 0x51, 0x03, tempArr);
}
break;
case 0xEA: // Channel Aftertouch
if (midiDev == 0xFF)
break;
WriteEvent(fInf, MTS, 0xD0, cmdP1, 0x00);
break;
case 0xEB: // Control Change
if (midiDev == 0xFF)
break;
WriteEvent(fInf, MTS, 0xB0, cmdP1, cmdP2);
break;
case 0xEC: // Instrument
if (midiDev == 0xFF)
break;
if (cmdP1 < 0x80)
{
WriteEvent(fInf, MTS, 0xC0, cmdP1, 0x00);
}
else if (cmdP1 < 0xC0 && (midChn >= 1 && midChn < 9))
{
// set MT-32 instrument from user bank
// used by RCP files from Granada X68000
UINT8 chkSum;
UINT8 curPos;
memcpy(tempArr, MT32_PATCH_CHG, 0x10);
tempArr[0x06] = (midChn - 1) << 4;
tempArr[0x07] = (cmdP1 >> 6) & 0x03;
tempArr[0x08] = (cmdP1 >> 0) & 0x3F;
chkSum = 0x00; // initialize checksum
for (curPos = 0x04; curPos < 0x0E; curPos ++)
chkSum += tempArr[curPos]; // add to checksum
tempArr[0x0E] = (0x100 - chkSum) & 0x7F;
WriteLongEvent(fInf, MTS, 0xF0, 0x10, tempArr);
}
break;
case 0xED: // Note Aftertouch
if (midiDev == 0xFF)
break;
WriteEvent(fInf, MTS, 0xA0, cmdP1, cmdP2);
break;
case 0xEE: // Pitch Bend
if (midiDev == 0xFF)
break;
WriteEvent(fInf, MTS, 0xE0, cmdP1, cmdP2);
break;
case 0xF5: // Key Signature Change
// TODO: find a file that uses this
printf("Warning Track %u: Key Signature Change at 0x%04X!\n", trkID, prevPos);
RcpKeySig2Mid(tempArr, (UINT8)cmdP0Delay);
WriteMetaEvent(fInf, MTS, 0x59, 0x02, tempArr);
break;
case 0xF6: // comment
{
UINT16 txtLen;
// at first, determine the size of the required buffer
txtLen = GetMultiCmdDataSize(rcpLen, rcpData, rcpInf, inPos, MCMD_INI_INCLUDE | MCMD_RET_DATASIZE);
if (txtBufSize < txtLen)
{
txtBufSize = (txtLen + 0x0F) & ~0x0F; // round up to 0x10
txtBuffer = (UINT8*)realloc(txtBuffer, txtBufSize);
}
// then read input data
txtLen = ReadMultiCmdData(rcpLen, rcpData, rcpInf, &inPos, txtBufSize, txtBuffer, MCMD_INI_INCLUDE);
txtLen = GetTrimmedLength(txtLen, (char*)txtBuffer, ' ', 0);
WriteMetaEvent(fInf, MTS, 0x01, txtLen, txtBuffer);
}
cmdP0Delay = 0;
break;
case 0xF7: // continuation of previous command
printf("Warning Track %u: Unexpected continuation command at 0x%04X!\n", trkID, prevPos);
break;
case 0xF8: // Loop End
if (loopIdx == 0)
{
printf("Warning Track %u: Loop End without Loop Start at 0x%04X!\n", trkID, prevPos);
if (BAR_MARKERS)
{
UINT32 txtLen = sprintf((char*)tempArr, "Bad Loop End");
WriteMetaEvent(fInf, MTS, 0x07, txtLen, tempArr);
}
}
else
{
UINT8 takeLoop;
takeLoop = 0;
loopIdx --;
loopCnt[loopIdx] ++;
if (cmdP0Delay == 0)
{
// infinite loop
if (loopCnt[loopIdx] < 0x80 && midiDev != 0xFF)
WriteEvent(fInf, MTS, 0xB0, 0x6F, (UINT8)loopCnt[loopIdx]);
if (loopCnt[loopIdx] < NUM_LOOPS)
takeLoop = 1;
}
else
{
if (loopCnt[loopIdx] < cmdP0Delay)
takeLoop = 1;
}
if (BAR_MARKERS)
{
UINT32 txtLen = sprintf((char*)tempArr, "Loop %u End (%u/%u)",
1 + loopIdx, loopCnt[loopIdx], cmdP0Delay);
WriteMetaEvent(fInf, MTS, 0x07, txtLen, tempArr);
}
if (takeLoop)
{
parentPos = loopPPos[loopIdx];
inPos = loopPos[loopIdx];
loopIdx ++;
}
}
cmdP0Delay = 0;
break;
case 0xF9: // Loop Start
if (BAR_MARKERS)
{
UINT32 txtLen = sprintf((char*)tempArr, "Loop %u Start", 1 + loopIdx);
WriteMetaEvent(fInf, MTS, 0x07, txtLen, tempArr);
}
if (loopIdx >= 8)
{
printf("Error Track %u: Trying to do more than 8 nested loops at 0x%04X!\n", trkID, prevPos);
}
else
{
if (inPos == trkInf.loopOfs && midiDev != 0xFF)
WriteEvent(fInf, MTS, 0xB0, 0x6F, 0);
loopPPos[loopIdx] = parentPos; // required by YS-2・018.RCP
loopPos[loopIdx] = inPos;
loopCnt[loopIdx] = 0;
if (loopIdx > 0 && loopPos[loopIdx] == loopPos[loopIdx - 1])
loopIdx --; // ignore loop command (required by YS-2・018.RCP)
loopIdx ++;
}
cmdP0Delay = 0;
break;
case 0xFC: // repeat previous measure
{
UINT16 measureID;
UINT32 repeatPos;
UINT32 cachedPos;
if (rcpInf->fileVer == 2)
{
measureID = cmdP0Delay;
repeatPos = (cmdP2 << 8) | (cmdP1 << 0);
}
else if (rcpInf->fileVer == 3)
{
measureID = cmdP0Delay;
// cmdDurat == command ID, I have no idea why the first command has ID 0x30
repeatPos = 0x002E + (cmdDurat - 0x0030) * 0x06;
}
cmdP0Delay = 0;
if (BAR_MARKERS)
{
UINT32 txtLen = sprintf((char*)tempArr, "Repeat Bar %u", 1 + measureID);
WriteMetaEvent(fInf, MTS, 0x07, txtLen, tempArr);
}
if (measureID >= measPosCount)
{
printf("Warning Track %u: Trying to repeat invalid bar %u (have %u bars) at 0x%04X!\n",
trkID, measureID, curBar + 1, prevPos);
break;
}
if (measureID == repMeasure)
break; // prevent recursion (just for safety)
cachedPos = measurePos[measureID] - trkBasePos;
if (cachedPos != repeatPos)
printf("Warning Track %u: Repeat Measure %u: offset mismatch (0x%04X != 0x%04X) at 0x%04X!\n",
trkID, measureID, repeatPos, cachedPos, prevPos);
if (! parentPos) // this check was verified to be necessary for some files
parentPos = inPos;
repMeasure = measureID;
if (rcpInf->fileVer == 2)
{
// YS3-25.RCP relies on using the actual offset. (*Some* of its measure numbers are off by 1.)
inPos = trkBasePos + repeatPos;
}
else
{
// For RCP v3 I'm not 100% that the offset calculation is correct, so I'm using cachedPos here.
inPos = trkBasePos + cachedPos;
}
}
break;
case 0xFD: // measure end
if (measPosCount >= 0x8000) // prevent infinite loops (and seg. fault due to overflow in measPosAlloc)
{
trkEnd = 1;
break;
}
if (parentPos)
{
inPos = parentPos;
parentPos = 0x00;
repMeasure = 0xFFFF;
}
if (measPosCount >= measPosAlloc)
{
measPosAlloc *= 2;
measurePos = (UINT32*)realloc(measurePos, measPosAlloc * sizeof(UINT32));
}
measurePos[measPosCount] = inPos;
measPosCount ++;
curBar ++;
cmdP0Delay = 0;
if (BAR_MARKERS)
{
UINT32 txtLen = sprintf((char*)tempArr, "Bar %u", 1 + curBar);
WriteMetaEvent(fInf, MTS, 0x07, txtLen, tempArr);
}
if (WOLFTEAM_LOOP && measPosCount == 2)
{
loopIdx = 0;
if (midiDev != 0xFF)
WriteEvent(fInf, MTS, 0xB0, 0x6F, 0);
loopPPos[loopIdx] = parentPos;
loopPos[loopIdx] = inPos;
loopCnt[loopIdx] = 0;
loopIdx ++;
}
break;
case 0xFE: // track end
trkEnd = 1;
cmdP0Delay = 0;
if (WOLFTEAM_LOOP)
{
loopIdx = 0;
loopCnt[loopIdx] ++;
if (loopCnt[loopIdx] < 0x80 && midiDev != 0xFF)
WriteEvent(fInf, MTS, 0xB0, 0x6F, (UINT8)loopCnt[loopIdx]);
if (loopCnt[loopIdx] < NUM_LOOPS)
{
parentPos = loopPPos[loopIdx];
inPos = loopPos[loopIdx];
loopIdx ++;
trkEnd = 0;
}
}
break;
default:
printf("Warning Track %u: Unhandled RCP command 0x%02X at position 0x%04X!\n", trkID, cmdType, prevPos);
break;
} // end if (cmdType >= 0x80) / switch(cmdType)
MTS->curDly += cmdP0Delay;
} // end while(! trkEnd)
free(txtBuffer);