-
Notifications
You must be signed in to change notification settings - Fork 14
/
seq2mid.c
1603 lines (1424 loc) · 37 KB
/
seq2mid.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
// PlayStation SEQ2MID Converter
// -----------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "stdtype.h"
#include "stdbool.h"
#define OPTIMIZE_NRPN
#define INS_MAX_INSTRUMENTS 0x80
#define INS_DATA_BLK_LINES 0x10
#define INS_LINE_CNT (INS_MAX_INSTRUMENTS * INS_DATA_BLK_LINES)
typedef struct instrument_header
{
UINT32 fccSignature;
UINT32 dwUnknown04;
UINT32 dwUnknown08;
UINT32 dwInsSize; // Instrument Definition + Data/Samples
UINT16 wUnknown10;
UINT16 wInsCount; // Number of Instument Data Blocks
UINT16 wInsLineUsed;
UINT16 wInsIDCount;
UINT8 bMasterVol;
UINT8 bMasterPan;
UINT8 bUnknown1A;
UINT8 bUnknown1B;
UINT32 dwPadding;
// -> 20 Bytes
} INS_HEADER;
typedef struct instrument_description
{
UINT8 bDataCount; // Number of Lines
UINT8 bInsVolume;
UINT16 wUnknown02;
UINT8 bInsPanorama;
UINT8 bUnknown05;
UINT8 bUnknown06;
UINT8 bUnknown07;
UINT32 dwPadding08;
UINT32 dwPadding0C;
// -> 10 Bytes
} INS_DESCRIPTION;
typedef struct instrument_data_block
{
UINT8 bUnknown00;
UINT8 bUnknown01;
UINT8 bToneVol;
UINT8 bTonePan;
UINT8 bUnknown04;
UINT8 bUnknown05;
UINT8 bNoteLow;
UINT8 bNoteHigh;
UINT32 dwUnknown08;
UINT8 bPbDepthMSB;
UINT8 bPbDepthLSB;
UINT16 wUnknown0E;
UINT16 wUnknown10;
UINT16 wUnknown12;
UINT16 wInsNumber;
UINT16 wInsID;
UINT16 wUnknown18;
UINT16 wUnknown1A;
UINT16 wUnknown1C;
UINT16 wUnknown1E;
// -> 20 Bytes
} INS_DATA_BLOCK;
typedef struct ins_data
{
INS_DATA_BLOCK DataBlk[INS_DATA_BLK_LINES];
} INS_DATA;
typedef struct instrument_data
{
INS_HEADER Head;
INS_DESCRIPTION Desc[INS_MAX_INSTRUMENTS];
INS_DATA Data[INS_MAX_INSTRUMENTS];
} INSTRUMENT_DATA;
typedef struct instrument_setup
{
UINT8 MidiIns;
INT8 NoteMove;
UINT8 DrumBase;
} INS_SETUP;
typedef struct channel_data_seq
{
UINT8 Ins;
INT8 Move;
UINT8 DrmNote;
UINT8 NewChn;
} CHN_DATA_SEQ;
typedef struct channel_data_midi
{
UINT16 Pitch;
UINT8 PBRange;
UINT8 RPN_MSB;
UINT8 RPN_LSB;
} CHN_DATA_MID;
typedef struct channel_data_drums
{
UINT8 Pitch;
UINT8 Volume;
UINT8 Pan;
} CHN_DATA_DRM;
// FourCharCode Constants
#define FCC_VABp 0x56414270
#define FCC_SEQp 0x53455170
int main(int argc, char* argv[]);
static void StripFileTitle(char* FilePath);
static void StripFilePath(char* FilePath);
static void StripFileExtention(char* FilePath);
void ReadConvData(const char* FileName);
void LoadInsData(const char* FileName, INSTRUMENT_DATA* RetIns);
INS_SETUP* GetInsSetupData(UINT8 Ins, UINT8 Note, const INSTRUMENT_DATA* InsData);
UINT32 Seq2MidConversion(UINT32* PosStart, UINT8** RetData, INSTRUMENT_DATA* InsData);
UINT32 Seq2InsConversion(UINT32 PosStart, UINT8** RetData, INSTRUMENT_DATA* RetIns);
void GetInsData(UINT32 InsLen, const UINT8* InsData, INSTRUMENT_DATA* RetIns);
void WriteBE32(UINT8* Data, UINT32 Value);
void WriteBE16(UINT8* Data, UINT16 Value);
UINT16 ReadBE16(const UINT8* Data);
void WriteLE32(UINT8* Data, UINT32 Value);
//void ReverseBytes(UINT32 ByteCount, void* DstData, void* SrcData);
static bool IsNoteOn(UINT8 Event, UINT8 Volume);
static UINT8 MidiVolConv(UINT8 Volume);
void MakeInstrumentTestMIDI(const INSTRUMENT_DATA* InsData, const char* OutFileName);
UINT8 FileNameType;
bool FixMidi;
bool InsertPB;
bool FixInsSet;
bool FixVolume;
UINT32 SeqLen;
UINT8* SeqFile;
char InsFileName[0x100];
INS_SETUP InsSetup[INS_LINE_CNT];
int main(int argc, char* argv[])
{
char FilePath[_MAX_PATH];
char FileName[_MAX_PATH];
char OutFileName[_MAX_PATH];
char* StrPtr;
UINT32 MidAllCnt;
UINT32 MidMnCnt;
UINT32 MidSubCnt;
UINT32 CurPos;
UINT32 MidLen;
UINT8* MidFile;
UINT32 TempLng;
char TempFile[0x10];
INSTRUMENT_DATA InsData;
FILE* hFile;
//char* FileList_Name;
if (argc <= 2)
{
printf("Usage: seq2mid Options File.seq [InsMap.ini]\n");
printf("Options: (options can be combined)\n");
printf(" r Raw conversion (other options are ignored)\n");
printf(" p insert Pitch Bend resets\n");
printf(" i fix Instruments (needs InsMap.ini)\n");
printf(" v fix Volume (convert linear PSX to logarithmic MIDI)\n");
return 9;
}
FixMidi = true;
InsertPB = false;
FixInsSet = false;
FixVolume = false;
StrPtr = argv[1];
while(*StrPtr != '\0')
{
switch(toupper(*StrPtr))
{
case 'R':
FixMidi = false;
break;
case 'P':
InsertPB = true;
break;
case 'I':
FixInsSet = true;
break;
case 'V':
FixVolume = true;
break;
}
StrPtr ++;
}
InsertPB &= FixMidi;
FixInsSet &= FixMidi;
FileNameType = 0x00;
//FileNameType = 0x02;
//#define BASE_PATH "D:\\VStudio-Programme\\VBasic\\PSFCnv\\ToP/"
//#define INS_SET "ToP"
// Works also with uncompressed PSFs
//FileList_Name = "ToD\\ToD_lib.EXE.bin";
//FileList_Name = "ToE\\InsSetA\\toe 026 mt. farlos.seq";
//FileList_Name = "ToE\\InsSetB\\toe 099 mid boss 2.seq";
// FileList_Name = "top 101 decisive.seq";
if (FixInsSet)
{
// sprintf(FileName, "%s%s", BASE_PATH, INS_SET ".ini");
// ReadConvData(FileName);
if (argc <= 3)
{
printf("Insufficient arguments!\n");
return 9;
}
ReadConvData(argv[3]);
LoadInsData(InsFileName, &InsData);
//sprintf(FileName, "%s%s", BASE_PATH, "ToP\\" INS_SET "_test.seq");
//MakeInstrumentTestMIDI(&InsData ,FileName);
}
//sprintf(FileName, "%s%s", BASE_PATH, FileList_Name);
strcpy(FileName, argv[2]);
printf("Loading ...");
hFile = fopen(FileName, "rb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return 1;
}
fseek(hFile, 0, SEEK_END);
SeqLen = ftell(hFile);
SeqFile = (UINT8*)malloc(SeqLen);
fseek(hFile, 0, SEEK_SET);
fread(SeqFile, 0x01, SeqLen, hFile);
fclose(hFile);
printf(" OK\n");
strcpy(FilePath, FileName);
StripFileTitle(FilePath);
StripFilePath(FileName);
StripFileExtention(FileName);
printf("Converting ...\n");
MidAllCnt = 0;
MidMnCnt = -1;
MidSubCnt = 0;
CurPos = 0x00;
while(true)
{
do
{
if (SeqFile[CurPos] != 'p')
{
CurPos ++;
continue;
}
//memcpy(&TempLng, &SeqFile[CurPos], 0x04);
TempLng = *((UINT32*)(SeqFile + CurPos));
if (TempLng == FCC_VABp)
{
memcpy(&TempLng, &SeqFile[CurPos + 0x08], 0x04);
if (! TempLng)
{
MidLen = Seq2InsConversion(CurPos, &MidFile, &InsData);
MidMnCnt ++;
MidSubCnt = 0;
switch(FileNameType)
{
case 0x00:
strcpy(TempFile, "");
break;
case 0x01:
sprintf(TempFile, "_%02", MidAllCnt);
break;
case 0x02:
sprintf(TempFile, "_%02", MidMnCnt);
break;
}
sprintf(OutFileName, "%s%s%s.ins", FilePath, FileName, TempFile);
hFile = fopen(OutFileName, "wb");
fwrite(MidFile, 0x01, MidLen, hFile);
fclose(hFile);
CurPos += TempLng;
}
}
else if (TempLng == FCC_SEQp)
{
memcpy(&TempLng, &SeqFile[CurPos + 0x04], 0x04);
if (TempLng == 0x01000000 && SeqFile[CurPos + 0x07] == 0x01)
break;
else
CurPos += 0x04;
}
else
{
CurPos ++;
}
} while(CurPos < SeqLen);
if (CurPos >= SeqLen)
break;
if (MidMnCnt == -1 && FileNameType == 0x01)
FileNameType = 0x00;
switch(FileNameType)
{
case 0x00:
strcpy(TempFile, "");
break;
case 0x01:
sprintf(TempFile, "_%02u", MidAllCnt);
break;
case 0x02:
sprintf(TempFile, "_%02u-%02u", MidMnCnt, MidSubCnt);
break;
}
printf("File %u: %s%s\n", MidAllCnt + 1, FileName, TempFile);
sprintf(OutFileName, "%s%s%s.mid", FilePath, FileName, TempFile);
MidLen = Seq2MidConversion(&CurPos, &MidFile, &InsData);
hFile = fopen(OutFileName, "wb");
if (hFile != NULL)
{
fwrite(MidFile, 0x01, MidLen, hFile);
fclose(hFile);
}
else
{
printf("Error opening file!\n");
}
MidAllCnt ++;
MidSubCnt ++;
}
printf("%u Files saved.\n", MidAllCnt);
#ifdef _DEBUG
//_getch();
#endif
return 0;
}
static void StripFileTitle(char* FilePath)
{
char* ChrPos1;
char* ChrPos2;
ChrPos1 = strrchr(FilePath, '\\');
ChrPos2 = strrchr(FilePath, '/');
if (ChrPos1 < ChrPos2)
ChrPos1 = ChrPos2;
if (ChrPos1 == NULL)
{
*FilePath = 0x00;
}
else
{
ChrPos1 ++;
*ChrPos1 = '\0';
}
return;
}
static void StripFilePath(char* FilePath)
{
char* ChrPos1;
char* ChrPos2;
ChrPos1 = strrchr(FilePath, '\\');
ChrPos2 = strrchr(FilePath, '/');
if (ChrPos1 < ChrPos2)
ChrPos1 = ChrPos2;
if (ChrPos1 != NULL)
{
ChrPos1 ++;
strcpy(FilePath, ChrPos1);
}
return;
}
static void StripFileExtention(char* FilePath)
{
char* PntPos;
char* ChrPos1;
char* ChrPos2;
PntPos = strrchr(FilePath, '.');
if (PntPos != NULL)
{
ChrPos1 = strrchr(PntPos, '\\');
ChrPos2 = strrchr(PntPos, '/');
if (ChrPos1 < ChrPos2)
ChrPos1 = ChrPos2;
if (ChrPos1 != NULL)
PntPos = NULL;
}
if (PntPos != NULL)
*PntPos = '\0';
return;
}
void ReadConvData(const char* FileName)
{
FILE* hFile;
char TempStr[0x100];
char* TempPnt;
char* EndPnt;
UINT8 InsNum;
UINT8 InsLine;
INS_SETUP* TempIns;
UINT8 FirstLine;
hFile = fopen(FileName, "rt");
if (hFile == NULL)
{
printf("Error opening %s\n", FileName);
return;
}
TempIns = InsSetup;
for (InsNum = 0x00; InsNum < INS_MAX_INSTRUMENTS; InsNum ++)
{
for (InsLine = 0x00; InsLine < INS_DATA_BLK_LINES; InsLine ++, TempIns ++)
{
TempIns->MidiIns = 0xFF/*InsNum*/;
TempIns->NoteMove = 0x00;
TempIns->DrumBase = 0xFF;
}
}
strcpy(InsFileName, FileName);
StripFileTitle(InsFileName);
FirstLine = 0x01;
while(! feof(hFile))
{
TempPnt = fgets(TempStr, 0x100, hFile);
if (TempPnt == NULL)
break;
if (TempStr[0x00] == '\n' || TempStr[0x00] == '\0')
continue;
if (TempStr[0x00] == ';')
{
// skip comment lines
// fgets has set a null-terminator at char 0xFF
while(TempStr[strlen(TempStr) - 1] != '\n')
{
fgets(TempStr, 0x100, hFile);
if (TempStr[0x00] == '\0')
break;
}
continue;
}
if (FirstLine)
{
FirstLine = 0x00;
TempPnt = strchr(TempStr, '\n');
if (TempPnt != NULL)
*TempPnt = '\0';
strcat(InsFileName, TempStr);
continue;
}
TempPnt = strchr(TempStr, '\t');
if (TempPnt == NULL || TempPnt == TempStr)
continue; // invalid line
InsNum = (UINT8)strtoul(TempStr, &EndPnt, 0x10);
if (EndPnt == TempStr || InsNum >= INS_MAX_INSTRUMENTS)
continue;
if (EndPnt != NULL && *EndPnt == '-')
InsLine = (UINT8)strtoul(EndPnt + 1, NULL, 0x10);
else
InsLine = 0x00;
InsLine &= 0x0F;
TempIns = &InsSetup[(InsNum << 4) | InsLine];
TempPnt ++;
if (*TempPnt != 'D')
{
TempIns->MidiIns = (UINT8)strtoul(TempPnt, NULL, 0x10);
TempPnt = strchr(TempPnt, '\t');
if (TempPnt != NULL)
{
TempPnt ++;
TempIns->NoteMove = (UINT8)strtoul(TempPnt, NULL, 10);
}
else
{
TempIns->NoteMove = 0x00;
}
TempIns->DrumBase = 0xFF;
}
else
{
TempPnt ++;
TempIns->MidiIns = 0x80;
TempIns->NoteMove = (UINT8)strtoul(TempPnt, NULL, 0x10);
TempPnt = strchr(TempPnt, '\t');
if (TempPnt != NULL)
{
TempPnt ++;
TempIns->DrumBase = (UINT8)strtoul(TempPnt, NULL, 0x10);
}
else
{
TempIns->DrumBase = 0x00;
}
if (! TempIns->DrumBase)
TempIns->DrumBase = TempIns->NoteMove;
}
}
fclose(hFile);
return;
}
void LoadInsData(const char* FileName, INSTRUMENT_DATA* RetIns)
{
FILE* hFile;
UINT32 InsLen;
UINT8* InsData;
hFile = fopen(FileName, "rb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return;
}
fseek(hFile, 0, SEEK_END);
InsLen = ftell(hFile);
InsData = (UINT8*)malloc(InsLen);
fseek(hFile, 0, SEEK_SET);
fread(InsData, 0x01, InsLen, hFile);
fclose(hFile);
GetInsData(InsLen, InsData, RetIns);
free(InsData);
return;
}
INS_SETUP* GetInsSetupData(UINT8 Ins, UINT8 Note, const INSTRUMENT_DATA* InsData)
{
const INS_DESCRIPTION* TempDesc;
const INS_DATA_BLOCK* TempBlk;
INS_SETUP* TempIS;
INS_SETUP* ISBase;
UINT8 CurLin;
TempDesc = &InsData->Desc[Ins];
if (TempDesc->bDataCount && Ins < INS_MAX_INSTRUMENTS)
{
TempBlk = InsData->Data[Ins].DataBlk;
TempIS = &InsSetup[Ins << 4];
ISBase = NULL;
for (CurLin = 0x00; CurLin < TempDesc->bDataCount; CurLin ++, TempIS ++, TempBlk ++)
{
if (ISBase == NULL && TempIS->MidiIns != 0xFF)
ISBase = TempIS;
if (Note >= TempBlk->bNoteLow && Note <= TempBlk->bNoteHigh)
{
if (TempIS->MidiIns != 0xFF)
return TempIS;
else
break;
}
}
if (ISBase == NULL)
ISBase = &InsSetup[Ins << 4];
if (ISBase->MidiIns == 0xFF)
{
printf("Warning: Unmapped instrument %02X!\n", Ins);
ISBase->MidiIns = Ins;
}
return ISBase;
}
else
{
printf("Illegal Instrument: %02X\n", Ins);
return &InsSetup[0x00];
}
}
void WriteRPN(UINT8* MidFile, UINT32* MidPos, UINT8 RpnMSB, UINT8 RpnLSB, UINT8 RpnData,
CHN_DATA_MID* MidChnData)
{
UINT8 RPNMsk;
UINT8 RPNCtrl;
RPNMsk = RpnMSB & 0x80;
if (! RPNMsk)
RPNCtrl = 0x64; // RPN
else
RPNCtrl = 0x62; // NRPN
RpnMSB &= 0x7F;
#ifdef OPTIMIZE_NRPN
if (MidChnData->RPN_MSB != (RPNMsk | RpnMSB))
{
MidFile[*MidPos + 0x00] = RPNCtrl | 0x01;
MidFile[*MidPos + 0x01] = RpnMSB;
MidFile[*MidPos + 0x02] = 0x00;
*MidPos += 0x03;
MidChnData->RPN_MSB = RPNMsk | RpnMSB;
}
if (MidChnData->RPN_LSB != (RPNMsk | RpnLSB))
{
MidFile[*MidPos + 0x00] = RPNCtrl | 0x00;
MidFile[*MidPos + 0x01] = RpnLSB;
MidFile[*MidPos + 0x02] = 0x00;
*MidPos += 0x03;
MidChnData->RPN_LSB = RPNMsk | RpnLSB;
}
#else
if (MidChnData->RPN_MSB != (RPNMsk | RpnMSB) ||
MidChnData->RPN_LSB != (RPNMsk | RpnLSB))
{
MidFile[*MidPos + 0x00] = RPNCtrl | 0x01;
MidFile[*MidPos + 0x01] = RpnMSB;
MidFile[*MidPos + 0x02] = 0x00;
MidFile[*MidPos + 0x03] = RPNCtrl | 0x00;
MidFile[*MidPos + 0x04] = RpnLSB;
MidFile[*MidPos + 0x05] = 0x00;
*MidPos += 0x06;
MidChnData->RPN_MSB = RPNMsk | RpnMSB;
MidChnData->RPN_LSB = RPNMsk | RpnLSB;
}
#endif
if (! (RpnData & 0x80))
{
MidFile[*MidPos + 0x00] = 0x06;
MidFile[*MidPos + 0x01] = RpnData;
MidFile[*MidPos + 0x02] = 0x00;
*MidPos += 0x03;
}
return;
}
UINT32 Seq2MidConversion(UINT32* PosStart, UINT8** RetData, INSTRUMENT_DATA* InsData)
{
UINT32 MidLen;
UINT8* MidFile;
UINT32 CurPos;
UINT32 MidPos;
UINT32 TempLng;
INT16 TempSht;
UINT8 TempByt;
//UINT8* TempArr;
UINT8 LastEvt;
bool ShortEvt;
bool WriteFullEvt;
bool MidiEnd;
UINT16 MidRes;
bool IntroOn;
UINT32 IntroDelay;
UINT32 Delay;
CHN_DATA_SEQ ChnSeq[0x10];
CHN_DATA_MID ChnMid[0x10];
CHN_DATA_DRM ChnDrm[0x80];
UINT8 ChnDrmPtchBak[0x80];
UINT8 CurChn;
UINT8 MidChn;
INS_DESCRIPTION* TempDesc;
INS_DATA_BLOCK* TempBlk;
INS_SETUP* TempIS;
CHN_DATA_SEQ* TempChS;
CHN_DATA_MID* TempChM;
CHN_DATA_DRM* TempChD;
UINT8 CurDrmPtch;
MidLen = 0x40000;
MidFile = (UINT8*)malloc(MidLen);
CurPos = *PosStart;
MidiEnd = false;
CurPos += 0x04;
WriteBE32( &MidFile[0x00], 0x4D546864); // MThd
WriteBE32( &MidFile[0x04], 0x06); // Header Length
memcpy( &MidFile[0x08], &SeqFile[CurPos + 0x00], 0x02); // Format
memcpy( &MidFile[0x0A], &SeqFile[CurPos + 0x02], 0x02); // Tracks
memcpy( &MidFile[0x0C], &SeqFile[CurPos + 0x04], 0x02); // Tick/Quarter
//If SeqFile[CurPos + 0x03) <> 0x01 Then Stop
WriteBE32( &MidFile[0x0E], 0x4D54726B); //MTrk
WriteBE32( &MidFile[0x12], 0xFFFFFFFF);
MidRes = ReadBE16(&SeqFile[CurPos + 0x04]);
MidPos = 0x16;
CurPos += 0x06;
for (CurChn = 0x00; CurChn < 0x10; CurChn ++)
{
TempChS = &ChnSeq[CurChn];
TempChM = &ChnMid[CurChn];
TempChS->Ins = 0xFF;
TempChS->Move = 0;
TempChS->DrmNote = 0xFF;
TempChS->NewChn = 0xFF;
TempChM->Pitch = 0x4000;
TempChM->PBRange = 0x00;
TempChM->RPN_MSB = 0x7F; // RPN Null
TempChM->RPN_LSB = 0x7F;
}
for (CurChn = 0x00; CurChn < 0x80; CurChn ++)
{
TempChD = &ChnDrm[CurChn];
TempChD->Pitch = 0x40;
TempChD->Volume = 0x7F;
TempChD->Pan = 0x40;
ChnDrmPtchBak[CurChn] = 0xFF;
}
CurDrmPtch = 0xFF;
// Tempo
MidFile[MidPos + 0x00] = 0x00;
MidFile[MidPos + 0x01] = 0xFF;
MidFile[MidPos + 0x02] = 0x51;
MidFile[MidPos + 0x03] = 0x03;
memcpy(&MidFile[MidPos + 0x04], &SeqFile[CurPos], 0x03);
MidPos += 0x07;
CurPos += 0x03;
if (FixMidi)
{
// Time for Initialiation
IntroDelay = (4 * MidRes >> SeqFile[CurPos + 0x01]) * SeqFile[CurPos + 0x00];
IntroOn = true;
}
else
{
IntroOn = false;
}
MidFile[MidPos + 0x00] = 0x00;
MidFile[MidPos + 0x01] = 0xFF;
MidFile[MidPos + 0x02] = 0x58;
MidFile[MidPos + 0x03] = 0x04;
MidFile[MidPos + 0x04] = SeqFile[CurPos + 0x00];
MidFile[MidPos + 0x05] = SeqFile[CurPos + 0x01];
MidFile[MidPos + 0x06] = 0x06 << SeqFile[CurPos + 0x01];
MidFile[MidPos + 0x07] = 0x08;
MidPos += 0x08;
CurPos += 0x02;
WriteFullEvt = false;
do
{
if (MidPos + 0x020 >= MidLen)
{
MidLen += 0x40000;
MidFile = (UINT8*)realloc(MidFile, MidLen);
}
if (IntroOn)
{
Delay = 0x00;
while(SeqFile[CurPos] & 0x80)
{
Delay <<= 7;
Delay |= SeqFile[CurPos] & 0x7F;
CurPos ++;
}
Delay <<= 7;
Delay |= SeqFile[CurPos] & 0x7F;
CurPos ++;
// Finish Initialisation Measure
if (Delay >= IntroDelay)
{
TempLng = IntroDelay;
TempByt = 0;
while(TempLng >= 0x80)
{
TempLng >>= 7;
TempByt += 7;
}
while(TempByt)
{
MidFile[MidPos] = 0x80 | ((IntroDelay >> TempByt) & 0x7F);
TempByt -= 7;
MidPos ++;
}
MidFile[MidPos] = 0x00 | (IntroDelay & 0x7F);
MidPos ++;
// Measure: TempByt / 2 ^ TempSht
TempByt = 0x04;
TempSht = 0x02;
MidFile[MidPos + 0x00] = 0xFF;
MidFile[MidPos + 0x01] = 0x58;
MidFile[MidPos + 0x02] = 0x04;
MidFile[MidPos + 0x03] = TempByt;
MidFile[MidPos + 0x04] = (UINT8)TempSht;
MidFile[MidPos + 0x05] = 0x06 << TempSht;
MidFile[MidPos + 0x06] = 0x08;
MidPos += 0x07;
WriteFullEvt = true;
Delay -= IntroDelay;
IntroDelay = 0;
IntroOn = false;
}
else
{
IntroDelay -= Delay;
}
TempLng = Delay;
TempByt = 0;
while(TempLng >= 0x80)
{
TempLng >>= 7;
TempByt += 7;
}
while(TempByt)
{
MidFile[MidPos] = 0x80 | ((Delay >> TempByt) & 0x7F);
TempByt -= 7;
MidPos ++;
}
MidFile[MidPos] = 0x00 | (Delay & 0x7F);
MidPos ++;
}
else
{
while(SeqFile[CurPos] & 0x80)
{
MidFile[MidPos] = SeqFile[CurPos];
CurPos ++;
MidPos ++;
}
MidFile[MidPos] = SeqFile[CurPos];
CurPos ++;
MidPos ++;
}
ShortEvt = ! (SeqFile[CurPos] & 0x80);
if (! ShortEvt)
{
LastEvt = SeqFile[CurPos];
CurPos ++;
}
if (WriteFullEvt)
{
ShortEvt = false;
WriteFullEvt = false;
}
CurChn = LastEvt & 0x0F;
if (ChnSeq[CurChn].NewChn != 0xFF)
MidChn = ChnSeq[CurChn].NewChn;
else
MidChn = CurChn;
switch(LastEvt & 0xF0)
{
case 0xC0:
if (FixInsSet && InsData->Head.wInsCount)
{
TempChS = &ChnSeq[CurChn];
TempChS->Ins = SeqFile[CurPos + 0x00];
TempDesc = &InsData->Desc[TempChS->Ins];
TempIS = GetInsSetupData(TempChS->Ins, 0xFF, InsData);
SeqFile[CurPos + 0x00] = TempIS->MidiIns & 0x7F;
if ((TempIS->MidiIns & 0x80) && TempIS->DrumBase != 0xFF)
{
TempChS->DrmNote = (UINT8)TempIS->NoteMove;
TempChS->NewChn = 0x09;
if (CurChn != 0x09 && ChnSeq[0x09].NewChn == 0xFF)
ChnSeq[0x09].NewChn = CurChn;
MidChn = 0x09;
if (CurDrmPtch == 0xFF)
{
MidFile[MidPos + 0x00] = 0xC0 | MidChn;
MidFile[MidPos + 0x01] = 0x00;
MidFile[MidPos + 0x02] = 0x00;
MidPos += 0x03;
MidFile[MidPos + 0x00] = 0xB0 | MidChn;
MidFile[MidPos + 0x01] = 0x07;
MidFile[MidPos + 0x02] = 0x7F;
MidFile[MidPos + 0x03] = 0x00;
MidPos += 0x04;
CurDrmPtch = 0x00;
}
TempChM = &ChnMid[MidChn];
TempIS = &InsSetup[TempChS->Ins << 4];
TempBlk = InsData->Data[TempChS->Ins].DataBlk;
ShortEvt = false;
for (TempByt = 0x00; TempByt < TempDesc->bDataCount; TempByt ++, TempBlk ++, TempIS ++)
{
if (TempIS->DrumBase == 0xFF)
continue;
TempChD = &ChnDrm[TempIS->NoteMove];
if (TempChD->Volume != TempBlk->bToneVol)
{
if (! ShortEvt)
{
MidFile[MidPos] = 0xB0 | MidChn;
MidPos ++;
ShortEvt = true;
}
// Write Drum Volume Level
WriteRPN(MidFile, &MidPos, 0x9A, (UINT8)TempIS->NoteMove,
TempBlk->bToneVol, TempChM);
TempChD->Volume = TempBlk->bToneVol;
}
if (TempChD->Pan != TempBlk->bTonePan)
{
if (! ShortEvt)
{
MidFile[MidPos] = 0xB0 | MidChn;
MidPos ++;
ShortEvt = true;
}
// Write Drum Panorama
WriteRPN(MidFile, &MidPos, 0x9C, (UINT8)TempIS->NoteMove,
TempBlk->bTonePan, TempChM);
TempChD->Pan = TempBlk->bTonePan;
}
}
ShortEvt = false;
MidChn = CurChn;
if (MidChn == 0x09)
{
for (MidChn = 0x00; MidChn < 0x0F; MidChn ++)
{
if (MidChn == 0x09)
continue;
if (ChnSeq[MidChn].NewChn == 0x09)
break;
}
// if no free channel found, use 0x0F
}
SeqFile[CurPos + 0x00] = 0x7F;
}
else
{
if ((TempIS->MidiIns & 0x80) && TempIS->DrumBase == 0xFF)
{
TempChS->Move = 0;
TempChS->DrmNote = (UINT8)TempIS->NoteMove;
TempChS->NewChn = 0x09;
MidChn = CurChn;
SeqFile[CurPos + 0x00] = 0x00;
}
else
{
if (CurChn == 0x09 && TempChS->NewChn == 0xFF)