-
Notifications
You must be signed in to change notification settings - Fork 14
/
mdc2mid.c
993 lines (902 loc) · 24.2 KB
/
mdc2mid.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
// MDC -> Midi Converter
// ---------------------
// Written by Valley Bell, 03 October 2019
// based on TGL FMP -> Midi Converter
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.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 _track_info
{
UINT32 startOfs;
UINT32 loopOfs;
UINT32 tickCnt;
UINT32 loopTick;
UINT16 loopTimes;
UINT8 trkID;
UINT8 chnID;
UINT8 flags; // bit 0 (01) - master loop via jump, bit 4 (10) - has Expression before first note
} TRK_INF;
#define RUNNING_NOTES
#define BALANCE_TRACK_TIMES
#include "midi_utils.h"
UINT8 Mdc2Mid(UINT32 SongLen, const UINT8* SongData);
INLINE UINT16 ReadVarLenInt(UINT32 SongLen, const UINT8* SongData, UINT32* pos);
INLINE void WritePitchBend(FILE_INF* fInf, MID_TRK_STATE* MTS, INT16 bend);
static void PreparseMdc(UINT32 SongLen, const UINT8* SongData, TRK_INF* trkInf, UINT8 pass);
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay);
static UINT8 WriteFileData(UINT32 dataLen, const UINT8* data, const char* fileName);
INLINE UINT32 BPM2Mid(UINT16 valBPM);
INLINE UINT16 ReadBE16(const UINT8* data);
INLINE UINT32 ReadBE32(const UINT8* data);
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 UINT16 RunNoteCnt;
static RUN_NOTE RunNotes[MAX_RUN_NOTES];
static UINT16 MIDI_RES = 0;
static UINT16 NUM_LOOPS = 2;
static UINT8 NO_LOOP_EXT = 0;
static UINT32* midWrtTick = NULL;
int main(int argc, char* argv[])
{
int argbase;
FILE* hFile;
UINT8 retVal;
printf("MDC -> Midi Converter\n---------------------\n");
if (argc < 3)
{
printf("Usage: Mdc2Mid.exe [options] input.bin output.mid\n");
printf("Options:\n");
printf(" -Loops n Loop each track at least n times. (default: %u)\n", NUM_LOOPS);
printf(" -NoLpExt No Loop Extension\n");
printf(" Do not fill short tracks to the length of longer ones.\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
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 > 0xFFFF) // 64 KB
ROMLen = 0xFFFF;
fseek(hFile, 0x00, SEEK_SET);
ROMData = (UINT8*)malloc(ROMLen);
fread(ROMData, 0x01, ROMLen, hFile);
fclose(hFile);
retVal = Mdc2Mid(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;
}
UINT8 Mdc2Mid(UINT32 SongLen, const UINT8* SongData)
{
TRK_INF trkInf[0x20];
TRK_INF* tempTInf;
UINT32 trkBaseOfs;
UINT16 trkCnt;
UINT8 curTrk;
UINT32 inPos;
FILE_INF midFileInf;
MID_TRK_STATE MTS;
UINT8 trkEnd;
UINT8 curCmd;
UINT8 LoopIdx;
UINT16 mstLoopCount;
UINT16 LoopCount[8];
UINT32 LoopStPos[8];
UINT32 LoopEndPos[8];
//UINT32 tempLng;
//UINT16 tempSht;
UINT8 tempByt;
UINT8 tempArr[4];
INT16 tempPB;
UINT8 chnMode;
UINT8 curNote;
UINT8 curNoteVol;
UINT8 flags;
INT8 noteLenMod;
UINT8 pbRange;
UINT8 portaBaseNote;
// portaState:
// bit 0 (01) - prepare portamento
// bit 1 (02) - pitch to new note
UINT8 portaState;
INT8 portaDelta;
INT16 curDetune;
UINT16 slideDurat;
INT16 slideRange;
UINT16 slideRemDurat;
INT32 slideDelta;
INT32 slideOffset;
UINT32 slideStart; // slideStart/midiTick are a hack so that I can properly place the pitch slides
UINT32 midiTick;
INT16 curChnPB;
UINT8 curChnExpr;
UINT32 sysExLen;
const char* songTitle;
UINT32 songTLen;
if (memcmp(&SongData[0x00], "MDC\x1A", 0x04))
{
printf("Invalid MDC file!\n");
MidData = NULL;
MidLen = 0x00;
return 0x80;
}
//SongLen = ReadBE32(&SongData[0x08]);
midFileInf.alloc = 0x20000; // 128 KB should be enough
midFileInf.data = (UINT8*)malloc(midFileInf.alloc);
midFileInf.pos = 0x00;
midWrtTick = &midiTick;
trkBaseOfs = ReadBE32(&SongData[0x10]);
MIDI_RES = ReadBE16(&SongData[0x2C]);
songTitle = NULL;
inPos = ReadBE32(&SongData[0x14]);
if (inPos && inPos < SongLen)
{
songTitle = (char*)&SongData[inPos];
songTLen = strlen(songTitle);
// strip off "0D 0A 1A" sequence
while(songTLen > 0 && (UINT8)songTitle[songTLen - 1] < 0x20)
songTLen --;
}
inPos = trkBaseOfs;
trkCnt = ReadBE16(&SongData[inPos]); inPos += 0x02;
if (trkCnt > 0x20)
trkCnt = 0x20;
for (curTrk = 0; curTrk < trkCnt; curTrk ++, inPos += 0x08)
{
tempTInf = &trkInf[curTrk];
tempTInf->startOfs = trkBaseOfs + ReadBE32(&SongData[inPos + 0x00]);
tempTInf->trkID = SongData[inPos + 0x04];
tempTInf->chnID = SongData[inPos + 0x05];
tempTInf->loopOfs = 0x0000;
tempTInf->tickCnt = 0;
tempTInf->loopTick = 0;
tempTInf->flags = 0x00;
PreparseMdc(SongLen, SongData, tempTInf, 0);
if (tempTInf->flags & 0x01) // loop via GoTo - parse track again to get loop tick
PreparseMdc(SongLen, SongData, tempTInf, 1);
tempTInf->loopTimes = tempTInf->loopOfs ? NUM_LOOPS : 0;
}
if (! NO_LOOP_EXT)
BalanceTrackTimes(trkCnt, trkInf, MIDI_RES / 4, 0xFF);
WriteMidiHeader(&midFileInf, 0x0001, trkCnt, MIDI_RES);
for (curTrk = 0; curTrk < trkCnt; curTrk ++)
{
tempTInf = &trkInf[curTrk];
inPos = tempTInf->startOfs;
WriteMidiTrackStart(&midFileInf, &MTS);
if (curTrk == 0)
{
if (songTitle != NULL && songTLen > 0)
WriteMetaEvent(&midFileInf, &MTS, 0x03, songTLen, songTitle);
}
trkEnd = 0;
LoopIdx = 0x00;
mstLoopCount = 0;
MTS.midChn = tempTInf->chnID & 0x0F;
if (tempTInf->chnID & 0x80)
{
// MIDI channels
}
else if ((tempTInf->chnID & 0xF0) == 0x00)
{
// FM channels
}
else if ((tempTInf->chnID & 0xF0) == 0x10)
{
// PCM channel
MTS.midChn = 0x09;
}
RunNoteCnt = 0;
chnMode = 0x00;
flags = 0x00;
curNoteVol = 0x7F; // confirmed via driver disassembly
noteLenMod = 0;
pbRange = 12; // according to the disasm, it defaults to 12
curDetune = 0;
midiTick = 0;
portaState = 0x00;
portaDelta = 0;
portaBaseNote = 0xFF;
slideOffset = 0;
curChnExpr = 106;
curChnPB = 0;
if (! (tempTInf->flags & 0x10))
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0B, curChnExpr); // default Expression value
while(inPos < SongLen)
{
if ((flags & 0x80) && slideStart < midiTick + MTS.curDly)
{
// do pitch slide
INT16 pbBase = curDetune + portaDelta * 0x2000 / pbRange;
UINT32 remTicks;
for (remTicks = MTS.curDly; remTicks > 0 && slideRemDurat > 0; slideRemDurat --)
{
if (midiTick < slideStart)
MTS.curDly = slideStart + 1 - midiTick;
else
MTS.curDly = 1;
remTicks -= MTS.curDly;
slideOffset += slideDelta;
// The formula below is a sort of stateless version. But due to slideDelta being 16.16 fixed point,
// it's accuracy is not noticeably higher.
//slideOffset = ((INT64)slideRange << 23) * (midiTick + MTS.curDly - slideStart) / pbRange / slideDurat;
tempPB = pbBase + (slideOffset >> 16);
if (tempPB != curChnPB && slideRemDurat > 1) // omit the PB where the slide is stopped
{
curChnPB = tempPB;
WritePitchBend(&midFileInf, &MTS, curChnPB);
}
}
MTS.curDly += remTicks;
if (slideRemDurat < 1)
{
flags &= ~0x80;
slideOffset = 0;
}
}
if (inPos == tempTInf->loopOfs && ! (flags & 0x10) && mstLoopCount == 0)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6F, (UINT8)mstLoopCount);
flags |= 0x10;
}
curCmd = SongData[inPos];
if (curCmd < 0x80 || (flags & 0x01))
{
UINT32 noteLen;
UINT32 noteDelay;
UINT8 noteVol;
if (flags & 0x01) // prefixed by command 0x81
{
flags &= ~0x01;
inPos ++;
noteLen = ReadVarLenInt(SongLen, SongData, &inPos);
noteDelay = noteLen;
noteVol = curNoteVol;
}
else
{
tempByt = SongData[inPos + 0x01];
inPos += 0x02;
if (tempByt & 0x80)
{
tempByt &= 0x7F;
noteLen = tempByt;
noteDelay = tempByt;
noteVol = curNoteVol;
}
else
{
noteVol = tempByt;
if (! noteVol)
noteVol = curNoteVol;
else
noteVol = tempByt;
noteLen = ReadVarLenInt(SongLen, SongData, &inPos);
noteDelay = ReadVarLenInt(SongLen, SongData, &inPos);
}
}
curNote = curCmd;
if (noteLenMod > 0)
{
if (noteLen > 0)
noteLen = (noteLen - 1) * noteLenMod / 8 + 1;
}
else if (noteLenMod < 0)
{
if (-noteLenMod < noteLen)
noteLen = noteLen + noteLenMod;
else
noteLen = 1;
}
if (portaState & 0x01)
noteLen = 0xFFFF;
if (noteLen == 0) // chord note?
{
noteLen = 0x10000; // need a workaround, because it will stop 0-length notes before the next event
}
else
{
// ignore for chords
portaDelta = (portaState & 0x02) ? (curNote - portaBaseNote) : 0;
tempPB = curDetune + portaDelta * 0x2000 / pbRange + (slideOffset >> 16);
if (tempPB != curChnPB)
{
curChnPB = tempPB;
WritePitchBend(&midFileInf, &MTS, curChnPB);
}
}
// modifying midiTick here, because CheckRunningNotes doesn't fix it by itself
midiTick += MTS.curDly;
CheckRunningNotes(&midFileInf, &MTS.curDly, &RunNoteCnt, RunNotes);
midiTick -= MTS.curDly;
if (portaState & 0x02)
{
for (tempByt = 0; tempByt < RunNoteCnt; tempByt ++)
{
// this is exactly what the driver does
if (RunNotes[tempByt].note == portaBaseNote)
{
RunNotes[tempByt].remLen = MTS.curDly + noteLen;
break;
}
}
}
else
{
for (tempByt = 0; tempByt < RunNoteCnt; tempByt ++)
{
if (RunNotes[tempByt].note == curNote)
{
RunNotes[tempByt].remLen = MTS.curDly + noteLen;
break;
}
}
if (tempByt >= RunNoteCnt)
{
WriteEvent(&midFileInf, &MTS, 0x90, curNote, noteVol);
AddRunningNote(MAX_RUN_NOTES, &RunNoteCnt, RunNotes,
MTS.midChn, curNote, 0x80, noteLen); // the MDC driver sends 9# note 00
portaBaseNote = curNote;
}
}
if (noteDelay > 0)
{
// special note length handling to fix chords
// (The first few notes have length 0 and the driver always overwrites "length 0" with the current length.)
for (tempByt = 0; tempByt < RunNoteCnt; tempByt ++)
{
if (RunNotes[tempByt].remLen == MTS.curDly + 0x10000)
RunNotes[tempByt].remLen = MTS.curDly + noteLen;
}
if (portaState & 0x02)
{
// when "portamento" was set for the *previous* note, set chord note length for all playing ones
// Note: Required for SRMP4, RM4M01C.MDC/RM4M01S.MDC, as it has a "chord" where
// the initial 3 notes don't match the extending notes.
for (tempByt = 0; tempByt < RunNoteCnt; tempByt ++)
RunNotes[tempByt].remLen = MTS.curDly + noteLen;
}
portaState = (portaState << 1) & 0x03; // the state is NOT refreshed on 0-delay notes
MTS.curDly += noteDelay;
}
}
else
{
switch(curCmd)
{
case 0x80: // delay
inPos ++;
// fix "length 0" notes
for (tempByt = 0; tempByt < RunNoteCnt; tempByt ++)
{
if (RunNotes[tempByt].remLen == MTS.curDly + 0x10000)
RunNotes[tempByt].remLen = MTS.curDly + 0;
}
portaState = (portaState << 1) & 0x03;
MTS.curDly += ReadVarLenInt(SongLen, SongData, &inPos);
break;
case 0x81: // note
flags |= 0x01;
inPos += 0x01;
break;
case 0x86: // prepare portamento
portaState |= 0x01;
inPos += 0x01;
break;
case 0x88: // Loop Start
LoopEndPos[LoopIdx] = 0x0000;
LoopCount[LoopIdx] = SongData[inPos + 0x01];
inPos += 0x03;
LoopStPos[LoopIdx] = inPos;
LoopIdx ++;
break;
case 0x89: // Loop End
if (! LoopIdx)
{
printf("Warning: Loop End without Loop Start on track %u at %04X\n", curTrk, inPos);
trkEnd = 1;
break;
}
LoopIdx --;
if (! LoopEndPos[LoopIdx])
LoopEndPos[LoopIdx] = inPos;
inPos += 0x03; // ignore pointer to Loop Start command for now
LoopCount[LoopIdx] --;
if (! LoopCount[LoopIdx])
break;
// loop back
inPos = LoopStPos[LoopIdx];
LoopIdx ++;
break;
case 0x8A: // Loop Exit
if (! LoopIdx)
{
printf("Warning: Loop Exit without Loop Start on track %u at %04X\n", curTrk, inPos);
trkEnd = 1;
break;
}
inPos += 0x03; // ignore pointer to Loop End command for now
if (LoopCount[LoopIdx - 1] == 1)
{
// output warning, because I have yet to find a file that uses this
//printf("Warning: Loop Exit on track %u at %04X\n", curTrk, inPos);
inPos = LoopEndPos[LoopIdx - 1]; // jump to Loop End command
}
break;
case 0x8C: // unknown/ignored
printf("Event %02X on track %u at %04X\n", curCmd, curTrk, inPos);
inPos += 0x01;
break;
case 0xA0: // set Note Velocity
case 0xA2: // set Expression
tempByt = SongData[inPos + 0x01];
if (tempByt & 0x80)
tempByt = (tempByt & 0x0F) * 0x08 + 0x07;
if (curCmd == 0xA0)
{
curNoteVol = tempByt;
}
else if (curCmd == 0xA1)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x07, tempByt);
}
else if (curCmd == 0xA2)
{
flags &= 0x02;
if (SongData[inPos + 0x01] & 0x80)
flags |= 0x02;
curChnExpr = tempByt;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0B, curChnExpr);
}
inPos += 0x02;
break;
case 0xA3: // change Expression
printf("Event %02X on track %u at %04X\n", curCmd, curTrk, inPos);
tempByt = SongData[inPos + 0x01];
if (flags & 0x02)
{
INT16 lutIdx = curChnExpr / 0x08 + (INT8)tempByt;
if (lutIdx < 0x00)
lutIdx = 0x00;
else if (lutIdx > 0x0F)
lutIdx = 0x0F;
curChnExpr = lutIdx * 0x08 + 0x07;
}
else
{
INT16 newExpr = curChnExpr + (INT8)tempByt;
if (newExpr < 0x00)
newExpr = 0x00;
else if (newExpr > 0x7F)
newExpr = 0x7F;
curChnExpr = (UINT8)newExpr;
}
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0B, curChnExpr);
inPos += 0x02;
break;
//case 0xA4: // set Main Volume
// // TODO: does this according to driver disasm, but I haven't seen it in the wild yet
// tempByt = SongData[inPos + 0x01];
// WriteEvent(&midFileInf, &MTS, 0xB0, 0x07, tempByt);
// inPos += 0x02;
// break;
case 0xA6: // set Pan
tempByt = SongData[inPos + 0x01];
if (tempByt & 0x80)
{
tempByt &= 0x03;
if (tempByt == 0x01)
tempByt = 0x00; // left
else if (tempByt == 0x02)
tempByt = 0x7F; // right
else if (tempByt == 0x03)
tempByt = 0x40; // centre
else
tempByt = 0x40; // actually invalid - use centre
}
// Note: The actual driver does NOT send an event for (tempByt >= 0x80).
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0A, tempByt);
inPos += 0x02;
break;
case 0xAA: // set Pitch Bend Range
pbRange = SongData[inPos + 0x01];
WriteEvent(&midFileInf, &MTS, 0xB0, 0x65, 0x00);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x64, 0x00);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x06, pbRange);
inPos += 0x02;
break;
case 0xAC: // set Detune (byte)
curDetune = (INT8)SongData[inPos + 0x01];
tempPB = curDetune + portaDelta * 0x2000 / pbRange + (slideOffset >> 16);
if (tempPB != curChnPB)
{
curChnPB = tempPB;
WritePitchBend(&midFileInf, &MTS, curChnPB);
}
inPos += 0x02;
break;
case 0xAE: // set Note Length Modificator
noteLenMod = (INT8)SongData[inPos + 0x01];
if (noteLenMod == 8)
noteLenMod = 0;
inPos += 0x02;
break;
case 0xB0: // set Detune (word)
curDetune = (INT16)ReadBE16(&SongData[inPos + 0x01]);
tempPB = curDetune + portaDelta * 0x2000 / pbRange + (slideOffset >> 16);
if (tempPB != curChnPB)
{
curChnPB = tempPB;
WritePitchBend(&midFileInf, &MTS, curChnPB);
}
inPos += 0x03;
break;
case 0xB8: // Pitch Slide
//printf("Track %u at %04X: Pitch Slide\n", curTrk, inPos);
slideRange = (INT16)ReadBE16(&SongData[inPos + 0x01]);
inPos += 0x03;
slideDurat = ReadVarLenInt(SongLen, SongData, &inPos);
slideDelta = ((INT64)slideRange << 23) / pbRange / slideDurat;
slideStart = midiTick + MTS.curDly;
slideRemDurat = slideDurat;
slideOffset = 0;
flags |= 0x80; // enable slide
break;
case 0xE0: // set Instrument
tempByt = SongData[inPos + 0x01];
WriteEvent(&midFileInf, &MTS, 0xC0, tempByt, 0x00);
inPos += 0x02;
break;
case 0xEC: // MIDI Controller
WriteEvent(&midFileInf, &MTS, 0xB0, SongData[inPos + 0x01], SongData[inPos + 0x02]);
inPos += 0x03;
break;
case 0xEF: // set channel mode
printf("Track %u at %04X: Channel Mode %u\n", curTrk, inPos, SongData[inPos + 0x01]);
chnMode &= 0x80;
chnMode |= SongData[inPos + 0x01];
inPos += 0x02;
break;
case 0xF0: // Tempo
{
UINT16 tempoBPM;
UINT32 midiTempo;
tempoBPM = ReadBE16(&SongData[inPos + 0x01]);
midiTempo = BPM2Mid(tempoBPM);
WriteBE32(tempArr, midiTempo);
WriteMetaEvent(&midFileInf, &MTS, 0x51, 0x03, &tempArr[0x01]);
inPos += 0x03;
}
break;
case 0xFA: // send raw MIDI data (for SysEx)
inPos ++;
sysExLen = ReadVarLenInt(SongLen, SongData, &inPos);
if (SongData[inPos] == 0xF0)
WriteLongEvent(&midFileInf, &MTS, SongData[inPos], sysExLen - 1, &SongData[inPos + 1]);
else
printf("Track %u at %04X: Sending unknown raw MIDI command %02X\n", curTrk, inPos, SongData[inPos + 0x01]);
inPos += sysExLen;
break;
case 0xFE: // Jump / Track End
{
INT16 destPos = ReadBE16(&SongData[inPos + 0x01]);
inPos += 0x03;
if (! destPos)
{
trkEnd = 1;
}
else
{
inPos += destPos;
mstLoopCount ++;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6F, (UINT8)mstLoopCount);
if (mstLoopCount >= trkInf[curTrk].loopTimes)
trkEnd = 1;
}
}
break;
default:
printf("Unknown event %02X on track %u at %04X\n", curCmd, curTrk, inPos);
//WriteEvent(&midFileInf, &MTS, 0xB0, 0x6E, curCmd & 0x7F);
inPos += 0x01;
trkEnd = 1;
break;
}
}
if (trkEnd)
break;
}
FlushRunningNotes(&midFileInf, &MTS.curDly, &RunNoteCnt, RunNotes, 0);
WriteEvent(&midFileInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFileInf, &MTS);
}
MidData = midFileInf.data;
MidLen = midFileInf.pos;
return 0x00;
}
INLINE UINT16 ReadVarLenInt(UINT32 SongLen, const UINT8* SongData, UINT32* pos)
{
UINT32 delay;
delay = 0;
while(SongData[*pos] & 0x80)
{
delay <<= 7;
delay |= (SongData[*pos] & 0x7F);
(*pos) ++;
}
delay <<= 7;
delay |= (SongData[*pos] & 0x7F);
(*pos) ++;
return (UINT16)delay;
}
INLINE void WritePitchBend(FILE_INF* fInf, MID_TRK_STATE* MTS, INT16 bend)
{
UINT16 bendVal;
if (bend < -0x2000)
bend = -0x2000;
else if (bend > +0x1FFF)
bend = +0x1FFF;
bendVal = 0x2000 + bend;
WriteEvent(fInf, MTS, 0xE0, (bendVal >> 0) & 0x7F, (bendVal >> 7) & 0x7F);
return;
}
static void PreparseMdc(UINT32 SongLen, const UINT8* SongData, TRK_INF* trkInf, UINT8 pass)
{
UINT32 inPos;
UINT16 cmdLen;
UINT8 trkEnd;
UINT8 curCmd;
UINT8 LoopIdx;
UINT16 LoopCount[8];
UINT32 LoopStPos[8];
UINT32 LoopEndPos[8];
UINT8 tempByt;
UINT8 flags;
UINT16 delay;
trkEnd = 0;
flags = 0x00;
LoopIdx = 0x00;
if (pass == 0)
{
trkInf->tickCnt = 0;
trkInf->loopOfs = 0x0000;
}
trkInf->loopTick = 0;
inPos = trkInf->startOfs;
while(inPos < SongLen && ! trkEnd)
{
if (pass == 1 && inPos == trkInf->loopOfs)
break;
curCmd = SongData[inPos];
if (curCmd < 0x80 || (flags & 0x01))
{
if (flags & 0x01) // prefixed by command 0x81
{
flags &= ~0x01;
inPos ++;
delay = ReadVarLenInt(SongLen, SongData, &inPos);
}
else
{
tempByt = SongData[inPos + 0x01];
inPos += 0x02;
if (tempByt & 0x80)
{
delay = tempByt & 0x7F;
}
else
{
ReadVarLenInt(SongLen, SongData, &inPos); // ignored
delay = ReadVarLenInt(SongLen, SongData, &inPos);
}
}
flags |= 0x10; // has note
if (pass == 0)
trkInf->tickCnt += delay;
else
trkInf->loopTick += delay;
}
else
{
cmdLen = 0x00;
switch(curCmd)
{
case 0x80: // delay
inPos ++;
delay = ReadVarLenInt(SongLen, SongData, &inPos);
if (pass == 0)
trkInf->tickCnt += delay;
else
trkInf->loopTick += delay;
break;
case 0x81: // special note??
flags |= 0x01;
cmdLen = 0x01;
break;
case 0xA2: // set Expression
if (! (flags & 0x10))
trkInf->flags |= 0x10;
inPos += 0x02;
break;
case 0x86: // prepare portamento
cmdLen = 0x01;
break;
case 0xA0: // set Note Velocity
//case 0xA1: // set Volume (just a guess)
case 0xA3: // ??
case 0xA6: // set Pan
case 0xAA: // set Pitch Bend Range
case 0xAC: // set Detune (byte)
case 0xAE: // set Note Length
case 0xEF: // set channel mode ??
case 0xE0: // set Instrument
inPos += 0x02;
break;
case 0xB0: // set Detune (word)
case 0xEC: // MIDI Controller
case 0xF0: // Tempo
inPos += 0x03;
break;
case 0xB8: // Pitch Slide
inPos += 0x04;
break;
case 0x88: // Loop Start
LoopEndPos[LoopIdx] = 0x0000;
LoopCount[LoopIdx] = SongData[inPos + 0x01];
cmdLen = 0x03;
LoopStPos[LoopIdx] = inPos;
LoopStPos[LoopIdx] = inPos + cmdLen;
LoopIdx ++;
break;
case 0x89: // Loop End
if (! LoopIdx)
{
trkEnd = 1;
break;
}
cmdLen = 0x03;
LoopIdx --;
if (! LoopEndPos[LoopIdx])
LoopEndPos[LoopIdx] = inPos;
LoopCount[LoopIdx] --;
if (! LoopCount[LoopIdx])
break;
// loop back
inPos = LoopStPos[LoopIdx];
cmdLen = 0x00;
LoopIdx ++;
break;
case 0x8A: // Loop Exit
if (! LoopIdx)
{
trkEnd = 1;
break;
}
cmdLen = 0x03;
if (LoopCount[LoopIdx - 1] == 1)
{
inPos = LoopEndPos[LoopIdx - 1]; // jump to Loop End command
cmdLen = 0x00;
}
break;
case 0xFA: // send raw MIDI data (for SysEx)
inPos ++;
inPos += ReadVarLenInt(SongLen, SongData, &inPos);
break;
case 0xFE: // Track End
{
INT16 destPos = ReadBE16(&SongData[inPos + 0x01]);
cmdLen = 0x03;
trkEnd = 1;
if (destPos)
{
trkInf->loopOfs = inPos + cmdLen + destPos;
trkInf->loopTick = (UINT32)-1;
trkInf->flags |= 0x01; // loop via GoTo
}
}
break;
default:
//printf("Preparser break\n"); getchar();
return;
}
inPos += cmdLen;
}
}
return;
}
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay)
{
if (midWrtTick != NULL)
(*midWrtTick) += *delay;
CheckRunningNotes(fInf, delay, &RunNoteCnt, RunNotes);
if (*delay)
{
UINT8 curNote;
for (curNote = 0; curNote < RunNoteCnt; curNote ++)
RunNotes[curNote].remLen -= *delay;
}
return 0x00;
}
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;
}
INLINE UINT32 BPM2Mid(UINT16 valBPM)
{
return 60000000 / 48 * MIDI_RES / valBPM;
}
INLINE UINT16 ReadBE16(const UINT8* data)
{
return (data[0x00] << 8) | (data[0x01] << 0);
}
INLINE UINT32 ReadBE32(const UINT8* data)
{
return (data[0x00] << 24) | (data[0x01] << 16) |
(data[0x02] << 8) | (data[0x03] << 0);
}