-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgmd2mid.c
1817 lines (1666 loc) · 45.5 KB
/
gmd2mid.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
// GMD -> Midi Converter
// ---------------------
// Written by Valley Bell
// based on MMU -> 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 _gmd_chunk
{
UINT16 itemCnt;
UINT8 itemMode;
UINT8 itemSize;
const UINT8* data;
} GMD_CHUNK;
typedef struct _gmd_info
{
UINT8 verMinor;
UINT8 verMajor;
UINT8 gblTransp;
UINT16 tempoBPM;
UINT8 timeSigNum;
UINT8 timeSigDen;
UINT16 tickRes;
GMD_CHUNK songTitle;
GMD_CHUNK fmIns;
GMD_CHUNK ssgIns;
UINT16 trkCnt;
UINT32 trkDataPos;
} GMD_INFO;
typedef struct _track_info
{
UINT32 startOfs;
UINT32 loopOfs;
UINT32 tickCnt;
UINT32 loopTick;
UINT16 loopTimes;
} TRK_INF;
#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
static UINT8 WriteFileData(UINT32 DataLen, const UINT8* Data, const char* FileName);
static void ReadGMDChunk(UINT32 songLen, const UINT8* songData, GMD_CHUNK* gmdChk, UINT32* pos);
static UINT32 GetSongTitleLen(UINT32 txtLen, const char* txtData);
UINT8 Gmd2Mid(UINT32 songLen, const UINT8* songData);
static void WriteRPN(FILE_INF* fInf, MID_TRK_STATE* MTS, UINT8* rpnCache,
UINT8 mode, UINT8 msb, UINT8 lsb, UINT8 value);
static UINT8 GmdTrk2MidTrk(UINT32 songLen, const UINT8* songData, const GMD_INFO* gmdInf,
TRK_INF* trkInf, FILE_INF* fInf, MID_TRK_STATE* MTS);
static UINT8 PreparseGmdTrack(UINT32 songLen, const UINT8* songData, const GMD_INFO* gmdInf, TRK_INF* trkInf);
static void WritePitchBend(FILE_INF* fInf, MID_TRK_STATE* MTS, UINT16 pbBase, INT16 pbDetune);
INLINE UINT32 Tempo2Mid(UINT16 bpm, UINT16 scale);
INLINE void RcpTimeSig2Mid(UINT8 buffer[4], UINT8 beatNum, UINT8 beatDen);
static UINT8 val2shift(UINT32 value);
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay);
INLINE UINT16 ReadLE16(const UINT8* data);
INLINE INT8 Read7BitSigned(UINT8 value);
INLINE UINT16 PitchBendAddClamp(UINT16 baseVal, INT16 add);
static const UINT8 GS_RESET[0x0A] = {0xF0, 0x41, 0x10, 0x42, 0x40, 0x00, 0x7F, 0x00, 0x41, 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 UINT16 RunNoteCnt;
static RUN_NOTE RunNotes[MAX_RUN_NOTES];
static UINT16 MIDI_RES = 48;
static UINT16 NUM_LOOPS = 2;
static UINT8 NO_LOOP_EXT = 0;
static UINT8 DRIVER_BUGS = 0;
int main(int argc, char* argv[])
{
int argbase;
FILE* hFile;
UINT8 retVal;
printf("GMD -> Midi Converter\n---------------------\n");
if (argc < 3)
{
printf("Usage: gmd2mid.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(" -DriverBugs include oddities and bugs from the sound driver\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, "DriverBugs"))
DRIVER_BUGS = 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 = Gmd2Mid(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;
}
static void ReadGMDChunk(UINT32 songLen, const UINT8* songData, GMD_CHUNK* gmdChk, UINT32* pos)
{
GMD_CHUNK temp;
if (gmdChk == NULL)
gmdChk = &temp;
gmdChk->itemCnt = 0;
gmdChk->itemMode = 0;
gmdChk->itemSize = 0;
gmdChk->data = NULL;
if (*pos >= songLen)
return;
gmdChk->itemCnt = ReadLE16(&songData[*pos]);
*pos += 0x02;
if (! gmdChk->itemCnt)
return;
gmdChk->itemMode = songData[*pos + 0x00];
gmdChk->itemSize = songData[*pos + 0x01];
gmdChk->data = &songData[*pos + 0x02];
if (gmdChk->itemMode == 0)
*pos += 0x02 + gmdChk->itemCnt * gmdChk->itemSize;
else
*pos += gmdChk->itemCnt;
return;
}
static UINT32 GetSongTitleLen(UINT32 txtLen, const char* txtData)
{
// Step 1: get "real" End-Of-String (first '\0' character OR buffer end)
// Note: The buffer size is aligned to 2-byte words and includes the
// terminating \0. If that results in an odd buffer size, it is
// padded with garbage data.
const char* txtEnd = (const char*)memchr(txtData, '\0', txtLen);
if (txtEnd == NULL)
txtEnd = txtData + txtLen;
// Step 2: trim off trailing spaces
// needed by hinadori_98
while(txtEnd > txtData)
{
if (txtEnd[-1] != ' ')
break;
txtEnd --;
}
return (UINT32)(txtEnd - txtData);
}
UINT8 Gmd2Mid(UINT32 songLen, const UINT8* songData)
{
TRK_INF trkInf[18];
TRK_INF* tempTInf;
UINT8 tempArr[0x20];
GMD_INFO gmdInf;
UINT8 curTrk;
UINT32 inPos;
UINT32 tempLng;
UINT8 retVal;
FILE_INF midFInf;
MID_TRK_STATE MTS;
if (memcmp(&songData[0x00], "GMD0", 0x04))
{
printf("Not a GMDx file!\n");
return 0x80;
}
midFInf.alloc = 0x20000; // 128 KB should be enough
midFInf.data = (UINT8*)malloc(midFInf.alloc);
midFInf.pos = 0x00;
gmdInf.verMinor = songData[0x04];
gmdInf.verMajor = songData[0x05];
gmdInf.gblTransp = 0;
gmdInf.tempoBPM = ReadLE16(&songData[0x0A]);
gmdInf.timeSigNum = songData[0x0C];
gmdInf.timeSigDen = songData[0x0D];
gmdInf.tickRes = ReadLE16(&songData[0x0E]);
inPos = 0x20;
ReadGMDChunk(songLen, songData, &gmdInf.songTitle, &inPos);
ReadGMDChunk(songLen, songData, &gmdInf.fmIns, &inPos);
ReadGMDChunk(songLen, songData, &gmdInf.ssgIns, &inPos);
ReadGMDChunk(songLen, songData, NULL, &inPos); // ignored by the sound driver
ReadGMDChunk(songLen, songData, NULL, &inPos); // ignored by the sound driver
ReadGMDChunk(songLen, songData, NULL, &inPos); // ignored by the sound driver
ReadGMDChunk(songLen, songData, NULL, &inPos); // TODO: the sound driver does something here
gmdInf.trkCnt = ReadLE16(&songData[inPos]);
inPos += 0x02;
gmdInf.trkDataPos = inPos;
for (curTrk = 0; curTrk < gmdInf.trkCnt; curTrk ++)
{
tempTInf = &trkInf[curTrk];
tempTInf->startOfs = inPos;
tempTInf->loopOfs = 0x0000;
tempTInf->tickCnt = 0;
tempTInf->loopTick = 0;
if (inPos < songLen)
{
tempLng = ReadLE16(&songData[inPos]);
PreparseGmdTrack(songLen, songData, &gmdInf, tempTInf);
inPos += tempLng;
}
tempTInf->loopTimes = tempTInf->loopOfs ? NUM_LOOPS : 0;
}
if (! NO_LOOP_EXT)
BalanceTrackTimes(gmdInf.trkCnt, trkInf, MIDI_RES / 4, 0xFF);
WriteMidiHeader(&midFInf, 0x0001, gmdInf.trkCnt, MIDI_RES);
inPos = gmdInf.trkDataPos;
retVal = 0x00;
for (curTrk = 0; curTrk < gmdInf.trkCnt; curTrk ++)
{
WriteMidiTrackStart(&midFInf, &MTS);
if (curTrk == 0)
{
// first track: write global song information
if (gmdInf.songTitle.itemCnt >= 2) // 2 bytes are part of the chunk header
{
const char* title = (const char*)gmdInf.songTitle.data;
UINT32 tLen = GetSongTitleLen(gmdInf.songTitle.itemCnt - 2, title);
WriteMetaEvent(&midFInf, &MTS, 0x03, tLen, title);
}
tempLng = Tempo2Mid(gmdInf.tempoBPM, 0x40);
WriteBE32(tempArr, tempLng);
WriteMetaEvent(&midFInf, &MTS, 0x51, 0x03, &tempArr[0x01]);
RcpTimeSig2Mid(tempArr, gmdInf.timeSigNum, gmdInf.timeSigDen);
WriteMetaEvent(&midFInf, &MTS, 0x58, 0x04, tempArr);
}
retVal = GmdTrk2MidTrk(songLen, songData, &gmdInf, &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;
}
}
MidData = midFInf.data;
MidLen = midFInf.pos;
midFInf.pos = 0x00;
WriteMidiHeader(&midFInf, 0x0001, curTrk, MIDI_RES);
return retVal;
}
static void WriteRPN(FILE_INF* fInf, MID_TRK_STATE* MTS, UINT8* rpnCache,
UINT8 mode, UINT8 msb, UINT8 lsb, UINT8 value)
{
UINT8 ctrlMSB = (mode & 0x01) ? 0x63 : 0x65;
UINT8 ctrlLSB = (mode & 0x01) ? 0x62 : 0x64;
UINT8 skipRPN = 0x00;
if (rpnCache != NULL)
{
if (DRIVER_BUGS)
{
// original driver behaviour
// bug: writing RPN/NRPN may go missing when alternating between the same RPN and NRPN settings
if (rpnCache[ctrlMSB - 0x62] != msb)
rpnCache[ctrlMSB - 0x62] = msb;
else
skipRPN |= 0x01;
if (rpnCache[ctrlLSB - 0x62] != lsb)
rpnCache[ctrlLSB - 0x62] = lsb;
else
skipRPN |= 0x02;
}
else
{
// fixed behaviour
UINT8 msbC = msb | ((mode & 0x01) << 7);
UINT8 lsbC = lsb | ((mode & 0x01) << 7);
if (rpnCache[1] == msbC && rpnCache[0] == lsbC)
{
skipRPN |= 0x03;
}
else
{
rpnCache[1] = msbC;
rpnCache[0] = lsbC;
}
}
}
if (! (skipRPN & 0x01))
WriteEvent(fInf, MTS, 0xB0, ctrlMSB, msb);
if (! (skipRPN & 0x02))
WriteEvent(fInf, MTS, 0xB0, ctrlLSB, lsb);
WriteEvent(fInf, MTS, 0xB0, 0x06, value);
return;
}
static UINT8 GmdTrk2MidTrk(UINT32 songLen, const UINT8* songData, const GMD_INFO* gmdInf,
TRK_INF* trkInf, FILE_INF* fInf, MID_TRK_STATE* MTS)
{
UINT32 inPos;
UINT32 trkEndPos;
UINT32 trkLen;
UINT32 parentPos;
UINT32 trkTick;
UINT8 trkID;
UINT8 chnMode;
UINT8 noteMode;
UINT8 transp;
UINT8 startTick;
UINT16 curBar;
UINT8 trkEnd;
UINT8 cmdType;
UINT8 noteVel;
UINT8 noteVelAcc;
UINT8 chnVol;
UINT8 chnVolAcc;
UINT8 chnPan;
UINT8 modStrength;
UINT8 modDelay;
UINT8 susPedState;
UINT8 loopIdx;
UINT32 loopPPos[8];
UINT32 loopPos[8];
UINT16 loopMax[8]; // total loop count
UINT16 loopCnt[8]; // remaining loops
UINT8 syxHdr[2]; // 0 device ID, 1 model ID
UINT32 syxBufSize;
UINT8* syxBuffer;
UINT8 rpnCache[4];
UINT16 songTempo;
UINT16 curTempo;
UINT16 curTempoMod;
UINT8 noteLenMul;
UINT8 noteLenSub;
INT16 trkDetune;
INT16 pbDetune;
UINT16 pbValue;
UINT8 swModEnable;
UINT8 swModDelay;
INT16 swModStrength;
UINT16 destTempo;
UINT16 destTempoMod;
UINT32 tempoSldNextTick;
UINT8 tempoSldStpSize;
INT8 tempoSldDir;
if (trkInf->startOfs >= songLen)
return 0x01;
inPos = trkInf->startOfs;
trkLen = ReadLE16(&songData[inPos]);
trkEndPos = inPos + trkLen;
if (trkEndPos > songLen)
trkEndPos = songLen;
if (inPos + 0x10 > songLen)
return 0x01; // not enough bytes to read the header
trkID = songData[inPos + 0x02]; // track ID
transp = songData[inPos + 0x04]; // transposition?? (ignored by driver)
startTick = songData[inPos + 0x05]; // start tick (unsigned 8-bit according to driver disassembly)
inPos += 0x10;
if (transp != 0)
{
// known values are: 0x00..0x3F (+0 .. +63), 0x40..0x7F (-64 .. -1), 0x80 (drums)
printf("Warning Track %u: transpose 0x%02X!\n", trkID, transp);
transp = 0;
}
if (startTick != 0)
printf("Warning Track %u: Start Tick %+d!\n", trkID, startTick);
syxBufSize = 0x00;
syxBuffer = NULL;
syxHdr[0] = 0x10; syxHdr[1] = 0x42; // default according to driver disassembly
memset(rpnCache, 0xFF, 4);
trkEnd = 0;
chnMode = 0x00;
noteMode = 0x00;
parentPos = 0x00;
RunNoteCnt = 0x00;
MTS->midChn = 0x00;
MTS->curDly = startTick;
songTempo = gmdInf->tempoBPM;
curTempoMod = 0x40;
curTempo = songTempo;
noteVel = 100; // confirmed via driver disassembly
noteVelAcc = 6;
chnVol = 100; // confirmed via driver disassembly
chnVolAcc = 6;
chnPan = 0x40;
modDelay = 0;
modStrength = 0;
noteLenMul = 0x10;
noteLenSub = 2;
trkDetune = 0x0000;
pbDetune = 0x0000;
pbValue = 0x2000;
swModEnable = 0;
swModDelay = 0;
swModStrength = 0x0000;
susPedState = 0x00;
loopIdx = 0x00;
curBar = 0;
tempoSldStpSize = 0;
tempoSldNextTick = (UINT32)-1;
tempoSldDir = 0;
destTempoMod = curTempoMod;
destTempo = curTempo;
trkTick = MTS->curDly;
while(inPos < trkEndPos && ! trkEnd)
{
UINT32 prevPos = inPos;
if (tempoSldStpSize > 0)
{
// handle tempo slides
// used by hinadori_98/MD2_55_21.DAT
while(trkTick >= tempoSldNextTick && tempoSldDir != 0)
{
UINT32 tempoVal;
UINT8 tempArr[4];
UINT32 tempDly;
tempDly = trkTick - tempoSldNextTick;
MTS->curDly -= tempDly;
// tempo slide algorithm according to sound driver disassembly
// After applying increment/decrement, the driver actually does some
// clamping that is omitted here.
// The lower limit is 17 BPM, the upper limit is 300 BPM (CSCP) / 340 BPM (SSCP).
curTempo += tempoSldDir;
tempoSldNextTick += tempoSldStpSize;
if (curTempo == destTempo)
{
tempoSldStpSize = 0;
tempoSldNextTick = (UINT32)-1;
tempoSldDir = 0;
curTempoMod = destTempoMod;
}
tempoVal = Tempo2Mid(curTempo, 0x40);
if (curTempo == destTempo && ! DRIVER_BUGS)
{
// Try to get a slightly more accurate "end" tempo by
// recalculating it using the Tempo Modifier value.
destTempo = songTempo * destTempoMod / 0x40;
curTempo = destTempo;
tempoVal = Tempo2Mid(songTempo, destTempoMod);
}
WriteBE32(tempArr, tempoVal);
WriteMetaEvent(fInf, MTS, 0x51, 0x03, &tempArr[0x01]);
MTS->curDly += tempDly;
}
}
cmdType = songData[inPos];
if (cmdType < 0x80)
{
UINT8 curNote;
UINT8 noteDelay;
UINT8 noteLen;
UINT8 curNoteVel;
UINT8 curRN;
noteDelay = songData[inPos + 0x01];
noteLen = noteDelay;
curNoteVel = noteVel;
if (noteMode == 0)
{
noteLen = songData[inPos + 0x02];
inPos += 0x03;
}
else if (noteMode == 1)
{
curNoteVel = songData[inPos + 0x02];
noteLen = 1;
inPos += 0x03;
}
else if (noteMode == 2)
{
inPos += 0x02;
if (songData[inPos] == 0xEF) // note tie?
{
inPos += 0x01; // skip it
noteLen ++;
}
else
{
if (noteLenMul == 0x10)
noteLen -= noteLenSub;
else if (noteLenMul != 0)
noteLen = noteLen * noteLenMul / 0x10;
}
}
else if (noteMode == 3)
{
noteLen = songData[inPos + 0x02];
curNoteVel = songData[inPos + 0x03];
inPos += 0x04;
}
if (curNoteVel & 0x80)
{
INT16 vel16 = noteVel + Read7BitSigned(curNoteVel & 0x7F);
if (curNoteVel != 0x80)
printf("Relative note velocity.\n");
// The driver does proper clamping.
if (vel16 < 0x00)
vel16 = 0x00;
else if (vel16 > 0x7F)
vel16 = 0x7F;
curNoteVel = (UINT8)vel16;
}
CheckRunningNotes(fInf, &MTS->curDly, &RunNoteCnt, RunNotes);
curNote = (cmdType + gmdInf->gblTransp + 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 + noteLen;
noteLen = 0; // prevent adding note below
break;
}
}
if (noteLen > 0)
{
WriteEvent(fInf, MTS, 0x90, curNote, curNoteVel);
AddRunningNote(MAX_RUN_NOTES, &RunNoteCnt, RunNotes,
MTS->midChn, curNote, 0x80, noteLen); // The sound driver sends 9# note 00.
}
MTS->curDly += noteDelay;
trkTick += noteDelay;
}
else switch(cmdType)
{
case 0x80: // rest
{
UINT8 delayVal = songData[inPos + 0x01];
inPos += 0x02;
CheckRunningNotes(fInf, &MTS->curDly, &RunNoteCnt, RunNotes);
if (noteMode == 0 || noteMode >= 3)
{
UINT8 cutNotes = songData[inPos];
inPos += 0x01;
if (cutNotes > 0)
{
// force all notes to play for a remaining X ticks
UINT8 curRN;
//printf("Cut %u notes to %u ticks\n", RunNoteCnt, cutNotes);
for (curRN = 0; curRN < RunNoteCnt; curRN ++)
{
UINT8 noteLen = cutNotes;
if (noteMode == 2)
{
if (songData[inPos] == 0xEF) // note tie?
{
inPos += 0x01; // skip it
noteLen ++;
}
else
{
if (noteLenMul == 0x10)
noteLen -= noteLenSub;
else if (noteLenMul != 0)
noteLen = noteLen * noteLenMul / 0x10;
}
}
RunNotes[curRN].remLen = (UINT16)MTS->curDly + noteLen;
}
}
}
MTS->curDly += delayVal;
trkTick += delayVal;
}
break;
case 0x81: // delay
MTS->curDly += songData[inPos + 0x01];
trkTick += songData[inPos + 0x01];
inPos += 0x02;
break;
case 0x82: // Note Off
printf("Warning Track %u: Explicit Note Off (untested)\n", trkID);
inPos ++;
{
UINT8 curNote;
do
{
cmdType = songData[inPos];
curNote = (cmdType + gmdInf->gblTransp + transp) & 0x7F;
WriteEvent(fInf, MTS, 0x90, curNote, 0x00);
inPos ++;
} while(! (cmdType & 0x80));
}
break;
case 0x83: // Note On
printf("Warning Track %u: Explicit Note On (untested)\n", trkID);
inPos ++;
{
UINT8 curNote;
do
{
cmdType = songData[inPos + 0x00];
curNote = (cmdType + gmdInf->gblTransp + transp) & 0x7F;
WriteEvent(fInf, MTS, 0x90, curNote, songData[inPos + 0x01]);
inPos += 0x02;
} while(! (cmdType & 0x80));
}
break;
case 0x84: // Note Length Multiplicator
noteLenMul = songData[inPos + 0x01];
inPos += 0x02;
break;
case 0x85: // Note Length Subtraction
noteLenSub = songData[inPos + 0x01];
inPos += 0x02;
break;
case 0x86: // Detune Set
transp -= (INT8)(trkDetune >> 8);
trkDetune = ReadLE16(&songData[inPos + 0x01]);
inPos += 0x03;
transp += (INT8)(trkDetune >> 8);
if (trkDetune < 0 && (trkDetune & 0x00FF) == 0x00)
transp ++; // xx01..xxFF -> round up
pbDetune = (((trkDetune < 0) ? -0x100 : 0x00) | (trkDetune & 0x00FF)) << 1;
WritePitchBend(fInf, MTS, pbValue, pbDetune);
break;
case 0x87: // Detune Add
transp -= (INT8)(trkDetune >> 8);
trkDetune += ReadLE16(&songData[inPos + 0x01]);
inPos += 0x03;
transp += (INT8)(trkDetune >> 8);
if (trkDetune < 0 && (trkDetune & 0x00FF) == 0x00)
transp ++; // xx01..xxFF -> round up
pbDetune = (((trkDetune < 0) ? -0x100 : 0x00) | (trkDetune & 0x00FF)) << 1;
WritePitchBend(fInf, MTS, pbValue, pbDetune);
break;
case 0x88: // Transposition Set
transp = songData[inPos + 0x01];
inPos += 0x02;
transp += (INT8)(trkDetune >> 8);
if (trkDetune < 0 && (trkDetune & 0x00FF) == 0x00)
transp ++; // xx01..xxFF -> round up
break;
case 0x89: // Transposition Add
transp += songData[inPos + 0x01];
inPos += 0x02;
break;
case 0x8A: // Pitch Bend Reset
pbValue = 0x2000;
WritePitchBend(fInf, MTS, pbValue, pbDetune);
inPos += 0x01;
break;
case 0x8B: // Pitch Bend
pbValue = ReadLE16(&songData[inPos + 0x01]);
WritePitchBend(fInf, MTS, pbValue, pbDetune);
inPos += 0x03;
break;
case 0x8C: // Pitch Bend Add
pbValue += ReadLE16(&songData[inPos + 0x01]);
WritePitchBend(fInf, MTS, pbValue, pbDetune);
inPos += 0x03;
break;
case 0x8D: // Portamento Up
case 0x8E: // Portamento Down
printf("Warning Track %u: Portamento %s at position 0x%04X [not implemented]\n",
trkID, (cmdType == 0x8D) ? "Up" : "Down", inPos);
inPos += 0x03;
break;
case 0x8F: // Sustain Pedal
susPedState = songData[inPos + 0x01];
WriteEvent(fInf, MTS, 0xB0, 0x40, susPedState);
inPos += 0x02;
break;
case 0x90: // Channel Volume
chnVol = songData[inPos + 0x01];
WriteEvent(fInf, MTS, 0xB0, 0x07, chnVol);
inPos += 0x02;
break;
case 0x91: // Channel Volume Accumulation
{
INT16 vol16;
UINT8 accSign = songData[inPos + 0x01] & 0x80;
UINT8 volAcc = songData[inPos + 0x01] & 0x7F; // accumulation value
if (! volAcc)
volAcc = chnVolAcc; // value 0 - get cached value
else
chnVolAcc = volAcc;
if (! accSign)
{
vol16 = chnVol + volAcc;
if (vol16 > 0x7F)
vol16 = 0x7F;
}
else
{
vol16 = chnVol - volAcc;
if (vol16 < 0x00)
vol16 = 0x00;
}
chnVol = (UINT8)vol16;
inPos += 0x02;
}
WriteEvent(fInf, MTS, 0xB0, 0x07, chnVol);
break;
case 0x92: // Note Velocity
noteVel = songData[inPos + 0x01];
inPos += 0x02;
break;
case 0x93: // Note Velocity Accumulation
{
INT16 vol16;
UINT8 accSign = songData[inPos + 0x01] & 0x80;
UINT8 volAcc = songData[inPos + 0x01] & 0x7F; // accumulation value
if (! volAcc)
volAcc = noteVelAcc; // value 0 - get cached value
else
noteVelAcc = volAcc;
if (! accSign)
{
vol16 = noteVel + volAcc;
if (vol16 > 0x7F)
vol16 = 0x7F;
}
else
{
vol16 = noteVel - volAcc;
if (vol16 < 0x00)
vol16 = 0x00;
}
noteVel = (UINT8)vol16;
inPos += 0x02;
}
break;
case 0x98: // Song Tempo
{
UINT32 tempoVal;
UINT8 tempArr[4];
printf("Warning Track %u: Song Tempo change! [may break tempo modifier]\n", trkID);
songTempo = ReadLE16(&songData[inPos + 0x01]);
// Note: CSCP (valkyrie_98) clamps the tempo to 300 BPM, SSCP to 340 BPM
// I'm omitting this here.
curTempoMod = 0x40;
curTempo = songTempo;
destTempoMod = curTempoMod;
destTempo = curTempo;
tempoSldDir = 0;
inPos += 0x03;
tempoVal = Tempo2Mid(songTempo, curTempoMod);
WriteBE32(tempArr, tempoVal);
WriteMetaEvent(fInf, MTS, 0x51, 0x03, &tempArr[0x01]);
}
break;
case 0x9A: // Tempo Modifier
{
UINT32 tempoVal;
UINT8 tempArr[4];
destTempoMod = songData[inPos + 0x01];
if (! destTempoMod)
destTempoMod = 0x100; // confirmed using disassembly
// the driver does integer arithmetic without rounding here
destTempo = songTempo * destTempoMod / 0x40;
// Note: The sound driver does the same clamping as with command 0x98 here.
tempoSldStpSize = songData[inPos + 0x02]; // number of ticks between each tempo slide event
inPos += 0x03;
if (destTempo == curTempo)
tempoSldStpSize = 0;
if (tempoSldStpSize == 0)
{
curTempoMod = destTempoMod;
curTempo = destTempo;
tempoSldDir = 0;
if (DRIVER_BUGS)
tempoVal = Tempo2Mid(curTempo, 0x40); // not really a "bug", but mimic the actual driver implementation
else
tempoVal = Tempo2Mid(songTempo, curTempoMod);
WriteBE32(tempArr, tempoVal);
WriteMetaEvent(fInf, MTS, 0x51, 0x03, &tempArr[0x01]);
}
else
{
tempoSldNextTick = trkTick + tempoSldStpSize;
if (destTempo < curTempo)
tempoSldDir = -1;
else //if (destTempo > curTempo)
tempoSldDir = +1;
if (! DRIVER_BUGS && tempoSldDir > 0)
{
// Try to get to the specified tempo incl. fraction.
// This may involve an additional tempo event, so we have to recalculate
// the destination tempo with integer ceil().
destTempo = (songTempo * destTempoMod + 0x3F) / 0x40;
}
}
}
break;
case 0x9C: // Instrument
if (DRIVER_BUGS) // original driver behaviour
{
WriteEvent(fInf, MTS, 0xB0, 0x40, 0x00);
susPedState = 0x00;
FlushRunningNotes(fInf, &MTS->curDly, &RunNoteCnt, RunNotes, 1);
WriteEvent(fInf, MTS, 0xB0, 0x7B, 0x00);
}
else
{
if (susPedState & 0x40)
{
printf("Warning Track %u: Sustain Off due to new instrument at 0x%04X!\n", trkID, inPos);
WriteEvent(fInf, MTS, 0xB0, 0x40, 0x00);
}
susPedState = 0x00;
FlushRunningNotes(fInf, &MTS->curDly, &RunNoteCnt, RunNotes, 1);
}
WriteEvent(fInf, MTS, 0xC0, songData[inPos + 0x01], 0x00);
inPos += 0x02;
break;
case 0x9D: // banked Instrument
WriteEvent(fInf, MTS, 0xB0, 0x00, songData[inPos + 0x01]);
WriteEvent(fInf, MTS, 0xC0, songData[inPos + 0x02], 0x00);
inPos += 0x03;
break;
case 0x9E: // Note Aftertouch
WriteEvent(fInf, MTS, 0xA0, songData[inPos + 0x01], songData[inPos + 0x02]);
inPos += 0x03;
break;
case 0x9F: // Channel Aftertouch
WriteEvent(fInf, MTS, 0xD0, songData[inPos + 0x01], 0x00);
inPos += 0x02;
break;
case 0xA0: // Pan (via index)
{
static const UINT8 PAN_LUT[0x04] = {0x40, 0x01, 0x7F, 0x40};
UINT8 panIdx = songData[inPos + 0x01];
inPos += 0x02;
if (panIdx >= 0x04)
break; // actually causes invalid values to be read
WriteEvent(fInf, MTS, 0xB0, 0x0A, PAN_LUT[panIdx]);
}
break;
case 0xA1: // Pan
chnPan = songData[inPos + 0x01];
inPos += 0x02;
WriteEvent(fInf, MTS, 0xB0, 0x0A, chnPan);
break;
case 0xA2: // Pan Add
{
INT16 pan16;
UINT8 accSign = songData[inPos + 0x01] & 0x80;
UINT8 panAcc = songData[inPos + 0x01] & 0x7F; // accumulation value
if (! accSign)
{
pan16 = chnPan + panAcc;
if (pan16 > 0x7F)
pan16 = 0x7F;
}
else