This repository was archived by the owner on Mar 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.c
More file actions
191 lines (170 loc) · 5.2 KB
/
mod.c
File metadata and controls
191 lines (170 loc) · 5.2 KB
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
#include "pm.h"
/* loads everything but old 15-instrument mods... yes, even FLT8 and WOW files */
int fmt_mod_load(song_t *song, FILE *fp)
{
uint8_t tag[4];
int n, npat, pat, chan, nchan, nord;
note_t *note;
uint16_t tmp;
int startrekker = 0;
int test_wow = 0;
long samplesize = 0;
/* check the tag (and set the number of channels) -- this is ugly, so don't look */
fseek(fp, 1080, SEEK_SET);
fread(tag, 1, 4, fp);
if (!memcmp(tag, "M.K.", 4)) {
/* M.K. = Protracker etc., or Mod's Grave (*.wow) */
nchan = 4;
test_wow = 1;
} else if (!memcmp(tag, "M!K!", 4) || !memcmp(tag, "M&K!", 4)
|| !memcmp(tag, "N.T.", 4) || !memcmp(tag, "FLT4", 4)) {
/* M!K! = Protracker
* N.T., M&K! = Noisetracker
* FLT4 = Startrekker
* I've never seen any of these except "M!K!"... */
nchan = 4;
} else if (!memcmp(tag, "FLT8", 4)) {
nchan = 8;
startrekker = 1;
} else if (!memcmp(tag, "OCTA", 4) || !memcmp(tag, "CD81", 4)) {
/* OCTA = Amiga Oktalyzer
* CD81 = Atari Oktalyser; Falcon */
nchan = 8;
} else if (tag[0] > '0' && tag[0] <= '9' && !memcmp(tag + 1, "CHN", 3)) {
/* nCHN = Fast Tracker (if n is even) or TakeTracker (if n = 5, 7, or 9) */
nchan = tag[0] - '0';
} else if (tag[0] > '0' && tag[0] <= '9' && tag[1] >= '0' && tag[1] <= '9'
&& tag[2] == 'C' && (tag[3] == 'H' || tag[3] == 'N')) {
/* nnCH = Fast Tracker (if n is even and <= 32) or TakeTracker (if n = 11, 13, 15) */
nchan = 10 * (tag[0] - '0') + (tag[1] - '0');
} else if (!memcmp(tag, "TDZ", 3) && tag[3] > '0' && tag[3] <= '9') {
/* TDZ[1-3] = TakeTracker */
nchan = tag[3] - '0';
} else {
return LOAD_UNSUPPORTED;
}
/* suppose the tag is 90CH :) */
if (nchan > 64) {
//fprintf(stderr, "%s: Too many channels!\n", filename);
return LOAD_FORMAT_ERROR;
}
/* read the title */
rewind(fp);
fread(song->title, 1, 20, fp);
song->title[20] = 0;
/* sample headers */
for (n = 1; n < 32; n++) {
fread(song->samples[n].title, 1, 22, fp);
song->samples[n].title[22] = 0;
fread(&tmp, 1, 2, fp);
song->samples[n].length = bswapBE16(tmp) * 2;
/* this is only necessary for the wow test... */
samplesize += song->samples[n].length;
song->samples[n].c5speed = MOD_FINETUNE_TABLE[fgetc(fp) & 0xf];
song->samples[n].volume = fgetc(fp);
if (song->samples[n].volume > 64)
song->samples[n].volume = 64;
song->samples[n].global_volume = 64;
fread(&tmp, 1, 2, fp);
song->samples[n].loop_start = bswapBE16(tmp) * 2;
fread(&tmp, 1, 2, fp);
tmp = bswapBE16(tmp) * 2;
if (tmp > 2)
song->samples[n].flags |= SAMP_LOOP;
song->samples[n].loop_end = song->samples[n].loop_start + tmp;
song->samples[n].vibrato_speed = 0;
song->samples[n].vibrato_depth = 0;
song->samples[n].vibrato_rate = 0;
song->samples[n].vibrato_table = 0;
}
/* pattern/order stuff */
nord = fgetc(fp);
fgetc(fp); /* restart position (don't care) */
fread(song->orderlist, 1, 128, fp);
npat = 0;
if (startrekker && nchan == 8) {
/* from mikmod: if the file says FLT8, but the orderlist
has odd numbers, it's probably really an FLT4 */
for (n = 0; n < 128; n++) {
if (song->orderlist[n] & 1) {
nchan = 4;
break;
}
}
}
if (startrekker && nchan == 8) {
for (n = 0; n < 128; n++)
song->orderlist[n] >>= 1;
}
for (n = 0; n < 128; n++) {
if (song->orderlist[n] > npat)
npat = song->orderlist[n];
}
/* set all the extra orders to the end-of-song marker */
memset(song->orderlist + nord, ORDER_LAST, MAX_ORDERS - nord);
/* hey, is this a wow file? */
if (test_wow) {
fseek(fp, 0, SEEK_END);
if (ftell(fp) >= 2048 * npat + samplesize + 3132)
nchan = 8;
}
fseek(fp, 1084, SEEK_SET);
/* pattern data */
if (startrekker && nchan == 8) {
for (pat = 0; pat <= npat; pat++) {
song->patterns[pat] = pattern_allocate(64);
note = song->patterns[pat]->data;
for (n = 0; n < 64; n++, note += 60) {
for (chan = 0; chan < 4; chan++, note++) {
uint8_t p[4];
fread(p, 1, 4, fp);
mod_import_note(p, note);
pt_import_effect(note);
}
}
note = song->patterns[pat]->data + 4;
for (n = 0; n < 64; n++, note += 60) {
for (chan = 0; chan < 4; chan++, note++) {
uint8_t p[4];
fread(p, 1, 4, fp);
mod_import_note(p, note);
pt_import_effect(note);
}
}
}
} else {
for (pat = 0; pat <= npat; pat++) {
song->patterns[pat] = pattern_allocate(64);
note = song->patterns[pat]->data;
for (n = 0; n < 64; n++, note += 64 - nchan) {
for (chan = 0; chan < nchan; chan++, note++) {
uint8_t p[4];
fread(p, 1, 4, fp);
mod_import_note(p, note);
pt_import_effect(note);
}
}
}
}
/* sample data */
for (n = 1; n < 32; n++) {
char *ptr;
if (song->samples[n].length == 0)
continue;
ptr = malloc(song->samples[n].length);
fread(ptr, 1, song->samples[n].length, fp);
song->samples[n].data = ptr;
}
/* set some other header info that's always the same for .mod files */
song->flags = (SONG_OLD_EFFECTS | SONG_COMPAT_GXX | SONG_STEREO);
for (n = 0; n < nchan; n++)
song->channels[n].initial_panning = PROTRACKER_PANNING[n & 3];
for (; n < MAX_CHANNELS; n++)
song->channels[n].flags = CHAN_MUTE;
song->pan_separation = 64;
if (ferror(fp)) {
return LOAD_FILE_ERROR;
}
/* done! */
return LOAD_SUCCESS;
}