-
Notifications
You must be signed in to change notification settings - Fork 14
/
M2MidiDec.c
1890 lines (1610 loc) · 49.5 KB
/
M2MidiDec.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
// Model2 MIDI Decoder
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <math.h> // for powf()
#include "stdtype.h"
#include "Soundfont.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
#define strdup _strdup
#else
#define stricmp strcasecmp
#endif
#ifdef _WIN32
#include <direct.h> // for _mkdir()
#define MakeDir(x) _mkdir(x)
#else
#include <sys/stat.h> // for mkdir()
#define MakeDir(x) mkdir(x, 0755)
#endif
typedef struct _rom_data ROM_DATA;
typedef struct _sample_def SMPL_DEF;
static UINT8 LoadROMData(const char* FileName, UINT32* retSize, UINT8** retData);
static UINT8 LoadROMsMerged(size_t romCount, const char** fileNames, UINT32* retSize, UINT8** retData);
static void RomByteswap(UINT32 Size, UINT8* Data);
static UINT8 IsUpperCase(const char* text);
static void NormalizePath(char* filePath);
static void CreateDirTree(const char* dirPath);
INLINE UINT32 GetGlobalPtr(UINT32 PtrID);
INLINE const UINT8* GetRomPtr(UINT32 Address);
static void DecodeMidiData(UINT32 PtrBase, UINT8 SongID);
static UINT32 DecodeMidiSegment(UINT32 ROMStPos, UINT32 MidStPos);
static UINT32 DoCommandA0(UINT32 MidStPos, UINT8 Command, UINT8 Arg1, UINT8 Arg2);
INLINE UINT8 TrkVol2MidiVol(UINT8 Volume);
INLINE UINT8 NoteVel2MidiVol(UINT8 Velocity);
INLINE UINT8 DB2MidiVol(float DB);
INLINE UINT16 ReadBE16(const UINT8* Data);
INLINE UINT32 ReadBE24(const UINT8* Data);
INLINE UINT32 ReadBE32(const UINT8* Data);
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value);
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value);
static void ExtractInstrumentSamples(const char* fNamePrefix);
static void ExtractSample(const char* FileName, UINT16 SmplID, UINT8 FreqMod);
static void CreateSoundfont(const char* FileName);
static UINT16 GenerateSampleTable(SF2_DATA* SF2Data, UINT8** RetLoopMsk);
static UINT16 GenerateInstruments(SF2_DATA* SF2Data, UINT16 SmplCnt, const UINT8* LoopMsk, UINT8* RetDrmMask);
static void ReadInsData(const UINT8* Data, UINT8 LastNote, UINT8 CurNote, SMPL_DEF* RetSmplDef,
UINT16 SmplCnt, const UINT8* LoopMask);
static INT16 SCSPtoSF2Rate(UINT16 SCSPRate, UINT8 IsAtk);
static INT16 SCSPtoSF2Level(UINT16 SCSPLevel);
static INT16 RoundTo16(double Value);
static void AddInsBag(UINT16* BagAlloc, UINT16* BagCount, sfInstBag** Bags, UINT16 GenIdx, UINT16 ModIdx);
static void AddInsGen_S16(UINT16* GenAlloc, UINT16* GenCount, sfInstGenList** Gens, UINT16 Type, INT16 Data);
static void AddInsGen_U16(UINT16* GenAlloc, UINT16* GenCount, sfInstGenList** Gens, UINT16 Type, UINT16 Data);
static void AddInsGen_8(UINT16* GenAlloc, UINT16* GenCount, sfInstGenList** Gens, UINT16 Type, UINT8 DataL, UINT8 DataH);
static void GeneratePresets(SF2_DATA* SF2Data, UINT16 InsCnt, const UINT8* DrumMask);
static const UINT8 VELOC_DATA[0x80] =
{ 0x00, 0x09, 0x11, 0x19, 0x21, 0x25, 0x29, 0x2D, 0x31, 0x35, 0x39, 0x3D, 0x41, 0x45, 0x49, 0x4D,
0x51, 0x55, 0x59, 0x5D, 0x61, 0x69, 0x71, 0x79, 0x81, 0x85, 0x89, 0x8D, 0x91, 0x95, 0x99, 0x9D,
0xA1, 0xA3, 0xA5, 0xA7, 0xA9, 0xAB, 0xAD, 0xAF, 0xB1, 0xB3, 0xB5, 0xB7, 0xB9, 0xBB, 0xBD, 0xBF,
0xC1, 0xC5, 0xC9, 0xCD, 0xD1, 0xD5, 0xD9, 0xDD, 0xE1, 0xE1, 0xE3, 0xE3, 0xE5, 0xE5, 0xE7, 0xE7,
0xE9, 0xE9, 0xE9, 0xE9, 0xEB, 0xEB, 0xEB, 0xEB, 0xED, 0xED, 0xED, 0xED, 0xEF, 0xEF, 0xEF, 0xEF,
0xF1, 0xF1, 0xF1, 0xF1, 0xF3, 0xF3, 0xF3, 0xF3, 0xF5, 0xF5, 0xF5, 0xF5, 0xF7, 0xF7, 0xF7, 0xF7,
0xF9, 0xF9, 0xF9, 0xF9, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFB, 0xFD, 0xFD, 0xFD, 0xFD,
0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF};
static const UINT8 WAVE_Header[0x2C] =
{ 0x52, 0x49, 0x46, 0x46, 0xFF, 0xFF, 0xFF, 0xFF, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6D, 0x74, 0x20,
0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x22, 0x56, 0x00, 0x00, 0x44, 0xAC, 0x00, 0x00,
0x01, 0x00, 0x08, 0x00, 0x64, 0x61, 0x74, 0x61, 0xFF, 0xFF, 0xFF, 0xFF};
#define MODE_MIDI 0x01 // convert sequences to MIDI
#define MODE_WAV 0x10 // dump samples to WAV
#define MODE_SF2 0x11 // dump samples to SF2 soundfonts
#define MODE_NONE 0xFF
typedef struct _rom_data
{
UINT32 Size;
UINT8* Data;
} ROM_DATA;
typedef struct _sample_def
{
UINT8 MinNote;
UINT8 MaxNote;
UINT8 RootNote;
UINT8 LoopMode;
UINT16 SmplID;
INT16 AtkRate; // Attack Rate
INT16 DecRate; // Decay Rate
INT16 SusRate; // Sustain Rate
INT16 RelRate; // Release Rate
UINT16 SusLvl; // Sustain Level
} SMPL_DEF;
static UINT32 ROMSize;
static UINT8* ROMData;
static ROM_DATA SmpROM;
static UINT32 MidSize;
static UINT8* MidData;
static UINT8 FixVolume = 1; // convert volume to MIDI scale
static UINT8 FixDrumNotes = 0; // turn off endlessly playing drum notes
// 01 - end when replaying note of segment ends
// 02 - end immediately
static UINT8 PatchDrumChn = 0; // swap MIDI channels 0F and 09
static UINT16 NUM_LOOPS = 2;
static UINT32 GLOBAL_PTR_OFS = 0x008000;
static const char* OUT_PREFIX = NULL;
static UINT8 Mode;
#define SMPL_DATA_ID 0x00 // Offset 0x008000
#define INS_DATA_ID 0x01 // Offset 0x008004
#define TRK_INIT_ID 0x03 // Offset 0x00800C
#define PLAYLIST_ID 0x07 // Offset 0x00801C
#define VEL_DATA_ID 0x0A // Offset 0x008028
#define PTR_GROUP 0x00
static UINT8 RunningDrmNotes[0x80];
int main(int argc, char* argv[])
{
FILE* hFile;
char* FileName;
UINT8 SongCnt;
UINT8 CurSng;
UINT32 BasePos;
UINT32 CurPos;
int argbase;
UINT8 retVal;
UINT32 drvInitAddr;
UINT16 driverVer;
UINT8 outPrefUCase;
printf("Model2 MIDI Decoder\n-------------------\n");
if (argc < 3)
{
printf("Usage: %s [-options] mode sound_program.bin [samples1.bin] ... [samples4.bin]\n", argv[0]);
printf("Modes:\n");
printf(" mus - extract music to MIDI files (default: SONG_##.MID)\n");
printf(" wav - extract samples as WAV files (default: Ins##_n##_Smpl##.wav)\n");
printf(" sf2 - extract samples as SF2 soundfont (default: out.sf2)\n");
printf("Options:\n");
printf(" -o out set output name prefix to \"out\"\n");
printf(" -l n loop song n times (default: %u)\n", NUM_LOOPS);
printf(" -v do NOT convert volume to MIDI scale\n");
printf(" -c swap MIDI channels 10 and 16 (some games have drum on ch16)\n");
printf(" -d n cut drum notes\n");
printf(" 0 - don't cut (default)\n");
printf(" 1 - stop when replaying note or when segment ends\n");
printf(" 2 - stop immediately\n");
printf("\n");
printf("The sound program ROM is the file loaded into the \"audiocpu\" region in MAME.\n");
printf("It is often called epr-XXXXX.30 or epr-XXXXX.31\n");
printf("\n");
printf("Byteswapped ROMs are detected and fixed automatically\n");
printf("Currently only driver version v2 (entry offset 0x601200) is supported.\n");
return 1;
}
Mode = MODE_NONE;
argbase = 1;
while(argbase < argc && argv[argbase][0] == '-')
{
if (! stricmp(argv[argbase] + 1, "o"))
{
argbase ++;
if (argbase < argc)
OUT_PREFIX = argv[argbase];
}
else if (! stricmp(argv[argbase] + 1, "l"))
{
argbase ++;
if (argbase < argc)
{
NUM_LOOPS = (UINT16)strtoul(argv[argbase], NULL, 0);
if (! NUM_LOOPS)
NUM_LOOPS = 2;
}
}
else if (! stricmp(argv[argbase] + 1, "v"))
{
FixVolume = 0;
}
else if (! stricmp(argv[argbase] + 1, "c"))
{
PatchDrumChn = 1;
}
else if (! stricmp(argv[argbase] + 1, "d"))
{
argbase ++;
if (argbase < argc)
FixDrumNotes = (UINT8)strtoul(argv[argbase], NULL, 0);
}
else
break;
argbase ++;
}
if (argc < argbase + 2)
{
printf("Insufficient arguments!\n");
return 1;
}
if (! stricmp(argv[argbase + 0], "mus"))
Mode = MODE_MIDI;
else if (! stricmp(argv[argbase + 0], "wav"))
Mode = MODE_WAV;
else if (! stricmp(argv[argbase + 0], "sf2"))
Mode = MODE_SF2;
if (Mode == MODE_NONE)
{
printf("Invalid mode!\n");
return 1;
}
retVal = LoadROMData(argv[argbase + 1], &ROMSize, &ROMData);
if (retVal > 0x00)
{
printf("Unable to load Sound Program ROM: %s\n", argv[argbase + 1]);
return 1;
}
if (Mode & 0x10)
{
if (argbase + 2 >= argc)
{
printf("Sample ROMs are required for sample dumping!\n");
free(ROMData);
return 2;
}
LoadROMsMerged(argc - (argbase + 2), &argv[argbase + 2], &SmpROM.Size, &SmpROM.Data);
}
if (ReadBE16(&ROMData[0x04]) == 0x6000)
{
printf("This ROM is byteswapped.\n");
printf("Swapping bytes again to get a clean ROM ...\n");
printf(" Main ROM ...");
fflush(stdout);
RomByteswap(ROMSize, ROMData);
printf(" Done.\n");
if (SmpROM.Size > 0x00)
{
printf(" Sample ROM ...");
fflush(stdout);
RomByteswap(SmpROM.Size, SmpROM.Data);
printf(" Done.\n");
}
}
drvInitAddr = ReadBE32(&ROMData[0x04]);
if (drvInitAddr == 0x600100)
driverVer = 1;
else if (drvInitAddr == 0x601200)
driverVer = 2;
else if (drvInitAddr == 0x600120)
driverVer = 3;
else
driverVer = 0xFF;
if (driverVer != 2)
{
printf("Unsupported sound driver version!\n");
if (SmpROM.Data != NULL)
free(SmpROM.Data);
free(ROMData);
return 3;
}
switch(Mode)
{
case MODE_MIDI:
if (OUT_PREFIX == NULL)
OUT_PREFIX = "SONG_";
outPrefUCase = IsUpperCase(OUT_PREFIX);
FileName = (char*)malloc(strlen(OUT_PREFIX) + 0x10);
MidSize = 0x100000;
MidData = (UINT8*)malloc(MidSize);
BasePos = GetGlobalPtr(PLAYLIST_ID) & 0x7FFFF;
CurPos = BasePos + ReadBE16(&ROMData[BasePos + 2 + PTR_GROUP * 2]); // get Pointer Group Offset
SongCnt = ReadBE16(&ROMData[CurPos]) + 1;
for (CurSng = 0x00; CurSng < SongCnt; CurSng ++)
{
printf("Song %02X / %02X\n", CurSng, SongCnt);
DecodeMidiData(GetGlobalPtr(PLAYLIST_ID) & 0x7FFFF, CurSng);
sprintf(FileName, "%s%02X.%s", OUT_PREFIX, CurSng, (outPrefUCase ? "MID" : "mid"));
hFile = fopen(FileName, "wb");
if (hFile == NULL)
{
printf("Error saving %s!\n", FileName);
continue;
}
fwrite(MidData, 0x01, MidSize, hFile);
fclose(hFile);
}
free(MidData); MidData = NULL;
free(FileName);
break;
case MODE_WAV:
if (OUT_PREFIX == NULL)
OUT_PREFIX = "";
printf("Extracting Samples to .wav format ...");
fflush(stdout);
ExtractInstrumentSamples(OUT_PREFIX);
break;
case MODE_SF2:
if (OUT_PREFIX == NULL)
OUT_PREFIX = "out";
outPrefUCase = IsUpperCase(OUT_PREFIX);
FileName = (char*)malloc(strlen(OUT_PREFIX) + 0x10);
sprintf(FileName, "%s.%s", OUT_PREFIX, (outPrefUCase ? "SF2" : "sf2"));
printf("Creating SoundFont ...");
fflush(stdout);
CreateSoundfont(FileName);
free(FileName);
break;
}
printf(" Done.\n");
if (SmpROM.Data != NULL)
free(SmpROM.Data);
free(ROMData);
#ifdef _DEBUG
getchar();
#endif
return 0;
}
static UINT8 LoadROMData(const char* FileName, UINT32* retSize, UINT8** retData)
{
FILE* hFile;
hFile = fopen(FileName, "rb");
if (hFile == NULL)
{
*retSize = 0x00;
*retData = NULL;
return 0xFF;
}
fseek(hFile, 0, SEEK_END);
*retSize = ftell(hFile);
*retData = (UINT8*)malloc(*retSize);
if (*retData == NULL)
{
fclose(hFile);
return 0x80;
}
fseek(hFile, 0, SEEK_SET);
fread(*retData, 0x01, *retSize, hFile);
fclose(hFile);
return 0x00;
}
static UINT8 LoadROMsMerged(size_t romCount, const char** fileNames, UINT32* retSize, UINT8** retData)
{
ROM_DATA* tmpROM;
size_t curROM;
UINT32 baseOfs;
UINT8 resVal;
UINT8 retVal;
resVal = 0x00;
tmpROM = (ROM_DATA*)calloc(romCount, sizeof(ROM_DATA));
for (curROM = 0; curROM < romCount; curROM ++)
{
retVal = LoadROMData(fileNames[curROM], &tmpROM[curROM].Size, &tmpROM[curROM].Data);
if (retVal > 0x00)
{
printf("Unable to load Sample ROM: %s\n", fileNames[curROM]);
resVal = 0x01; // error loading one or more ROMs
break;
}
}
if (resVal && curROM == 0)
{
free(tmpROM);
return 0xFF; // load fail for first ROM
}
for (curROM = 1; curROM < romCount; curROM ++)
{
if (tmpROM[curROM].Size != tmpROM[0].Size)
{
printf("Warning: Sample ROMs have different sizes!\n");
break;
}
}
*retSize = 0x00;
for (curROM = 0; curROM < romCount; curROM ++)
*retSize += tmpROM[curROM].Size;
*retData = (UINT8*)malloc(*retSize);
baseOfs = 0x00;
for (curROM = 0; curROM < romCount; curROM ++)
{
memcpy(&(*retData)[baseOfs], tmpROM[curROM].Data, tmpROM[curROM].Size);
baseOfs += tmpROM[curROM].Size;
free(tmpROM[curROM].Data);
}
free(tmpROM);
return resVal;
}
static void RomByteswap(UINT32 Size, UINT8* Data)
{
UINT32 CurPos;
UINT8 TempByt;
for (CurPos = 0x00; CurPos < Size; CurPos += 0x02)
{
TempByt = Data[CurPos + 0x00];
Data[CurPos + 0x00] = Data[CurPos + 0x01];
Data[CurPos + 0x01] = TempByt;
}
return;
}
static UINT8 IsUpperCase(const char* text)
{
UINT8 flags;
unsigned char chr;
flags = 0x00;
while(*text != '\0')
{
chr = (unsigned char)*text;
if (isupper(chr))
flags |= 0x01;
if (islower(chr))
flags |= 0x02;
text ++;
}
// no letters -> false, lower/mixed case -> false, only uppercase letters -> true
return (flags == 0x01);
}
static void NormalizePath(char* filePath)
{
for (; *filePath != '\0'; filePath ++)
{
if (*filePath == '\\')
*filePath = '/';
}
return;
}
static void CreateDirTree(const char* dirPath)
{
char* tempPath;
char* dirSepPos;
char bakChr;
tempPath = strdup(dirPath);
NormalizePath(tempPath);
dirSepPos = strchr(tempPath, '/');
while(dirSepPos != NULL)
{
bakChr = *dirSepPos;
*dirSepPos = '\0';
MakeDir(tempPath);
*dirSepPos = bakChr;
dirSepPos = strchr(dirSepPos + 1, '/');
}
return;
}
INLINE UINT32 GetGlobalPtr(UINT32 PtrID)
{
return ReadBE24(&ROMData[GLOBAL_PTR_OFS + PtrID * 0x04]);
}
INLINE const UINT8* GetRomPtr(UINT32 Address)
{
switch((Address & 0xE00000) >> 16)
{
/*case 0x00:
case 0x20:
case 0x40:
return NULL;*/
case 0x60:
return ROMData;
case 0x80:
case 0xA0:
case 0xC0:
case 0xE0:
return &SmpROM.Data[Address & 0x600000];
default:
return NULL;
}
}
static void DecodeMidiData(UINT32 PtrBase, UINT8 SongID)
{
UINT32 SegBase;
UINT32 CurPos;
UINT32 SegPos;
UINT32 MidPos;
UINT32 MidTrkBase;
UINT32 TempLng;
UINT8 TempByt;
UINT8 CurSeg;
UINT8 LoopCnt;
char TempStr[0x10];
// Table Format:
// - Group Table
// - Group 1 Table
// - Group 2 Table
// etc.
//
// Pointer Table:
// 2 Bytes - Number of Pointer n
// n*2 Bytes - Pointer List (relative to Table Base)
//
// Sound Data:
// Ofs Len Desc
// 00 01 Sound Type (SFX/Music)
// 01 03 unused
// 04 04 (goes to 003004)
// 08 ?? Data
CurPos = PtrBase + ReadBE16(&ROMData[PtrBase + 2 + PTR_GROUP * 2]); // get Pointer Group Offset
SegBase = PtrBase + ReadBE16(&ROMData[CurPos + 2 + SongID * 2]); // get actual Song Pointer
MidPos = 0x00;
WriteBE32(&MidData[MidPos], 0x4D546864); MidPos += 0x04; // 'MThd' Signature
WriteBE32(&MidData[MidPos], 0x00000006); MidPos += 0x04; // Header Size
WriteBE16(&MidData[MidPos], 0x0000); MidPos += 0x02; // Format: 0
WriteBE16(&MidData[MidPos], 0x0001); MidPos += 0x02; // Tracks: 1
WriteBE16(&MidData[MidPos], 0x01E0); MidPos += 0x02; // Resolution
WriteBE32(&MidData[MidPos], 0x4D54726B); MidPos += 0x04; // 'MTrk' Signature
WriteBE32(&MidData[MidPos], 0x00000000); MidPos += 0x04; // Track Size
MidTrkBase = MidPos;
LoopCnt = 0;
SegPos = SegBase + 0x04;
MidData[MidPos] = 0x00; MidPos ++; // Delay
MidData[MidPos] = 0xFF; MidPos ++; // FF - Meta Event
MidData[MidPos] = 0x51; MidPos ++; // Meta Event 51 - Tempo
MidData[MidPos] = 0x03; MidPos ++; // Data Length
// TempLng = 0x0927C0; // 100 BPM (600 000 usec per Quarter)
TempLng = 0x0927C0 * 0x1E0 / 0x200;
MidData[MidPos] = (TempLng >> 16) & 0xFF; MidPos ++;
MidData[MidPos] = (TempLng >> 8) & 0xFF; MidPos ++;
MidData[MidPos] = (TempLng >> 0) & 0xFF; MidPos ++;
MidData[MidPos] = 0x00; MidPos ++; // Delay
memset(RunningDrmNotes, 0x00, 0x80);
for (CurSeg = 0x00; ; CurSeg ++, SegPos += 0x04)
{
sprintf(TempStr, "Segment %hu", CurSeg);
TempByt = (UINT8)strlen(TempStr);
MidData[MidPos] = 0xFF; MidPos ++; // FF - Meta Event
MidData[MidPos] = 0x06; MidPos ++; // Meta Event 06 - Marker
MidData[MidPos] = TempByt; MidPos ++; // Text Length
memcpy(&MidData[MidPos], TempStr, TempByt);
MidPos += TempByt;
MidData[MidPos] = 0x00; MidPos ++; // Delay
TempLng = ReadBE32(&ROMData[SegPos]);
if (ROMData[SegPos + 0x00] == 0x80)
{
if ((ROMData[SegPos + 0x01] & 0xE0) == 0xC0)
{
MidData[MidPos] = ROMData[SegPos + 0x01]; MidPos ++;
MidData[MidPos] = ROMData[SegPos + 0x02]; MidPos ++;
}
else
{
MidData[MidPos] = ROMData[SegPos + 0x01]; MidPos ++;
MidData[MidPos] = ROMData[SegPos + 0x02]; MidPos ++;
MidData[MidPos] = ROMData[SegPos + 0x03]; MidPos ++;
}
if ((ROMData[SegPos + 0x01] & 0xF0) == 0xA0)
MidPos += DoCommandA0(MidPos, ROMData[SegPos + 0x01],
ROMData[SegPos + 0x02], ROMData[SegPos + 0x03]);
MidData[MidPos] = 0x00; MidPos ++; // Delay
}
else if (TempLng == 0xFFFFFFFF)
{
// Song End
break;
}
else if (TempLng == 0xFFFFFFF1)
{
// Jump to other Segment
SegPos += 0x04;
TempLng = ReadBE32(&ROMData[SegPos]) & 0x7FFFF;
CurSeg = (TempLng - SegBase) / 4 - 1;
SegPos = TempLng - 0x04;
LoopCnt ++;
//sprintf(TempStr, "loop%s", LoopCnt ? "End" : "Start");
sprintf(TempStr, "Loop %hu", LoopCnt);
TempByt = (UINT8)strlen(TempStr);
MidData[MidPos] = 0xFF; MidPos ++; // FF - Meta Event
MidData[MidPos] = 0x06; MidPos ++; // Meta Event 06 - Marker
MidData[MidPos] = TempByt; MidPos ++; // Text Length
memcpy(&MidData[MidPos], TempStr, TempByt);
MidPos += TempByt;
MidData[MidPos] = 0x00; MidPos ++; // Delay
if (LoopCnt >= NUM_LOOPS)
break; // Terminate Song
}
else if (TempLng == 0xFFFFFFF2)
{
printf("Encountered Segment Call F2 at Offset 0x%06X!\n", SegPos);
break;
}
else
{
TempLng &= 0x7FFFF;
MidPos += DecodeMidiSegment(TempLng, MidPos);
}
}
MidData[MidPos] = 0xFF; MidPos ++; // FF - Meta Event
MidData[MidPos] = 0x2F; MidPos ++; // Meta Event 2F - Track End
MidData[MidPos] = 0x00; MidPos ++; // Length 00
// Fix Track Size
WriteBE32(&MidData[MidTrkBase - 0x04], MidPos - MidTrkBase);
MidSize = MidPos;
return;
}
static UINT32 DecodeMidiSegment(UINT32 ROMStPos, UINT32 MidStPos)
{
UINT32 CurPos;
UINT32 MidPos;
UINT8 LastCmd;
UINT8 TempByt;
UINT8 TrkEnd;
UINT8 NoDelay;
UINT8 Param1;
UINT8 IsDrum;
CurPos = ROMStPos;
MidPos = MidStPos;
LastCmd = 0x00;
TrkEnd = 0x00;
NoDelay = 0x02;
while(! TrkEnd)
{
if (! NoDelay)
{
TempByt = ROMData[CurPos];
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
if (TempByt & 0x80)
{
MidData[MidPos] = ROMData[CurPos];
CurPos ++; MidPos ++;
}
}
else if (NoDelay == 0x01)
{
MidData[MidPos] = 0x00;
MidPos ++;
}
NoDelay = 0x00;
TempByt = ROMData[CurPos];
if (TempByt & 0x80)
{
if (TempByt < 0xF0 && (TempByt & 0x0F) == 0x0F)
IsDrum = 0x01;
else
IsDrum = 0x00;
if (TempByt < 0xF0 && PatchDrumChn)
{
if ((TempByt & 0x0F) == 0x09)
TempByt = (TempByt & 0xF0) | 0x0F;
else if ((TempByt & 0x0F) == 0x0F)
TempByt = (TempByt & 0xF0) | 0x09;
}
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
LastCmd = TempByt;
}
else if (LastCmd >= 0xF0)
{
MidData[MidPos] = LastCmd;
MidPos ++;
}
switch(LastCmd & 0xF0)
{
case 0xA0: // loc_603E28
case 0xB0: // loc_603EB0 (MidEvt_CtrlChg)
case 0xE0: // loc_603F0C (MidEvt_NoteOn)
default: // loc_603F0C (MidEvt_NoteOn)
Param1 = ROMData[CurPos];
MidData[MidPos] = Param1;
CurPos ++; MidPos ++;
TempByt = ROMData[CurPos];
if (TempByt & 0x80)
{
NoDelay = 0x01;
TempByt &= 0x7F;
}
// additional fixes
if ((LastCmd & 0xF0) == 0xB0)
{
if (Param1 == 0x07 && FixVolume)
TempByt = TrkVol2MidiVol(TempByt);
}
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
if ((LastCmd & 0xF0) == 0xA0)
MidPos += DoCommandA0(MidPos, LastCmd, ROMData[CurPos - 0x02], ROMData[CurPos - 0x01]);
break;
case 0x80: // loc_603EDE (MidEvt_NoteOff)
TempByt = ROMData[CurPos];
if (TempByt & 0x80)
{
NoDelay = 0x01;
TempByt &= 0x7F;
}
MidData[MidPos] = TempByt;
if (IsDrum)
RunningDrmNotes[TempByt] = 0x00;
CurPos ++; MidPos ++;
MidData[MidPos] = 0x7F;
MidPos ++;
break;
case 0x90: // loc_603F0C (MidEvt_NoteOn)
if (IsDrum)
{
TempByt = ROMData[CurPos];
if (FixDrumNotes == 0x01 && RunningDrmNotes[TempByt])
{
// Drum notes are never turned off
MidData[MidPos] = ROMData[CurPos]; MidPos ++; // Note
MidData[MidPos] = 0x00; MidPos ++; // Velocity: 00 (Note Off)
MidData[MidPos] = 0x00; MidPos ++; // Delay
}
RunningDrmNotes[TempByt] = 0x01;
}
MidData[MidPos] = ROMData[CurPos];
CurPos ++; MidPos ++;
TempByt = ROMData[CurPos];
if (TempByt & 0x80)
{
NoDelay = 0x01;
TempByt &= 0x7F;
}
if (FixVolume)
TempByt = NoteVel2MidiVol(TempByt);
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
if (IsDrum && FixDrumNotes == 0x02)
{
// Drum notes are never turned off
MidData[MidPos] = 0x00; MidPos ++; // Delay
MidData[MidPos] = ROMData[CurPos - 0x02]; MidPos ++; // Note
MidData[MidPos] = 0x00; MidPos ++; // Velocity: 00 (Note Off)
}
break;
case 0xC0: // loc_603F38
case 0xD0:
TempByt = ROMData[CurPos];
if (TempByt & 0x80)
{
NoDelay = 0x01;
TempByt &= 0x7F;
}
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
break;
case 0xF0:
switch(LastCmd)
{
case 0xF7: // loc_603F82
TempByt = ROMData[CurPos];
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
while(TempByt)
{
MidData[MidPos] = ROMData[CurPos];
CurPos ++; MidPos ++;
TempByt --;
}
break;
case 0xF0: // loc_603F8C
while(ROMData[CurPos] != 0xF7)
{
MidData[MidPos] = ROMData[CurPos];
CurPos ++; MidPos ++;
}
break;
case 0xFF: // loc_603F96
Param1 = ROMData[CurPos]; // Meta Event Type
if (Param1 == 0x2F) // Type 2F - Track End
{
// loc_603FE6
MidPos --; // undo last byte
TrkEnd = 0x01;
break;
}
MidData[MidPos] = Param1;
CurPos ++; MidPos ++;
TempByt = ROMData[CurPos]; // Meta Event Length
MidData[MidPos] = TempByt;
CurPos ++; MidPos ++;
while(TempByt)
{
MidData[MidPos] = ROMData[CurPos];
CurPos ++; MidPos ++;
TempByt --;
}
break;
default: // loc_603FE6
TrkEnd = 0x01;
break;
}
break;
}
}
if (FixDrumNotes == 0x01)
{
LastCmd = PatchDrumChn ? 0x99 : 0x9F;
for (TempByt = 0x00; TempByt < 0x80; TempByt ++)
{
if (RunningDrmNotes[TempByt])
{
MidData[MidPos] = LastCmd; MidPos ++; // Command
MidData[MidPos] = TempByt; MidPos ++; // Note
MidData[MidPos] = 0x00; MidPos ++; // Velocity: 00 (Note Off)
MidData[MidPos] = 0x00; MidPos ++; // Delay
RunningDrmNotes[TempByt] = 0x00;
}
}
}
return MidPos - MidStPos;
}
static UINT32 DoCommandA0(UINT32 MidStPos, UINT8 Command, UINT8 Arg1, UINT8 Arg2)
{
UINT32 MidPos;
UINT32 BasePtr;
UINT32 CurPos;
UINT8 TrkCnt;
UINT8 CurTrk;
UINT8 MidChn;
UINT8 TempByt;
MidPos = MidStPos;
// Used Commands: 00, 01, 03..0A
// Command 10 plays music or sounds.
switch(Arg1)
{
case 0x00:
// Used Sub-Commands: 01..04, 09..0C, 0E
switch(Arg2)
{
case 0x02: // Reset All Channels
// I *could* send something, but this is placed only at the
// beginning of the song anyway.
return 0;
default:
return 0;
}
break;
case 0x05: // Load Instruments (Pointer is at 0x008000)
return 0;
case 0x07: // Init All Track Instruments
BasePtr = GetGlobalPtr(TRK_INIT_ID) & 0x7FFFF;
if (Arg2 > ReadBE16(&ROMData[BasePtr])) // the value is the last valid ID
return 0;
CurPos = BasePtr + ReadBE16(&ROMData[BasePtr + 0x02 + Arg2 * 0x02]);
// TODO: Pitch Bend Range
// Dead Or Alive, song 00, channel 1 needs PB range 12
// Base+0x00
// TODO: Enqueue Command A0 01 ROMData[CurPos]
CurPos ++;
// Base+0x01
// if (ROMData[CurPos] < 0x80)
// ; // call Sound Command A0 08
CurPos ++;
// Base+0x02
// if (ROMData[CurPos] < 0x80)
// ; // call Sound Command A0 06
CurPos ++;
// Base+0x03
// if (ROMData[CurPos] < 0x80)
// ; // call Sound Command A0 05
CurPos ++;
// Base+0x04 - ignored
CurPos ++;
// Base+0x05 - Track Count (for the 68000 dbf instruction)
TrkCnt = ROMData[CurPos] + 1;
CurPos ++;
for (CurTrk = 0x00; CurTrk < TrkCnt; CurTrk ++, CurPos += 0x10)
{
// Format:
// 00 - Track Flags?
// 01 - MIDI Channel
// 02 - Instrument Number
// 03 - Volume -> (MidiVol * 2) + 1
// 04 - Transpose -> (Ctrl_11 & 0x7F) - 0x40
// 05 - ?? -> changed by Ctrl_12, read from PanTable
// 06 - SCSP Direct Data Register
// -> bits 0-5 (0x1F) are Pan (Ctrl_0A)
// -> bits 6-7 (0xE0) are changed by Ctrl_19 (Instrument Volume)
// 07 - Unknown -> Ctrl_13
// 08/09 - SCSP LFO Bits
// -> bits 0- 2 (0x0007) are changed by Ctrl_17 (Amp. Mod. Level)
// -> bits 3- 4 (0x0018) are changed by Ctrl_15 (Amp. Mod. Wave Select)
// -> bits 5- 7 (0x00E0) are changed by Ctrl_16 (Freq. Mod. Level)
// -> bits 8- 9 (0x0300) are changed by Ctrl_14 (Freq. Mod. Wave Select)
// -> bits 10-14 (0x7C00) are changed by Ctrl_18 (LFO Frequency)
// Example Data:
// 0001 0203 0405 0607 0809 0A0B 0C0D 0E0F