forked from ValleyBell/MidiConverters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathit2mid.c
1396 lines (1239 loc) · 33.1 KB
/
it2mid.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
// IT -> MIDI Converter
// --------------------
// Valley Bell, 2015-05/2015-06
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "stdtype.h"
#define INLINE static __inline
#define USE_NOTE_VEL
//#define IGNORE_NOTE_OFF
#define IT_CHANNELS 0x40
typedef struct _it_header
{
char fileSig[4];
char songName[26];
UINT16 pHilight;
UINT16 ordCount;
UINT16 insCount;
UINT16 smpCount;
UINT16 patCount;
UINT16 verCreate;
UINT16 verCompat;
UINT16 flags;
UINT16 special;
UINT8 globalVol;
UINT8 mixVol;
UINT8 initSpeed;
UINT8 initTempo;
UINT8 panSep;
UINT8 midiPbDepth;
UINT16 msgLength;
UINT32 msgOffset; // absolute file offset
UINT32 reserved;
UINT8 chnPan[IT_CHANNELS];
UINT8 chnVol[IT_CHANNELS];
UINT8* orderList;
UINT32* insOfs; // absolute file offsets
UINT32* smpOfs; // absolute file offsets
UINT32* patOfs; // absolute file offsets
} IT_HEAD;
typedef struct _it_edit_history_date
{
UINT16 fatDate;
UINT16 fatTime;
UINT32 dosTimer;
} IT_EHST_DATE;
typedef struct _it_edit_history
{
UINT16 numEntries;
IT_EHST_DATE* Entries;
} IT_EDT_HST;
typedef struct _it_channel_names
{
char fileSig[4];
UINT16 chnCount;
char** chnNames;
} IT_CHN_NAMES;
typedef struct _it_ins_keyboard_table
{
UINT8 note;
UINT8 smplID;
} IT_INS_KBD_TBL;
typedef struct _it_instrument_old
{
char fileSig[4];
char fileName[12];
UINT8 reserved10;
UINT8 flags;
UINT8 volLpStart;
UINT8 volLpEnd;
UINT8 susLpStart;
UINT8 susLpEnd;
UINT8 reserved16;
UINT8 reserved17;
UINT16 fadeOut;
UINT8 newNoteAct;
UINT8 dupNoteChk;
UINT16 trkVer;
UINT8 numSmpls;
UINT8 reserved1F;
char insName[26];
UINT8 initFltCut;
UINT8 initFltRes;
UINT8 midChn;
UINT8 midPrg;
UINT16 midBank;
IT_INS_KBD_TBL noteMap[120];
UINT8 volEnv[200];
//IT_ENV_NODE_OLD envNodes[25];
} IT_INS_OLD;
typedef struct _it_instrument
{
char fileSig[4];
char fileName[12];
UINT8 reserved10;
UINT8 newNoteAct;
UINT8 dupNoteChk;
UINT8 dupNoteAct;
UINT16 fadeOut;
UINT8 ptchPanSep;
UINT8 ptchPanCntr;
UINT8 globalVol;
UINT8 defaultPan;
UINT8 randVol;
UINT8 randPan;
UINT16 trkVer;
UINT8 numSmpls;
UINT8 reserved1F;
char insName[26];
UINT8 initFltCut;
UINT8 initFltRes;
UINT8 midChn;
UINT8 midPrg;
UINT16 midBank;
IT_INS_KBD_TBL noteMap[120];
//IT_ENVELOPE envelopes[3]; // 0 - volume, 1 - pan, 2 - pitch
} IT_INS;
typedef struct _it_instrument_generic
{
char fileSig[4];
char fileName[12];
UINT8 reserved10;
UINT8 misc11[11];
UINT16 trkVer;
UINT8 numSmpls;
UINT8 reserved1F;
char insName[26];
UINT8 initFltCut;
UINT8 initFltRes;
UINT8 midChn;
UINT8 midPrg;
UINT16 midBank;
IT_INS_KBD_TBL noteMap[120];
UINT8 envSpace[250];
} IT_INS_GENERIC;
typedef struct _it_sample
{
char fileSig[4];
char fileName[12];
UINT8 reserved10;
UINT8 globalVol;
UINT8 flags;
UINT8 defaultVol;
char smplName[26];
UINT8 convert;
UINT8 defaultPan;
UINT32 smplLength;
UINT32 loopStart;
UINT32 loopEnd;
UINT32 c5Speed;
UINT32 susLpStart;
UINT32 susLpEnd;
UINT32 smplOfs; // absolute file offset
UINT8 vibSpeed;
UINT8 vibDepth;
UINT8 vibRate;
UINT8 vibType;
} IT_SMPL;
typedef struct _it_row
{
UINT8 mask;
UINT8 note;
UINT8 ins;
UINT8 volcmd;
UINT8 fxcmd;
UINT8 fxval;
} IT_ROW;
typedef struct _it_rows
{
IT_ROW rowChn[IT_CHANNELS];
} IT_ROWS;
typedef struct _it_pattern
{
UINT16 length;
UINT16 rows;
UINT32 reserved04;
UINT8* patDataPtr;
IT_ROWS* patRows;
} IT_PAT;
typedef struct
{
UINT8 type;
UINT8 param1;
UINT8 param2;
} RUNNING_FX;
typedef struct _channel_info
{
UINT8 ins;
UINT8 chnvol;
UINT8 pan;
UINT8 note;
UINT8 smpvol;
union
{
// Flags:
// Bit 0 (01) - Hold Note (for portamento)
// Bit 1 (02) - Sample Volume set (if unset, use default sample volume)
struct
{
UINT8 hold : 1;
UINT8 volset : 1;
};
UINT8 all;
} flags;
UINT8 fx_act[26]; // effect active flags (01 - enabled this frame, 02 - last frame)
UINT8 fx_mem[26]; // effect parameter memory
//RUNNING_FX fx_run[2];
UINT8 midiIns;
UINT8 midiNote;
UINT8 pbRange;
UINT8 midModFX;
UINT8 midModFXLast;
UINT8 midModParam;
} IT_CHN_INFO;
typedef struct _playback_info
{
UINT32 tick;
UINT16 curOrd;
UINT16 curRow;
UINT16 startRow;
UINT8 curSpd;
IT_CHN_INFO chns[IT_CHANNELS];
} IT_PB_INFO;
typedef struct midi_track_event
{
UINT32 tick;
UINT8 type;
UINT8 param1; // note height/controller type
UINT8 param2; // note velocity/controller data
UINT16 dataLen;
UINT8* data;
} MIDTRK_EVENT;
typedef struct midi_track_events
{
UINT32 EvtAlloc;
UINT32 EvtCount;
MIDTRK_EVENT* Evts;
} MIDTRK_EVTS;
void ReadITFile(void);
static UINT8 ReadITHeader(UINT32 baseOfs, IT_HEAD* itHead, UINT32* bytesRead);
static UINT8 ReadITEditHistory(UINT32 baseOfs, IT_EDT_HST* itEdtHist, UINT32* bytesRead);
static UINT8 ReadITChannelNames(UINT32 baseOfs, IT_CHN_NAMES* itChnNames, UINT32* bytesRead);
static UINT8 ReadITInstrument_Gen(UINT32 baseOfs, IT_INS_GENERIC* itIns);
static UINT8 ReadITInstrument(UINT32 baseOfs, IT_INS* itIns);
static UINT8 ReadITSample(UINT32 baseOfs, IT_SMPL* itSmpl);
static UINT8 ReadITPattern(UINT32 baseOfs, IT_PAT* itPat);
static UINT32 ReadITPatternData(UINT32 baseOfs, IT_ROWS* patRow, IT_ROW* chnRowStates);
static MIDTRK_EVENT* GetNewMidiTrkEvt(UINT16 trkID);
static void AddMidiTrkEvt(UINT16 chnID, UINT32 time, UINT8 evtType, UINT8 evtVal1, UINT8 evtVal2);
static void AddMidiTrkEvt_Data(UINT16 chnID, UINT32 time, UINT8 evtType, UINT8 evtSubType, UINT16 dataLen, void* data);
static void ConvertItRowFX(IT_PB_INFO* itPbInf, UINT8 chn, UINT8 col, char fxcmd, UINT8 fxval);
static void ConvertItRow(IT_PB_INFO* itPbInf, UINT8 chn, IT_ROW* rowState);
static void ConvertFX2Mid(IT_PB_INFO* itPbInf, UINT8 chn);
static void GenerateMidiTracks(void);
void ConvertIT2Mid(void);
INLINE double Lin2DB(UINT8 LinVol);
INLINE UINT8 DB2Mid(double DB);
INLINE UINT32 Tempo2Mid(UINT16 BPM, UINT16 TicksPerBeat);
static void WriteEvent(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 Evt, UINT8 Val1, UINT8 Val2);
static void WriteMidiValue(UINT8* Buffer, UINT32* Pos, UINT32 Value);
static void WriteMetaEvent_Data(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 MetaType, UINT32 DataLen, const UINT8* Data);
INLINE UINT16 ReadLE16(const UINT8* Data);
INLINE UINT32 ReadLE32(const UINT8* Data);
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value);
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value);
UINT32 ItLen;
UINT8* ItData;
UINT32 MidLen;
UINT8* MidData;
UINT16 MIDI_RES;
UINT16 MidiTickMult; // MidiTick = ItTick * MidiTickMult
UINT16 ItTickspQrt;
UINT16 itUsedChns;
IT_HEAD ItHead;
IT_EDT_HST ItEdtHist;
IT_CHN_NAMES ItChnNames;
IT_SMPL* ItSmpls;
IT_INS* ItIns;
IT_PAT* ItPats;
UINT16 MidiTrkCount;
MIDTRK_EVTS* MidiTrks;
int main(int argc, char* argv[])
{
FILE* hFile;
if (argc <= 2)
{
printf("Usage: ...\n");
return 0;
}
MidiTickMult = 0x14;
hFile = fopen(argv[1], "rb");
if (hFile == NULL)
{
printf("Error reading file!\n");
return 1;
}
fseek(hFile, 0, SEEK_END);
ItLen = ftell(hFile);
ItData = (UINT8*)malloc(ItLen);
fseek(hFile, 0, SEEK_SET);
fread(ItData, 0x01, ItLen, hFile);
fclose(hFile);
ReadITFile();
ConvertIT2Mid();
hFile = fopen(argv[2], "wb");
if (hFile == NULL)
{
printf("Error reading file!\n");
return 1;
}
fwrite(MidData, 0x01, MidLen, hFile);
fclose(hFile);
return 0;
}
INLINE UINT16 ReadLE16(const UINT8* Data)
{
#if 1
return *(UINT16*)Data;
#else
return (Data[0x00] << 0) | (Data[0x01] << 8);
#endif
}
INLINE UINT32 ReadLE32(const UINT8* Data)
{
#if 1
return *(UINT32*)Data;
#else
return (Data[0x00] << 0) | (Data[0x01] << 8) |
(Data[0x02] << 16) | (Data[0x03] << 24);
#endif
}
void ReadITFile(void)
{
UINT8 retVal;
UINT16 curIdx;
UINT32 procBytes;
UINT32 filePos;
filePos = 0x00;
retVal = ReadITHeader(filePos, &ItHead, &procBytes);
if (retVal)
return;
filePos += procBytes;
if (ItHead.special & 0x02)
{
retVal = ReadITEditHistory(filePos, &ItEdtHist, &procBytes);
if (! retVal)
filePos += procBytes;
}
memset(&ItChnNames, 0x00, sizeof(IT_CHN_NAMES));
retVal = ReadITChannelNames(filePos, &ItChnNames, &procBytes);
if (! retVal)
filePos += procBytes;
ItIns = (IT_INS*)malloc(ItHead.insCount * sizeof(IT_INS));
ItSmpls = (IT_SMPL*)malloc(ItHead.smpCount * sizeof(IT_SMPL));
ItPats = (IT_PAT*)malloc(ItHead.patCount * sizeof(IT_PAT));
for (curIdx = 0; curIdx < ItHead.insCount; curIdx ++)
ReadITInstrument(ItHead.insOfs[curIdx], &ItIns[curIdx]);
for (curIdx = 0; curIdx < ItHead.smpCount; curIdx ++)
ReadITSample(ItHead.smpOfs[curIdx], &ItSmpls[curIdx]);
itUsedChns = 0x00;
for (curIdx = 0; curIdx < ItHead.patCount; curIdx ++)
ReadITPattern(ItHead.patOfs[curIdx], &ItPats[curIdx]);
return;
}
static UINT8 ReadITHeader(UINT32 baseOfs, IT_HEAD* itHead, UINT32* bytesRead)
{
UINT32 curPos;
UINT16 curIdx;
curPos = baseOfs;
memcpy(&itHead->fileSig, &ItData[curPos + 0x00], 4);
if (strncmp(itHead->fileSig, "IMPM", 4))
return 0xFF;
memcpy(&itHead->songName, &ItData[curPos + 0x04], 26);
itHead->pHilight = ReadLE16(&ItData[curPos + 0x1E]);
itHead->ordCount = ReadLE16(&ItData[curPos + 0x20]);
itHead->insCount = ReadLE16(&ItData[curPos + 0x22]);
itHead->smpCount = ReadLE16(&ItData[curPos + 0x24]);
itHead->patCount = ReadLE16(&ItData[curPos + 0x26]);
itHead->verCreate = ReadLE16(&ItData[curPos + 0x28]);
itHead->verCompat = ReadLE16(&ItData[curPos + 0x2A]);
itHead->flags = ReadLE16(&ItData[curPos + 0x2C]);
itHead->special = ReadLE16(&ItData[curPos + 0x2E]);
itHead->globalVol = ItData[curPos + 0x30];
itHead->mixVol = ItData[curPos + 0x31];
itHead->initSpeed = ItData[curPos + 0x32];
itHead->initTempo = ItData[curPos + 0x33];
itHead->panSep = ItData[curPos + 0x34];
itHead->midiPbDepth = ItData[curPos + 0x35];
itHead->msgLength = ReadLE16(&ItData[curPos + 0x36]);
itHead->msgOffset = ReadLE32(&ItData[curPos + 0x38]);
itHead->reserved = ReadLE32(&ItData[curPos + 0x3C]);
curPos += 0x40;
memcpy(itHead->chnPan, &ItData[curPos], IT_CHANNELS);
curPos += IT_CHANNELS;
memcpy(itHead->chnVol, &ItData[curPos], IT_CHANNELS);
curPos += IT_CHANNELS;
itHead->orderList = (UINT8*)malloc(itHead->ordCount * sizeof(UINT8));
itHead->insOfs = (UINT32*)malloc(itHead->insCount * sizeof(UINT32));
itHead->smpOfs = (UINT32*)malloc(itHead->smpCount * sizeof(UINT32));
itHead->patOfs = (UINT32*)malloc(itHead->patCount * sizeof(UINT32));
memcpy(itHead->orderList, &ItData[curPos], itHead->ordCount);
curPos += itHead->ordCount;
for (curIdx = 0; curIdx < itHead->insCount; curIdx ++, curPos += 0x04)
itHead->insOfs[curIdx] = ReadLE32(&ItData[curPos]);
for (curIdx = 0; curIdx < itHead->smpCount; curIdx ++, curPos += 0x04)
itHead->smpOfs[curIdx] = ReadLE32(&ItData[curPos]);
for (curIdx = 0; curIdx < itHead->patCount; curIdx ++, curPos += 0x04)
itHead->patOfs[curIdx] = ReadLE32(&ItData[curPos]);
if (bytesRead != NULL)
*bytesRead = curPos - baseOfs;
return 0x00;
}
static UINT8 ReadITEditHistory(UINT32 baseOfs, IT_EDT_HST* itEdtHist, UINT32* bytesRead)
{
UINT32 curPos;
UINT16 curHst;
curPos = baseOfs;
itEdtHist->numEntries = ReadLE16(&ItData[curPos]);
curPos += 0x02;
if (itEdtHist->numEntries == 0)
{
itEdtHist->Entries = NULL;
}
else
{
itEdtHist->Entries = (IT_EHST_DATE*)malloc(itEdtHist->numEntries * sizeof(IT_EHST_DATE));
for (curHst = 0; curHst < itEdtHist->numEntries; curHst ++, curPos += 0x08)
{
itEdtHist->Entries[curHst].fatDate = ReadLE16(&ItData[curPos + 0x00]);
itEdtHist->Entries[curHst].fatTime = ReadLE16(&ItData[curPos + 0x02]);
itEdtHist->Entries[curHst].dosTimer = ReadLE16(&ItData[curPos + 0x04]);
}
}
if (bytesRead != NULL)
*bytesRead = curPos - baseOfs;
return 0x00;
}
static UINT8 ReadITChannelNames(UINT32 baseOfs, IT_CHN_NAMES* itChnNames, UINT32* bytesRead)
{
UINT32 curPos;
UINT32 dataSize;
UINT16 curChn;
curPos = baseOfs;
memcpy(itChnNames->fileSig, &ItData[curPos + 0x00], 4);
if (strncmp(itChnNames->fileSig, "CNAM", 4))
return 0xFF;
dataSize = ReadLE32(&ItData[curPos + 0x04]);
curPos += 0x08;
itChnNames->chnCount = dataSize / 20;
if (itChnNames->chnCount == 0)
{
itChnNames->chnNames = NULL;
}
else
{
itChnNames->chnNames = (char**)malloc(itChnNames->chnCount * sizeof(char*));
for (curChn = 0; curChn < itChnNames->chnCount; curChn ++, curPos += 20)
itChnNames->chnNames[curChn] = (char*)&ItData[curPos];
}
if (bytesRead != NULL)
*bytesRead = curPos - baseOfs;
return 0x00;
}
static UINT8 ReadITInstrument_Gen(UINT32 baseOfs, IT_INS_GENERIC* itIns)
{
UINT8 curNote;
UINT32 curPos;
memcpy(itIns->fileSig, &ItData[baseOfs + 0x00], 4);
if (strncmp(itIns->fileSig, "IMPI", 4))
return 0xFF;
memcpy(itIns->fileName, &ItData[baseOfs + 0x04], 12);
itIns->reserved10 = ItData[baseOfs + 0x10];
itIns->trkVer = ReadLE16(&ItData[baseOfs + 0x1C]);
itIns->numSmpls = ItData[baseOfs + 0x1E];
itIns->reserved1F = ItData[baseOfs + 0x1F];
memcpy(itIns->insName, &ItData[baseOfs + 0x20], 26);
itIns->initFltCut = ItData[baseOfs + 0x3A];
itIns->initFltRes = ItData[baseOfs + 0x3B];
itIns->midChn = ItData[baseOfs + 0x3C];
itIns->midPrg = ItData[baseOfs + 0x3D];
itIns->midBank = ReadLE16(&ItData[baseOfs + 0x3E]);
curPos = baseOfs + 0x40;
for (curNote = 0; curNote < 120; curNote ++, curPos += 0x02)
{
itIns->noteMap[curNote].note = ItData[curPos + 0x00];
itIns->noteMap[curNote].smplID = ItData[curPos + 0x01];
}
return 0x00;
}
static UINT8 ReadITInstrument(UINT32 baseOfs, IT_INS* itIns)
{
UINT8 retVal;
UINT8 curEnv;
UINT8 curNode;
UINT32 curPos;
retVal = ReadITInstrument_Gen(baseOfs, (IT_INS_GENERIC*)itIns);
if (retVal)
return retVal;
itIns->newNoteAct = ItData[baseOfs + 0x11];
itIns->dupNoteChk = ItData[baseOfs + 0x12];
itIns->dupNoteAct = ItData[baseOfs + 0x13];
itIns->fadeOut = ReadLE16(&ItData[baseOfs + 0x14]);
itIns->ptchPanSep = ItData[baseOfs + 0x16];
itIns->ptchPanCntr = ItData[baseOfs + 0x17];
itIns->globalVol = ItData[baseOfs + 0x18];
itIns->defaultPan = ItData[baseOfs + 0x19];
itIns->randVol = ItData[baseOfs + 0x1A];
itIns->randPan = ItData[baseOfs + 0x1B];
curPos = baseOfs + 0x40 + 120 * 0x02;
for (curEnv = 0; curEnv < 3; curEnv ++)
{
curPos += 0x06;
for (curNode = 0; curNode < 25; curNode ++, curPos += 0x03)
{
}
}
return 0x00;
}
static UINT8 ReadITSample(UINT32 baseOfs, IT_SMPL* itSmpl)
{
memcpy(itSmpl->fileSig, &ItData[baseOfs + 0x00], 4);
if (strncmp(itSmpl->fileSig, "IMPS", 4))
return 0xFF;
memcpy(itSmpl->fileName, &ItData[baseOfs + 0x04], 12);
itSmpl->reserved10 = ItData[baseOfs + 0x10];
itSmpl->globalVol = ItData[baseOfs + 0x11];
itSmpl->flags = ItData[baseOfs + 0x12];
itSmpl->defaultVol = ItData[baseOfs + 0x13];
memcpy(itSmpl->smplName, &ItData[baseOfs + 0x14], 26);
itSmpl->convert = ItData[baseOfs + 0x2E];
itSmpl->defaultPan = ItData[baseOfs + 0x2F];
itSmpl->smplLength = ReadLE32(&ItData[baseOfs + 0x30]);
itSmpl->loopStart = ReadLE32(&ItData[baseOfs + 0x34]);
itSmpl->loopEnd = ReadLE32(&ItData[baseOfs + 0x38]);
itSmpl->c5Speed = ReadLE32(&ItData[baseOfs + 0x3C]);
itSmpl->susLpStart = ReadLE32(&ItData[baseOfs + 0x40]);
itSmpl->susLpEnd = ReadLE32(&ItData[baseOfs + 0x44]);
itSmpl->smplOfs = ReadLE32(&ItData[baseOfs + 0x48]);
itSmpl->vibSpeed = ItData[baseOfs + 0x4C];
itSmpl->vibDepth = ItData[baseOfs + 0x4D];
itSmpl->vibRate = ItData[baseOfs + 0x4E];
itSmpl->vibType = ItData[baseOfs + 0x4F];
return 0x00;
}
static UINT8 ReadITPattern(UINT32 baseOfs, IT_PAT* itPat)
{
UINT16 curRow;
UINT32 curPos;
IT_ROW rowStates[IT_CHANNELS];
itPat->length = ReadLE16(&ItData[baseOfs + 0x00]);
itPat->rows = ReadLE16(&ItData[baseOfs + 0x02]);
itPat->reserved04 = ReadLE32(&ItData[baseOfs + 0x04]);
curPos = baseOfs + 0x08;
itPat->patDataPtr = &ItData[curPos];
itPat->patRows = (IT_ROWS*)malloc(itPat->rows * sizeof(IT_ROWS));
memset(rowStates, 0x00, IT_CHANNELS * sizeof(IT_ROW));
for (curRow = 0; curRow < itPat->rows; curRow ++)
curPos += ReadITPatternData(curPos, &itPat->patRows[curRow], rowStates);
return 0x00;
}
static UINT32 ReadITPatternData(UINT32 baseOfs, IT_ROWS* patRow, IT_ROW* chnRowStates)
{
UINT32 curPos;
IT_ROW* rowState;
UINT8 chnMask;
UINT8 chnID;
IT_ROW* tempChn;
memset(patRow->rowChn, 0x00, IT_CHANNELS * sizeof(IT_ROW));
curPos = baseOfs;
chnMask = ItData[curPos]; curPos ++;
while(chnMask)
{
chnID = (chnMask - 1) & 0x3F;
tempChn = &patRow->rowChn[chnID];
rowState = &chnRowStates[chnID];
if (chnMask & 0x80)
{
rowState->mask = ItData[curPos];
curPos ++;
}
if (rowState->mask & 0x01)
{
if (chnID >= itUsedChns)
itUsedChns = chnID + 1;
rowState->note = ItData[curPos];
curPos ++;
}
if (rowState->mask & 0x02)
{
rowState->ins = ItData[curPos];
curPos ++;
}
if (rowState->mask & 0x04)
{
rowState->volcmd = ItData[curPos];
curPos ++;
}
if (rowState->mask & 0x08)
{
rowState->fxcmd = ItData[curPos]; curPos ++;
rowState->fxval = ItData[curPos]; curPos ++;
}
tempChn->mask = rowState->mask;
if (rowState->mask & 0x11)
tempChn->note = rowState->note;
if (rowState->mask & 0x22)
tempChn->ins = rowState->ins;
if (rowState->mask & 0x44)
tempChn->volcmd = rowState->volcmd;
if (rowState->mask & 0x88)
{
tempChn->fxcmd = rowState->fxcmd;
tempChn->fxval = rowState->fxval;
}
chnMask = ItData[curPos]; curPos ++;
}
return curPos - baseOfs;
}
static MIDTRK_EVENT* GetNewMidiTrkEvt(UINT16 trkID)
{
MIDTRK_EVTS* midTrk;
MIDTRK_EVENT* midEvt;
if (trkID >= MidiTrkCount)
return NULL;
midTrk = &MidiTrks[trkID];
if (midTrk->EvtCount >= midTrk->EvtAlloc)
{
midTrk->EvtAlloc += 0x10000; // 64K more elements
midTrk->Evts = (MIDTRK_EVENT*)realloc(midTrk->Evts, midTrk->EvtAlloc * sizeof(MIDTRK_EVENT));
}
midEvt = &midTrk->Evts[midTrk->EvtCount];
memset(midEvt, 0x00, sizeof(MIDTRK_EVENT));
midTrk->EvtCount ++;
return midEvt;
}
static void AddMidiTrkEvt(UINT16 chnID, UINT32 time, UINT8 evtType, UINT8 evtVal1, UINT8 evtVal2)
{
MIDTRK_EVENT* midEvt;
midEvt = GetNewMidiTrkEvt(1 + chnID);
if (midEvt == NULL)
return;
midEvt->tick = time;
if (evtType < 0xF0)
midEvt->type = (evtType & 0xF0) | (chnID & 0x0F);
else
midEvt->type = evtType;
midEvt->param1 = evtVal1;
midEvt->param2 = evtVal2;
return;
}
static void AddMidiTrkEvt_Data(UINT16 chnID, UINT32 time, UINT8 evtType, UINT8 evtSubType, UINT16 dataLen, void* data)
{
MIDTRK_EVENT* midEvt;
midEvt = GetNewMidiTrkEvt(1 + chnID);
if (midEvt == NULL)
return;
midEvt->tick = time;
midEvt->type = evtType;
midEvt->param1 = evtSubType;
midEvt->dataLen = dataLen;
midEvt->data = (UINT8*)malloc(dataLen);
memcpy(midEvt->data, data, dataLen);
return;
}
static void ConvertItRowFX(IT_PB_INFO* itPbInf, UINT8 chn, UINT8 col, char fxcmd, UINT8 fxval)
{
IT_CHN_INFO* chnInf;
UINT8 fxMemID;
UINT8 fxActID;
UINT8 tempByt;
UINT8 evtParam;
UINT8 fxHigh;
UINT8 fxLow;
chnInf = &itPbInf->chns[chn];
fxActID = fxcmd - 'A';
fxMemID = fxActID;
fxHigh = (fxval & 0xF0) >> 4;
fxLow = (fxval & 0x0F) >> 4;
// do effect memory
if (fxcmd == 'F')
fxMemID = 'E' - 'A';
if (fxActID < 26)
{
if ((fxcmd >= 'D' && fxcmd <= 'L') ||
(fxcmd >= 'N' && fxcmd <= 'R') ||
fxcmd == 'U' || fxcmd == 'W' || fxcmd == 'Y')
{
if (! fxval)
fxval = chnInf->fx_mem[fxMemID];
}
chnInf->fx_act[fxActID] |= 0x01;
if (chnInf->fx_mem[fxMemID] != fxval)
chnInf->fx_act[fxActID] |= 0x80;
chnInf->fx_mem[fxMemID] = fxval;
if (fxMemID != fxActID)
chnInf->fx_mem[fxActID] = fxval;
}
switch(fxcmd)
{
case 'A': // Set Speed
if (fxval)
itPbInf->curSpd = fxval;
break;
case 'B': // Jump to Order
// prevent backwards jumps
if (fxval >= itPbInf->curOrd)
{
itPbInf->curOrd = fxval - 1;
itPbInf->curRow = 0xFFFE;
}
break;
case 'C': // Break to Row
itPbInf->startRow = fxval;
itPbInf->curRow = 0xFFFE;
break;
case 'G': // Portamento
chnInf->flags.hold = 1;
break;
case 'H': // Vibrato
break;
case 'J': // Arpeggio
break;
case 'M': // Set Channel Volume
if (fxval != chnInf->chnvol)
{
chnInf->chnvol = fxval;
evtParam = DB2Mid(Lin2DB(chnInf->chnvol));
AddMidiTrkEvt(chn, 0, 0xB0, 0x07, evtParam);
}
break;
case 'X': // set Pan
tempByt = (fxval + 2) / 4;
if (chnInf->pan != tempByt)
{
chnInf->pan = tempByt;
evtParam = fxval / 2;
AddMidiTrkEvt(chn, itPbInf->tick, 0xB0, 0x0A, evtParam);
}
break;
}
return;
}
static UINT8 GetInsSmplVol(UINT8 insID, UINT8 note)
{
IT_INS* itIns;
IT_SMPL* itSmpl;
UINT8 smplID;
if (note >= 120)
return 0x40;
if (! ItHead.insCount)
{
smplID = insID;
}
else
{
insID --;
if (insID >= ItHead.insCount)
return 0x40;
itIns = &ItIns[insID];
smplID = itIns->noteMap[note].smplID;
}
smplID --;
if (smplID >= ItHead.smpCount)
return 0x40;
itSmpl = &ItSmpls[smplID];
return itSmpl->defaultVol;
}
static void ConvertItRow(IT_PB_INFO* itPbInf, UINT8 chn, IT_ROW* rowState)
{
IT_CHN_INFO* chnInf;
UINT8 tempByt;
UINT8 evtParam;
UINT8 newNote;
chnInf = &itPbInf->chns[chn];
chnInf->flags.all = 0x00;
for (tempByt = 0; tempByt < 26; tempByt ++)
chnInf->fx_act[tempByt] = (chnInf->fx_act[tempByt] & 0x01) << 1;
if (rowState->mask & 0x22)
{
if (rowState->ins && chnInf->ins != rowState->ins)
{
chnInf->ins = rowState->ins;
tempByt = chnInf->ins - 1;
if (chnInf->midiIns != tempByt)
{
chnInf->midiIns = tempByt;
AddMidiTrkEvt(chn, itPbInf->tick, 0xC0, chnInf->midiIns, 0x00);
}
}
}
if (rowState->mask & 0x44)
{
if (rowState->volcmd <= 64)
{
tempByt = rowState->volcmd - 0x00; // Volume
if (chnInf->smpvol != tempByt)
{
chnInf->smpvol = tempByt;
#ifndef USE_NOTE_VEL
evtParam = DB2Mid(Lin2DB(chnInf->smpvol));
AddMidiTrkEvt(chn, itPbInf->tick, 0xB0, 0x0B, evtParam);
#endif
}
chnInf->flags.volset = 1; // set "Sample Volume" flag
}
else if (rowState->volcmd < 75)
{
tempByt = rowState->volcmd - 65; // Fine volume up
ConvertItRowFX(itPbInf, chn, 1, 'D', (tempByt << 4) | 0x0F);
}
else if (rowState->volcmd < 85)
{
tempByt = rowState->volcmd - 75; // Fine volume down
ConvertItRowFX(itPbInf, chn, 1, 'D', 0xF0 | (tempByt << 0));
}
else if (rowState->volcmd < 95)
{
tempByt = rowState->volcmd - 85; // Volume slide up
ConvertItRowFX(itPbInf, chn, 1, 'D', (tempByt << 4) | 0x00);
}
else if (rowState->volcmd < 105)
{
tempByt = rowState->volcmd - 95; // Volume slide down
ConvertItRowFX(itPbInf, chn, 1, 'D', 0x00 | (tempByt << 0));
}
else if (rowState->volcmd < 115)
{
tempByt = rowState->volcmd - 105; // Pitch Slide down
ConvertItRowFX(itPbInf, chn, 1, 'E', tempByt * 4);
}
else if (rowState->volcmd < 125)
{
tempByt = rowState->volcmd - 115; // Pitch Slide up
ConvertItRowFX(itPbInf, chn, 1, 'F', tempByt * 4);
}
else if (rowState->volcmd < 0x80)
{
}
else if (rowState->volcmd <= 0xC0)
{
tempByt = rowState->volcmd - 0x80; // Panning
if (chnInf->pan != tempByt)
{
chnInf->pan = tempByt;
evtParam = (tempByt >= 0x40) ? 0x7F : (tempByt * 2);
AddMidiTrkEvt(chn, itPbInf->tick, 0xB0, 0x0A, evtParam);
}
}
else if (rowState->volcmd < 203)
{
static const UINT8 portTable[10] =
{ 0x00, 0x01, 0x04, 0x08, 0x10, 0x20, 0x40, 0x60, 0x80, 0xFF};
tempByt = rowState->volcmd - 193; // Portamento
ConvertItRowFX(itPbInf, chn, 1, 'G', portTable[tempByt]);
}
else if (rowState->volcmd < 213)
{
tempByt = rowState->volcmd - 203; // Vibrato
ConvertItRowFX(itPbInf, chn, 1, 'H', 0x00 | (tempByt << 0));
}
}
if (rowState->mask & 0x88)
{
ConvertItRowFX(itPbInf, chn, 0, (char)(rowState->fxcmd - 1 + 'A'), rowState->fxval);
}
if (rowState->mask & 0x11)
chnInf->note = rowState->note; // for FX delay detection
ConvertFX2Mid(itPbInf, chn);
if (rowState->mask & 0x11)
{
chnInf->note = rowState->note;
newNote = rowState->note;
if (rowState->note < 0xFE)
{
if (! chnInf->flags.volset) // if sample volume was NOT overridden
{
tempByt = GetInsSmplVol(chnInf->ins, chnInf->note);
if (chnInf->smpvol != tempByt)
{
chnInf->smpvol = tempByt;
#ifndef USE_NOTE_VEL
evtParam = DB2Mid(Lin2DB(chnInf->smpvol));
AddMidiTrkEvt(chn, itPbInf->tick, 0xB0, 0x0B, evtParam);
#endif
}
}
newNote += 0; // apply instrument transpose
}
else // Note Off/Cut
{
chnInf->flags.hold = 0;
#ifdef IGNORE_NOTE_OFF
if (rowState->note == 0xFF)