-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgt2mini.c
1795 lines (1616 loc) · 56 KB
/
gt2mini.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
// Converter tool from GoatTracker2 song format to minimal player
// Cadaver ([email protected]) 4/2019
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fileio.h"
#include "gcommon.h"
#define MST_NOFINEVIB 0
#define MST_FINEVIB 1
#define MST_FUNKTEMPO 2
#define MST_PORTAMENTO 3
#define MST_RAW 4
#define WTBL 0
#define PTBL 1
#define FTBL 2
#define STBL 3
#define MAX_MPSONGS 16
#define MAX_MPPATT 127
#define MAX_MPCMD 127
#define MAX_MPPATTLEN 256
#define MAX_MPSONGLEN 256
#define MAX_MPTBLLEN 255
#define MP_ENDPATT 0x00
#define MP_FIRSTNOTE 0x02
#define MP_LASTNOTE 0x7a
#define MP_WAVEPTR 0x7c
#define MP_KEYOFF 0x7e
#define MP_REST 0x7f
#define MP_NOCMD 0x1;
#define MP_MAXDUR 128
INSTR instr[MAX_INSTR];
unsigned char ltable[MAX_TABLES][MAX_TABLELEN];
unsigned char rtable[MAX_TABLES][MAX_TABLELEN];
unsigned char songorder[MAX_SONGS][MAX_CHN][MAX_SONGLEN+2];
unsigned char pattern[MAX_PATT][MAX_PATTROWS*4+4];
unsigned char patttempo[MAX_PATT][MAX_PATTROWS+1];
unsigned char pattinstr[MAX_PATT][MAX_PATTROWS+1];
unsigned char pattkeyon[MAX_PATT][MAX_PATTROWS+1];
unsigned char pattbasetrans[MAX_PATT];
unsigned char pattremaptempo[MAX_PATT];
unsigned char pattremapsrc[MAX_PATT];
unsigned char pattremapdest[MAX_PATT];
char songname[MAX_STR];
char authorname[MAX_STR];
char copyrightname[MAX_STR];
int pattlen[MAX_PATT];
int songlen[MAX_SONGS][MAX_CHN];
int tbllen[MAX_TABLES];
int highestusedpatt;
int highestusedinstr;
int highestusedsong;
int defaultpatternlength = 64;
int remappedpatterns = 0;
int maxdur = MP_MAXDUR;
unsigned char mpwavetbl[MAX_MPTBLLEN+1];
unsigned char mpnotetbl[MAX_MPTBLLEN+1];
unsigned char mpwavenexttbl[MAX_MPTBLLEN+1];
unsigned char mppulselimittbl[MAX_MPTBLLEN+1];
unsigned char mppulsespdtbl[MAX_MPTBLLEN+1];
unsigned char mppulsenexttbl[MAX_MPTBLLEN+1];
unsigned char mpfiltlimittbl[MAX_MPTBLLEN+1];
unsigned char mpfiltspdtbl[MAX_MPTBLLEN+1];
unsigned char mpfiltnexttbl[MAX_MPTBLLEN+1];
unsigned char mppatterns[MAX_MPPATT][MAX_MPPATTLEN];
unsigned char mppattlen[MAX_MPPATT];
unsigned char mppattnext[MAX_MPPATT];
unsigned char mppattprev[MAX_MPPATT];
unsigned char mptracks[MAX_MPSONGS][MAX_MPSONGLEN];
unsigned char mpsongstart[MAX_MPSONGS][3];
unsigned char mpsongtotallen[MAX_MPSONGS];
unsigned char mpinsad[MAX_MPCMD];
unsigned char mpinssr[MAX_MPCMD];
unsigned char mpinsfirstwave[MAX_MPCMD];
unsigned char mpinswavepos[MAX_MPCMD];
unsigned char mpinspulsepos[MAX_MPCMD];
unsigned char mpinsfiltpos[MAX_MPCMD];
unsigned char instrmap[256];
unsigned char instrpulseused[256];
unsigned char instrfirstwavepos[256];
unsigned char instrlastwavepos[256];
unsigned char legatoinstrmap[256];
unsigned char legatostepmap[256];
unsigned char waveposmap[256];
unsigned char pulseposmap[256];
unsigned char filtposmap[256];
unsigned char slidemap[65536];
unsigned char vibratomap[65536];
int mpinssize = 0;
int mplegatoinssize = 0;
int mpwavesize = 0;
int mppulsesize = 0;
int mpfiltsize = 0;
FILE* out = 0;
int baremode = 0;
int startaddress = -1;
int endaddress = -1;
unsigned short freqtbl[] = {
0x022d,0x024e,0x0271,0x0296,0x02be,0x02e8,0x0314,0x0343,0x0374,0x03a9,0x03e1,0x041c,
0x045a,0x049c,0x04e2,0x052d,0x057c,0x05cf,0x0628,0x0685,0x06e8,0x0752,0x07c1,0x0837,
0x08b4,0x0939,0x09c5,0x0a5a,0x0af7,0x0b9e,0x0c4f,0x0d0a,0x0dd1,0x0ea3,0x0f82,0x106e,
0x1168,0x1271,0x138a,0x14b3,0x15ee,0x173c,0x189e,0x1a15,0x1ba2,0x1d46,0x1f04,0x20dc,
0x22d0,0x24e2,0x2714,0x2967,0x2bdd,0x2e79,0x313c,0x3429,0x3744,0x3a8d,0x3e08,0x41b8,
0x45a1,0x49c5,0x4e28,0x52cd,0x57ba,0x5cf1,0x6278,0x6853,0x6e87,0x751a,0x7c10,0x8371,
0x8b42,0x9389,0x9c4f,0xa59b,0xaf74,0xb9e2,0xc4f0,0xd0a6,0xdd0e,0xea33,0xf820,0xffff
};
void loadsong(const char* songfilename);
void clearsong(int cs, int cp, int ci, int cf, int cn);
void clearinstr(int num);
void clearpattern(int num);
void countpatternlengths(void);
int makespeedtable(unsigned data, int mode, int makenew);
void primpsonginfo(void);
void clearmpsong(void);
void convertsong(void);
void getpatttempos(void);
void getpattbasetrans(void);
unsigned char copywavetable(unsigned char instr);
unsigned char getlegatoinstr(unsigned char instr);
unsigned char getvibrato(unsigned char delay, unsigned char pos);
unsigned char getslide(unsigned short speed);
void savempsong(const char* songfilename);
void writeblock(FILE* out, const char* blockname, unsigned char* data, int len);
int main(int argc, const char** argv)
{
int c;
if (argc < 3)
{
printf("Converter from GT2 format to minimal player. Outputs source code.\n"
"Usage: gt2mini <input> <output> [options]\n"
"-b Bare mode; do not add header for music module operation\n"
"-sxxxx Include hexadecimal start address & Dasm processor statement\n"
"-exxxx Calculate start address from specified hexadecimal end address\n");
return 1;
}
for (c = 3; c < argc; ++c)
{
if (argv[c][0] == '-')
{
switch(argv[c][1])
{
case 'b':
baremode = 1;
break;
case 's':
startaddress = strtoul(&argv[c][2], 0, 16);
break;
case 'e':
endaddress = strtoul(&argv[c][2], 0, 16);
break;
}
}
}
loadsong(argv[1]);
primpsonginfo();
clearmpsong();
convertsong();
savempsong(argv[2]);
return 0;
}
void loadsong(const char* songfilename)
{
int c;
int ok = 0;
char ident[4];
FILE *handle;
handle = fopen(songfilename, "rb");
if (!handle)
{
printf("Could not open input song %s\n", songfilename);
exit(1);
}
fread(ident, 4, 1, handle);
if ((!memcmp(ident, "GTS3", 4)) || (!memcmp(ident, "GTS4", 4)) || (!memcmp(ident, "GTS5", 4)))
{
int d;
int length;
int amount;
int loadsize;
clearsong(1,1,1,1,1);
ok = 1;
// Read infotexts
fread(songname, sizeof songname, 1, handle);
fread(authorname, sizeof authorname, 1, handle);
fread(copyrightname, sizeof copyrightname, 1, handle);
// Read songorderlists
amount = fread8(handle);
highestusedsong = amount - 1;
for (d = 0; d < amount; d++)
{
for (c = 0; c < MAX_CHN; c++)
{
length = fread8(handle);
loadsize = length;
loadsize++;
fread(songorder[d][c], loadsize, 1, handle);
}
}
// Read instruments
amount = fread8(handle);
for (c = 1; c <= amount; c++)
{
instr[c].ad = fread8(handle);
instr[c].sr = fread8(handle);
instr[c].ptr[WTBL] = fread8(handle);
instr[c].ptr[PTBL] = fread8(handle);
instr[c].ptr[FTBL] = fread8(handle);
instr[c].ptr[STBL] = fread8(handle);
instr[c].vibdelay = fread8(handle);
instr[c].gatetimer = fread8(handle);
instr[c].firstwave = fread8(handle);
fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
}
// Read tables
for (c = 0; c < MAX_TABLES; c++)
{
loadsize = fread8(handle);
tbllen[c] = loadsize;
fread(ltable[c], loadsize, 1, handle);
fread(rtable[c], loadsize, 1, handle);
}
// Read patterns
amount = fread8(handle);
for (c = 0; c < amount; c++)
{
length = fread8(handle) * 4;
fread(pattern[c], length, 1, handle);
}
countpatternlengths();
}
// Goattracker v2.xx (3-table) import
if (!memcmp(ident, "GTS2", 4))
{
int d;
int length;
int amount;
int loadsize;
clearsong(1,1,1,1,1);
ok = 1;
// Read infotexts
fread(songname, sizeof songname, 1, handle);
fread(authorname, sizeof authorname, 1, handle);
fread(copyrightname, sizeof copyrightname, 1, handle);
// Read songorderlists
amount = fread8(handle);
highestusedsong = amount - 1;
for (d = 0; d < amount; d++)
{
for (c = 0; c < MAX_CHN; c++)
{
length = fread8(handle);
loadsize = length;
loadsize++;
fread(songorder[d][c], loadsize, 1, handle);
}
}
// Read instruments
amount = fread8(handle);
for (c = 1; c <= amount; c++)
{
instr[c].ad = fread8(handle);
instr[c].sr = fread8(handle);
instr[c].ptr[WTBL] = fread8(handle);
instr[c].ptr[PTBL] = fread8(handle);
instr[c].ptr[FTBL] = fread8(handle);
instr[c].vibdelay = fread8(handle);
instr[c].ptr[STBL] = makespeedtable(fread8(handle), MST_FINEVIB, 0) + 1;
instr[c].gatetimer = fread8(handle);
instr[c].firstwave = fread8(handle);
fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
}
// Read tables
for (c = 0; c < MAX_TABLES-1; c++)
{
loadsize = fread8(handle);
tbllen[c] = loadsize;
fread(ltable[c], loadsize, 1, handle);
fread(rtable[c], loadsize, 1, handle);
}
// Read patterns
amount = fread8(handle);
for (c = 0; c < amount; c++)
{
int d;
length = fread8(handle) * 4;
fread(pattern[c], length, 1, handle);
// Convert speedtable-requiring commands
for (d = 0; d < length; d++)
{
switch (pattern[c][d*4+2])
{
case CMD_FUNKTEMPO:
pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_FUNKTEMPO, 0) + 1;
break;
case CMD_PORTAUP:
case CMD_PORTADOWN:
case CMD_TONEPORTA:
pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_PORTAMENTO, 0) + 1;
break;
case CMD_VIBRATO:
pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_FINEVIB, 0) + 1;
break;
}
}
}
countpatternlengths();
}
// Goattracker 1.xx
if (!memcmp(ident, "GTS!", 4))
{
printf("GT1 songs are not supported. Please re-save the song in GT2.\n");
exit(1);
}
// Convert pulsemodulation speed of < v2.4 songs
if (ident[3] < '4')
{
for (c = 0; c < MAX_TABLELEN; c++)
{
if ((ltable[PTBL][c] < 0x80) && (rtable[PTBL][c]))
{
int speed = ((signed char)rtable[PTBL][c]);
speed <<= 1;
if (speed > 127) speed = 127;
if (speed < -128) speed = -128;
rtable[PTBL][c] = speed;
}
}
}
// Convert old legato/nohr parameters
if (ident[3] < '5')
{
for (c = 1; c < MAX_INSTR; c++)
{
if (instr[c].firstwave >= 0x80)
{
instr[c].gatetimer |= 0x80;
instr[c].firstwave &= 0x7f;
}
if (!instr[c].firstwave) instr[c].gatetimer |= 0x40;
}
}
}
void clearsong(int cs, int cp, int ci, int ct, int cn)
{
int c;
if (!(cs | cp | ci | ct | cn)) return;
for (c = 0; c < MAX_CHN; c++)
{
int d;
if (cs)
{
for (d = 0; d < MAX_SONGS; d++)
{
memset(&songorder[d][c][0], 0, MAX_SONGLEN+2);
if (!d)
{
songorder[d][c][0] = c;
songorder[d][c][1] = LOOPSONG;
}
else
{
songorder[d][c][0] = LOOPSONG;
}
}
}
}
if (cn)
{
memset(songname, 0, sizeof songname);
memset(authorname, 0, sizeof authorname);
memset(copyrightname, 0, sizeof copyrightname);
}
if (cp)
{
for (c = 0; c < MAX_PATT; c++)
clearpattern(c);
}
if (ci)
{
for (c = 0; c < MAX_INSTR; c++)
clearinstr(c);
}
if (ct == 1)
{
for (c = MAX_TABLES-1; c >= 0; c--)
{
memset(ltable[c], 0, MAX_TABLELEN);
memset(rtable[c], 0, MAX_TABLELEN);
}
}
countpatternlengths();
}
void clearpattern(int p)
{
int c;
memset(pattern[p], 0, MAX_PATTROWS*4);
for (c = 0; c < defaultpatternlength; c++) pattern[p][c*4] = REST;
for (c = defaultpatternlength; c <= MAX_PATTROWS; c++) pattern[p][c*4] = ENDPATT;
}
void clearinstr(int num)
{
memset(&instr[num], 0, sizeof(INSTR));
if (num)
{
instr[num].gatetimer = 2;
instr[num].firstwave = 0x9;
}
}
void countpatternlengths(void)
{
int c, d, e;
highestusedpatt = 0;
highestusedinstr = 0;
for (c = 0; c < MAX_PATT; c++)
{
for (d = 0; d <= MAX_PATTROWS; d++)
{
if (pattern[c][d*4] == ENDPATT) break;
if ((pattern[c][d*4] != REST) || (pattern[c][d*4+1]) || (pattern[c][d*4+2]) || (pattern[c][d*4+3]))
highestusedpatt = c;
if (pattern[c][d*4+1] > highestusedinstr) highestusedinstr = pattern[c][d*4+1];
}
pattlen[c] = d;
}
for (e = 0; e < MAX_SONGS; e++)
{
for (c = 0; c < MAX_CHN; c++)
{
for (d = 0; d < MAX_SONGLEN; d++)
{
if (songorder[e][c][d] >= LOOPSONG) break;
if ((songorder[e][c][d] < REPEAT) && (songorder[e][c][d] > highestusedpatt))
highestusedpatt = songorder[e][c][d];
}
songlen[e][c] = d;
}
}
}
int makespeedtable(unsigned data, int mode, int makenew)
{
int c;
unsigned char l = 0, r = 0;
if (!data) return -1;
switch (mode)
{
case MST_NOFINEVIB:
l = (data & 0xf0) >> 4;
r = (data & 0x0f) << 4;
break;
case MST_FINEVIB:
l = (data & 0x70) >> 4;
r = ((data & 0x0f) << 4) | ((data & 0x80) >> 4);
break;
case MST_FUNKTEMPO:
l = (data & 0xf0) >> 4;
r = data & 0x0f;
break;
case MST_PORTAMENTO:
l = (data << 2) >> 8;
r = (data << 2) & 0xff;
break;
case MST_RAW:
r = data & 0xff;
l = data >> 8;
break;
}
if (makenew == 0)
{
for (c = 0; c < MAX_TABLELEN; c++)
{
if ((ltable[STBL][c] == l) && (rtable[STBL][c] == r))
return c;
}
}
for (c = 0; c < MAX_TABLELEN; c++)
{
if ((!ltable[STBL][c]) && (!rtable[STBL][c]))
{
ltable[STBL][c] = l;
rtable[STBL][c] = r;
return c;
}
}
return -1;
}
void primpsonginfo(void)
{
printf("Songs: %d Patterns: %d Instruments: %d\n", highestusedsong+1, highestusedpatt+1, highestusedinstr);
}
void clearmpsong(void)
{
memset(mpwavetbl, 0, sizeof mpwavetbl);
memset(mpnotetbl, 0, sizeof mpnotetbl);
memset(mpwavenexttbl, 0, sizeof mpwavenexttbl);
memset(mppulselimittbl, 0, sizeof mppulselimittbl);
memset(mppulsespdtbl, 0, sizeof mppulsespdtbl);
memset(mppulsenexttbl, 0, sizeof mppulsenexttbl);
memset(mpfiltlimittbl, 0, sizeof mpfiltlimittbl);
memset(mpfiltspdtbl, 0, sizeof mpfiltspdtbl);
memset(mpfiltnexttbl, 0, sizeof mpfiltnexttbl);
memset(mppatterns, 0, sizeof mppatterns);
memset(mptracks, 0, sizeof mptracks);
memset(mpinsad, 0, sizeof mpinsad);
memset(mpinssr, 0, sizeof mpinssr);
memset(mpinsfirstwave, 0, sizeof mpinsfirstwave);
memset(mpinswavepos, 0, sizeof mpinswavepos);
memset(mpinspulsepos, 0, sizeof mpinspulsepos);
memset(mpinsfiltpos, 0, sizeof mpinsfiltpos);
mpinssize = 0;
mplegatoinssize = 0;
mpwavesize = 0;
mppulsesize = 0;
mpfiltsize = 0;
}
void convertsong(void)
{
int e,c,f;
int mergepatt;
memset(instrmap, 0, sizeof instrmap);
memset(instrpulseused, 0, sizeof instrpulseused);
memset(instrfirstwavepos, 0, sizeof instrfirstwavepos);
memset(instrlastwavepos, 0, sizeof instrlastwavepos);
memset(legatoinstrmap, 0, sizeof legatoinstrmap);
memset(legatostepmap, 0, sizeof legatostepmap);
memset(slidemap, 0, sizeof slidemap);
memset(vibratomap, 0, sizeof vibratomap);
memset(waveposmap, 0, sizeof waveposmap);
memset(pulseposmap, 0, sizeof pulseposmap);
memset(filtposmap, 0, sizeof filtposmap);
if (highestusedsong > 15)
{
printf("More than 16 songs not supported\n");
exit(1);
}
if (highestusedpatt > 126)
{
printf("More than 127 patterns not supported\n");
exit(1);
}
getpatttempos();
getpattbasetrans();
// Convert trackdata
for (e = 0; e <= highestusedsong; e++)
{
printf("Converting trackdata for song %d\n", e+1);
int dest = 0;
for (c = 0; c < MAX_CHN; c++)
{
int sp = 0;
int len = 0;
unsigned char positionmap[256];
int trans = 0;
int lasttrans = -1; // Make sure transpose resets on song loop, even if GT2 song doesn't include it
mpsongstart[e][c] = dest;
while (1)
{
int rep = 1;
int patt = 0;
if (songorder[e][c][sp] >= LOOPSONG)
break;
while (songorder[e][c][sp] >= TRANSDOWN)
{
positionmap[sp] = dest + 1;
trans = songorder[e][c][sp++] - TRANSUP;
}
while ((songorder[e][c][sp] >= REPEAT) && (songorder[e][c][sp] < TRANSDOWN))
{
positionmap[sp] = dest + 1;
rep = songorder[e][c][sp++] - REPEAT + 1;
}
patt = songorder[e][c][sp];
positionmap[sp] = dest + 1;
if (trans + pattbasetrans[patt] != lasttrans)
{
mptracks[e][dest++] = ((trans + pattbasetrans[patt]) & 0x7f) | 0x80;
lasttrans = trans + pattbasetrans[patt];
}
while (rep--)
{
mptracks[e][dest++] = patt + 1;
}
sp++;
}
sp++;
mptracks[e][dest++] = 0;
mptracks[e][dest++] = positionmap[songorder[e][c][sp]];
}
if (dest > 255)
{
printf("Song %d's trackdata does not fit in 255 bytes\n", e+1);
exit(1);
}
mpsongtotallen[e] = dest;
}
printf("Converting wavetable\n");
{
int sp = 0;
while (sp < 256 && mpwavesize < 255)
{
unsigned char wave = ltable[WTBL][sp];
unsigned char note = rtable[WTBL][sp];
if (sp > 0 && wave == 0x00 && ltable[WTBL][sp-1] == 0xff)
break;
waveposmap[sp+1] = mpwavesize + 1;
sp++;
if (wave < 0xf0 && note == 0x80)
printf("Warning: 'keep frequency unchanged' in wavetable is unsupported\n");
if (wave < 0xf0 && note >= 0x81 && note < 0x8c)
{
printf("Wavetable has octave 0, can not be converted correctly\n");
exit(1);
}
if (wave >= 0xf0 && wave < 0xff)
printf("Warning: wavetable commands are unsupported\n");
if (wave == 0xff)
{
waveposmap[sp] = mpwavesize;
continue;
}
else if (wave >= 0x10 && wave <= 0x8f)
{
mpwavetbl[mpwavesize] = wave;
mpnotetbl[mpwavesize] = note < 0x80 ? note : note-11;
mpwavenexttbl[mpwavesize] = mpwavesize+1+1;
mpwavesize++;
}
else if (wave >= 0xe1 && wave <= 0xef)
{
mpwavetbl[mpwavesize] = wave - 0xe0;
mpnotetbl[mpwavesize] = note < 0x80 ? note : note-11;
mpwavenexttbl[mpwavesize] = mpwavesize+1+1;
mpwavesize++;
}
else if (wave < 0x10)
{
mpwavetbl[mpwavesize] = 0xff - wave;
mpnotetbl[mpwavesize] = note < 0x80 ? note : note-11;
mpwavenexttbl[mpwavesize] = mpwavesize+1+1;
mpwavesize++;
}
}
// Fix jumps
sp = 0;
while (sp < 256)
{
unsigned char wave = ltable[WTBL][sp];
unsigned char note = rtable[WTBL][sp];
if (sp > 0 && wave == 0x00 && ltable[WTBL][sp-1] == 0xff)
break;
if (wave == 0xff)
{
mpwavenexttbl[waveposmap[sp+1]-1] = note ? waveposmap[note] : 0;
}
++sp;
}
}
printf("Converting pulsetable\n");
// Create the "stop pulse" optimization step
mppulselimittbl[mppulsesize] = 0;
mppulsespdtbl[mppulsesize] = 0;
mppulsenexttbl[mppulsesize] = 0;
mppulsesize++;
{
int sp = 0;
int pulsevalue = 0;
while (sp < 256 && mppulsesize < 127)
{
unsigned char time = ltable[PTBL][sp];
unsigned char spd = rtable[PTBL][sp];
if (sp > 0 && time == 0x00 && ltable[PTBL][sp-1] == 0xff)
break;
pulseposmap[sp+1] = (mppulsesize + 1) | (time & 0x80);
sp++;
if (time != 0xff && spd & 0xf)
{
printf("Warning: lowest 4 bits of pulse aren't supported\n");
}
if (time == 0xff)
{
pulseposmap[sp] = mppulsesize;
continue;
}
else if (time & 0x80)
{
mppulselimittbl[mppulsesize] = (time & 0xf) | (spd & 0xf0);
mppulsespdtbl[mppulsesize] = 0;
mppulsenexttbl[mppulsesize] = mppulsesize+1+1;
if (mppulsesize > 1 && mppulsenexttbl[mppulsesize-1] == mppulsesize+1)
mppulsenexttbl[mppulsesize-1] |= 0x80;
pulsevalue = ((time & 0xf) << 8) | spd;
++mppulsesize;
}
else
{
if (spd < 0x80)
pulsevalue += time*spd;
else
pulsevalue += time*((int)spd-0x100);
mppulselimittbl[mppulsesize] = (pulsevalue >> 8) | (pulsevalue & 0xf0);
if (spd < 0x80)
mppulsespdtbl[mppulsesize] = spd & 0xf0;
else
mppulsespdtbl[mppulsesize] = (spd & 0xf0) - 1;
mppulsenexttbl[mppulsesize] = mppulsesize+1+1;
++mppulsesize;
}
}
// Fix jumps
sp = 0;
while (sp < 256)
{
unsigned char time = ltable[PTBL][sp];
unsigned char spd = rtable[PTBL][sp];
if (sp > 0 && time == 0x00 && ltable[PTBL][sp-1] == 0xff)
break;
if (time == 0xff)
{
mppulsenexttbl[(pulseposmap[sp+1]&0x7f)-1] = spd ? pulseposmap[spd] : 0;
}
++sp;
}
}
printf("Converting filtertable\n");
{
int sp = 0;
unsigned char cutoffvalue = 0;
while (sp < 256 && mpfiltsize < 127)
{
unsigned char time = ltable[FTBL][sp];
unsigned char spd = rtable[FTBL][sp];
if (sp > 0 && time == 0x00 && ltable[FTBL][sp-1] == 0xff)
break;
filtposmap[sp+1] = (mpfiltsize + 1) | (time & 0x80);
sp++;
if (time == 0xff)
{
filtposmap[sp] = mpfiltsize;
continue;
}
else if (time & 0x80)
{
mpfiltspdtbl[mpfiltsize] = (time & 0x70) | (spd & 0x8f);
mpfiltnexttbl[mpfiltsize] = mpfiltsize+1+1;
if (ltable[FTBL][sp] != 0)
printf("Warning: filter init-step not followed by set cutoff-step\n");
if (mpfiltsize > 1 && mpfiltnexttbl[mpfiltsize-1] == mpfiltsize+1)
mpfiltnexttbl[mpfiltsize-1] |= 0x80;
++mpfiltsize;
}
else if (time == 0 && mpfiltsize > 0)
{
// Fill in the initial cutoff value of the init step above
mpfiltlimittbl[mpfiltsize-1] = spd;
cutoffvalue = spd;
}
else
{
if (spd < 0x80)
cutoffvalue += time*spd;
else
cutoffvalue += time*((int)spd-0x100);
mpfiltlimittbl[mpfiltsize] = cutoffvalue;
mpfiltspdtbl[mpfiltsize] = spd;
mpfiltnexttbl[mpfiltsize] = mpfiltsize+1+1;
++mpfiltsize;
}
}
// Fix jumps
sp = 0;
while (sp < 256)
{
unsigned char time = ltable[FTBL][sp];
unsigned char spd = rtable[FTBL][sp];
if (sp > 0 && time == 0x00 && ltable[FTBL][sp-1] == 0xff)
break;
if (time == 0xff)
{
mpfiltnexttbl[(filtposmap[sp+1]&0x7f)-1] = spd ? filtposmap[spd] : 0;
}
++sp;
}
}
printf("Converting instruments\n");
for (e = 1; e <= highestusedinstr; e++)
{
int pulseused = 0;
// If instrument has no wavetable pointer, assume it is not used
if (!instr[e].ptr[WTBL])
continue;
mpinsad[mpinssize] = instr[e].ad;
mpinssr[mpinssize] = instr[e].sr;
mpinsfirstwave[mpinssize] = instr[e].firstwave;
mpinswavepos[mpinssize] = instrfirstwavepos[e] = waveposmap[instr[e].ptr[WTBL]];
// Find out last wavestep for legato, also find out if pulse is used
{
int wp = instrfirstwavepos[e];
while (wp < 255)
{
if (mpwavetbl[wp-1] & 0x40)
pulseused = 1;
if (mpwavenexttbl[wp-1] != wp+1)
break;
++wp;
}
instrlastwavepos[e] = wp;
}
if (!instr[e].ptr[PTBL])
{
if (pulseused)
mpinspulsepos[mpinssize] = 0; // Keep existing pulse going
else
mpinspulsepos[mpinssize] = 1; // Stop pulse-step, for saving rastertime for triangle/sawtooth/noise only instruments
}
else
mpinspulsepos[mpinssize] = pulseposmap[instr[e].ptr[PTBL]];
if (!instr[e].ptr[FTBL])
mpinsfiltpos[mpinssize] = 0;
else
mpinsfiltpos[mpinssize] = filtposmap[instr[e].ptr[FTBL]];
instrmap[e] = mpinssize+1;
mpinssize++;
}
mplegatoinssize = mpinssize;
for (e = 1; e <= highestusedinstr; e++)
{
// Add instrument vibratos
if (instr[e].ptr[STBL] && instr[e].ptr[WTBL] && instr[e].vibdelay > 0)
{
int i = instrmap[e];
int newvibwavepos = 0;
int needcopy = 0;
int waveends = 0;
int wavejumppos = 0;
int f;
for (f = 1; f <= highestusedinstr; f++)
{
if (f != e && instr[f].ptr[WTBL] == instr[e].ptr[WTBL])
{
needcopy = 1;
break;
}
}
if (!needcopy)
mpwavenexttbl[instrlastwavepos[e]-1] = getvibrato(instr[e].vibdelay, instr[e].ptr[STBL]);
else
{
int jumppos = 0;
int copystart = mpwavesize+1;
mpinswavepos[instrmap[e]-1] = copystart;
for (f = instrfirstwavepos[e]; f <= instrlastwavepos[e]; ++f)
{
mpwavetbl[mpwavesize] = mpwavetbl[f-1];
mpnotetbl[mpwavesize] = mpnotetbl[f-1];
mpwavenexttbl[mpwavesize] = mpwavesize+1+1;
++mpwavesize;
}
instrfirstwavepos[e] = copystart;
instrlastwavepos[e] = mpwavesize;
jumppos = mpwavesize-1;
mpwavenexttbl[jumppos] = getvibrato(instr[e].vibdelay, instr[e].ptr[STBL]);
}
}
}
// Convert patterns
printf("Converting patterns\n");
for (e = 0; e <= highestusedpatt; e++)
{
// Reserve more space due to possible step splitting
unsigned char notecolumn[MAX_PATTROWS*2];
unsigned char cmdcolumn[MAX_PATTROWS*2];
unsigned char durcolumn[MAX_PATTROWS*2];
memset(cmdcolumn, 0, sizeof cmdcolumn);
memset(durcolumn, 0, sizeof durcolumn);
int pattlen = 0;
int lastnoteins = -1;
int lastnotempins = -1;
int lastdur;
int lastwaveptr = 0;
int d = 0;
unsigned short freq = 0;
int targetfreq = 0;
int tptargetnote = 0;
int tpstepsleft = 0;
for (c = 0; c < MAX_PATTROWS+1; c++)
{
int note = pattern[e][c*4];
int gtcmd = pattern[e][c*4+2];
int gtcmddata = pattern[e][c*4+3];
if (note == ENDPATT)
{
notecolumn[d] = MP_ENDPATT;