Mercurial > hg > dmlib
annotate minijss/jloadxm.c @ 778:a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 24 May 2013 01:34:00 +0300 |
parents | c430112449a7 |
children | 97ecc0a9c21f |
rev | line source |
---|---|
0 | 1 /* |
2 * miniJSS - Fast Tracker ][ (XM) module loader | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2006-2007 Tecnic Software productions (TNSP) | |
5 * | |
6 * TO DO: | |
7 * - Add support for 1.02/1.03 XM-format versions. | |
8 * (Not very useful, but if it's not too hard, then do it) | |
9 */ | |
10 #include "jssmod.h" | |
11 #include <string.h> | |
12 | |
13 | |
14 /* XM value limit definitions | |
15 */ | |
16 #define XM_MaxChannels (32) | |
17 #define XM_MaxPatterns (256) | |
18 #define XM_MaxOrders (255) | |
19 #define XM_MaxInstruments (128) | |
20 #define XM_MaxInstSamples (16) | |
21 #define XM_MaxEnvPoints (12) | |
22 #define XM_MaxNotes (96) | |
23 #define XM_MaxSampleVolume (64) | |
24 | |
25 | |
26 /* XM format structures | |
27 */ | |
28 typedef struct | |
29 { | |
30 char idMagic[17]; // XM header ID "Extended Module: " | |
31 char songName[20]; // Module song name | |
32 Uint8 unUsed1A; // ALWAYS 0x1a | |
33 char trackerName[20]; // ID-string of tracker software | |
34 Uint16 version; // XM-version 0x0104 | |
35 Uint32 headSize; // Module header size, FROM THIS POINT! | |
36 Uint16 norders, // Number of orders | |
37 defRestartPos, // Default song restart position | |
38 nchannels, // Number of channels | |
39 npatterns, // Number of patterns | |
40 ninstruments, // Number of instruments | |
552
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
41 flags, // Module flags: |
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
42 // bit0: 0 = Amiga frequency table |
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
43 // 1 = Linear frequency table |
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
44 // |
0 | 45 defSpeed, // Default speed |
46 defTempo; // Default tempo | |
47 Uint8 orderList[256]; // Order list | |
48 } XMHeader; | |
49 | |
50 | |
51 typedef struct | |
52 { | |
53 Uint32 headSize; // Instrument header size (see docs!) | |
54 char instName[22]; // Name/description | |
55 Uint8 instType; // Type | |
56 Uint16 nsamples; // Number of samples | |
57 } XMInstrument1; | |
58 | |
59 | |
60 typedef struct | |
61 { | |
62 Uint16 frame, value; | |
552
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
63 } XMEnvPoint; |
0 | 64 |
65 | |
66 typedef struct | |
67 { | |
68 Uint8 flags, npoints, sustain, loopS, loopE; | |
552
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
69 XMEnvPoint points[XM_MaxEnvPoints]; |
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
70 } XMEnvelope; |
0 | 71 |
72 | |
73 typedef struct | |
74 { | |
75 Uint32 headSize; // Header size | |
76 Uint8 sNumForNotes[XM_MaxNotes]; // Sample numbers for notes | |
552
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
77 XMEnvelope volumeEnv, panningEnv; |
0 | 78 Uint8 vibratoType, vibratoSweep, vibratoDepth, vibratoRate; |
79 | |
80 Uint16 fadeOut, ARESERVED; | |
81 } XMInstrument2; | |
82 | |
83 | |
84 typedef struct | |
85 { | |
86 Uint32 size, loopS, loopL; | |
87 Uint8 volume; | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
88 Sint8 fineTune; |
0 | 89 Uint8 type, panning; |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
90 Sint8 relNote; |
0 | 91 Uint8 ARESERVED; |
92 char sampleName[22]; | |
93 } XMSample; | |
94 | |
95 | |
96 typedef struct | |
97 { | |
98 Uint32 headSize; | |
99 Uint8 packing; | |
100 Uint16 nrows, size; | |
101 } XMPattern; | |
102 | |
103 | |
104 | |
105 /* Unpack XM-format pattern from file-stream into JSS-pattern structure | |
106 */ | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
107 #define JSGETBYTE(XV) do { \ |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
108 size--; \ |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
109 if (size < 0) \ |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
110 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
111 "Unexpected end of packed pattern data.\n"); \ |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
112 XV = dmfgetc(inFile); \ |
0 | 113 } while (0) |
114 | |
115 | |
313
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
116 static int jssXMConvertNote(int val) |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
117 { |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
118 if (val < 1 || val > 97) |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
119 return jsetNotSet; |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
120 else if (val == 97) |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
121 return jsetNoteOff; |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
122 else |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
123 return val - 1; |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
124 } |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
125 |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
126 |
0 | 127 static int jssXMUnpackPattern(DMResource *inFile, int size, JSSPattern *pattern) |
128 { | |
129 int packb, row, channel, tmp; | |
130 JSSNote *pnote; | |
131 assert(pattern != NULL); | |
132 | |
133 pnote = pattern->data; | |
134 | |
313
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
135 for (row = 0; row < pattern->nrows && size > 0; row++) |
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
136 for (channel = 0; channel < pattern->nchannels && size > 0; channel++) |
0 | 137 { |
138 JSGETBYTE(packb); | |
139 if (packb & 0x80) | |
140 { | |
141 if (packb & 0x01) | |
142 { | |
143 // PACK 0x01: Read note | |
144 JSGETBYTE(tmp); | |
313
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
145 pnote->note = jssXMConvertNote(tmp); |
0 | 146 } |
147 | |
148 if (packb & 0x02) | |
149 { | |
150 // PACK 0x02: Read instrument | |
151 JSGETBYTE(tmp); | |
152 pnote->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
153 } | |
154 | |
155 if (packb & 0x04) | |
156 { | |
157 // PACK 0x04: Read volume | |
158 JSGETBYTE(tmp); | |
159 pnote->volume = (tmp >= 0x10) ? tmp - 0x10 : jsetNotSet; | |
160 } | |
161 | |
162 if (packb & 0x08) | |
163 { | |
164 // PACK 0x08: Read effect | |
165 JSGETBYTE(pnote->effect); | |
166 pnote->param = 0; | |
167 } | |
168 | |
169 if (packb & 0x10) | |
170 { | |
171 // PACK 0x10: Read effect parameter | |
172 JSGETBYTE(pnote->param); | |
173 if (pnote->effect == jsetNotSet && pnote->param != 0) | |
174 pnote->effect = 0; | |
175 } | |
176 } | |
177 else | |
178 { | |
179 // All data available | |
313
a89500f26dde
Cleanup the pattern decoder a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
183
diff
changeset
|
180 pnote->note = jssXMConvertNote(packb & 0x7f); |
0 | 181 |
182 // Get instrument | |
183 JSGETBYTE(tmp); | |
184 pnote->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
185 | |
186 // Get volume | |
187 JSGETBYTE(tmp); | |
188 pnote->volume = (tmp >= 0x10) ? tmp - 0x10 : jsetNotSet; | |
189 | |
190 // Get effect | |
191 JSGETBYTE(pnote->effect); | |
192 | |
193 // Get parameter | |
194 JSGETBYTE(pnote->param); | |
195 if (pnote->effect == 0 && pnote->param == 0) | |
196 pnote->effect = pnote->param = jsetNotSet; | |
197 } | |
198 pnote++; | |
199 } | |
200 | |
201 // Check the state | |
202 if (size > 0) | |
203 { | |
204 // Some data left unparsed | |
205 JSSWARNING(DMERR_EXTRA_DATA, DMERR_EXTRA_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
206 "Unparsed data after pattern (%d bytes), possibly broken file.\n", size); |
0 | 207 } |
208 | |
209 return DMERR_OK; | |
210 } | |
211 | |
212 | |
213 /* Convert XM envelope structure to JSS envelope structure | |
214 */ | |
552
d4cee32e7050
Rename internal structures, cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents:
313
diff
changeset
|
215 static int jssXMConvertEnvelope(JSSEnvelope * d, XMEnvelope * s, char * e, int instr) |
0 | 216 { |
217 int i; | |
218 (void) e; (void) instr; | |
219 | |
220 // Convert envelope points | |
221 for (i = 0; i < XM_MaxEnvPoints; i++) | |
222 { | |
183
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
223 d->points[i].frame = s->points[i].frame; |
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
224 d->points[i].value = s->points[i].value; |
0 | 225 } |
226 | |
227 // Convert other values | |
183
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
228 d->npoints = s->npoints; |
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
229 d->sustain = s->sustain; |
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
230 d->loopS = s->loopS; |
a65f0c3deaa7
Some more player logic cleanups and variable renames.
Matti Hamalainen <ccr@tnsp.org>
parents:
98
diff
changeset
|
231 d->loopE = s->loopE; |
0 | 232 |
233 // Check if the envelope is used | |
234 if (s->flags & 0x01) | |
235 { | |
236 // Convert envelope flags | |
237 d->flags = jenvfUsed; | |
238 if (s->flags & 0x02) | |
239 d->flags |= jenvfSustain; | |
240 | |
241 if (s->flags & 0x04) | |
242 d->flags |= jenvfLooped; | |
243 | |
244 // Check other values | |
245 if (s->npoints > XM_MaxEnvPoints) | |
246 { | |
247 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
248 "Inst#%d/%s-env: nPoints > MAX, possibly broken file.\n", instr, e); |
0 | 249 s->npoints = XM_MaxEnvPoints; |
250 } | |
251 | |
252 if ((d->flags & jenvfSustain) && s->sustain > s->npoints) | |
253 { | |
254 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
255 "Inst#%d/%s-env: iSustain > nPoints (%d > %d), possibly broken file.\n", |
0 | 256 instr, e, s->sustain, s->npoints); |
257 s->sustain = s->npoints; | |
258 } | |
259 | |
260 if ((d->flags & jenvfLooped) && s->loopE > s->npoints) | |
261 { | |
262 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
263 "Inst#%d/%s-env: loopE > nPoints (%d > %d), possibly broken file.\n", |
0 | 264 instr, e, s->loopE, s->npoints); |
265 s->loopE = s->npoints; | |
266 } | |
267 | |
268 if ((d->flags & jenvfLooped) && s->loopS > s->loopE) | |
269 { | |
270 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
271 "Inst#%d/%s-env: loopS > loopE (%d > %d), possibly broken file.\n", |
0 | 272 instr, e, s->loopS, s->loopE); |
273 s->loopS = 0; | |
274 } | |
275 } | |
276 | |
277 return DMERR_OK; | |
278 } | |
279 | |
280 | |
281 /* Load XM-format extended instrument from file-stream into JSS module's given inst | |
282 */ | |
283 static int jssXMLoadExtInstrument(DMResource *inFile, int ninst, JSSModule *module) | |
284 { | |
285 XMInstrument1 xmI1; | |
286 off_t pos, remainder; | |
287 | |
288 // Get instrument header #1 | |
289 pos = dmftell(inFile); | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
290 if (!dmf_read_le32(inFile, &xmI1.headSize) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
291 !dmf_read_str(inFile, &xmI1.instName, sizeof(xmI1.instName)) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
292 !dmf_read_byte(inFile, &xmI1.instType) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
293 !dmf_read_le16(inFile, &xmI1.nsamples)) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
294 return DMERR_FREAD; |
0 | 295 |
296 // If there are samples, there is header #2 | |
297 if (xmI1.nsamples > 0) | |
298 { | |
299 int i, nsample, tmp; | |
300 int xmConvTable[XM_MaxInstruments + 1]; | |
301 JSSExtInstrument *pEInst; | |
302 JSSInstrument *pInst; | |
303 XMInstrument2 xmI2; | |
304 | |
305 // Allocate instrument | |
306 if ((pEInst = jssAllocateExtInstrument()) == NULL) | |
307 { | |
308 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
309 "Could not allocate extended instrument structure #%d\n", ninst); |
0 | 310 } |
311 | |
312 module->extInstruments[ninst] = pEInst; | |
313 | |
314 // Get instrument header #2 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
315 if (!dmf_read_le32(inFile, &xmI2.headSize) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
316 !dmf_read_str(inFile, &xmI2.sNumForNotes, sizeof(xmI2.sNumForNotes))) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
317 { |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
318 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
319 "Could not read secondary instrument header #1 for #%d.\n", ninst); |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
320 } |
0 | 321 |
322 for (i = 0; i < XM_MaxEnvPoints; i++) | |
323 { | |
324 dmf_read_le16(inFile, &xmI2.volumeEnv.points[i].frame); | |
325 dmf_read_le16(inFile, &xmI2.volumeEnv.points[i].value); | |
326 } | |
327 | |
328 for (i = 0; i < XM_MaxEnvPoints; i++) | |
329 { | |
330 dmf_read_le16(inFile, &xmI2.panningEnv.points[i].frame); | |
331 dmf_read_le16(inFile, &xmI2.panningEnv.points[i].value); | |
332 } | |
333 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
334 if (!dmf_read_byte(inFile, &xmI2.volumeEnv.npoints) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
335 !dmf_read_byte(inFile, &xmI2.panningEnv.npoints) || |
0 | 336 |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
337 !dmf_read_byte(inFile, &xmI2.volumeEnv.sustain) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
338 !dmf_read_byte(inFile, &xmI2.volumeEnv.loopS) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
339 !dmf_read_byte(inFile, &xmI2.volumeEnv.loopE) || |
0 | 340 |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
341 !dmf_read_byte(inFile, &xmI2.panningEnv.sustain) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
342 !dmf_read_byte(inFile, &xmI2.panningEnv.loopS) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
343 !dmf_read_byte(inFile, &xmI2.panningEnv.loopE) || |
0 | 344 |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
345 !dmf_read_byte(inFile, &xmI2.volumeEnv.flags) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
346 !dmf_read_byte(inFile, &xmI2.panningEnv.flags) || |
0 | 347 |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
348 !dmf_read_byte(inFile, &xmI2.vibratoType) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
349 !dmf_read_byte(inFile, &xmI2.vibratoSweep) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
350 !dmf_read_byte(inFile, &xmI2.vibratoDepth) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
351 !dmf_read_byte(inFile, &xmI2.vibratoRate) || |
0 | 352 |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
353 !dmf_read_le16(inFile, &xmI2.fadeOut) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
354 !dmf_read_le16(inFile, &xmI2.ARESERVED)) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
355 { |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
356 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
357 "Could not read secondary instrument header #2 for #%d.\n", ninst); |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
358 } |
0 | 359 |
360 // Skip the extra data after header #2 | |
361 remainder = xmI1.headSize - (dmftell(inFile) - pos); | |
362 if (remainder > 0) | |
363 { | |
98 | 364 JSSDEBUG("xmI1#1 Skipping: %li\n", remainder); |
0 | 365 dmfseek(inFile, remainder, SEEK_CUR); |
366 } | |
367 | |
368 // Check and convert all ext instrument information | |
369 #ifndef JSS_LIGHT | |
370 pEInst->desc = jssASCIItoStr(xmI1.instName, 0, sizeof(xmI1.instName)); | |
371 #endif | |
372 jssXMConvertEnvelope(&pEInst->volumeEnv, &xmI2.volumeEnv, "vol", ninst); | |
373 jssXMConvertEnvelope(&pEInst->panningEnv, &xmI2.panningEnv, "pan", ninst); | |
374 | |
375 switch (xmI2.vibratoType) | |
376 { | |
377 case 0: pEInst->vibratoType = jvibSine; break; | |
378 case 1: pEInst->vibratoType = jvibRamp; break; | |
379 case 2: pEInst->vibratoType = jvibSquare; break; | |
380 case 3: pEInst->vibratoType = jvibRandom; break; | |
381 default: | |
382 pEInst->vibratoType = jvibSine; | |
383 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
384 "Invalid extinstrument vibrato type %d for inst #%d\n", ninst); | |
385 break; | |
386 } | |
387 pEInst->vibratoSweep = xmI2.vibratoSweep; | |
388 pEInst->vibratoDepth = xmI2.vibratoDepth; | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
389 pEInst->vibratoRate = xmI2.vibratoRate; |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
390 pEInst->fadeOut = xmI2.fadeOut; |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
391 pEInst->nsamples = xmI1.nsamples; |
0 | 392 |
393 // Initialize the SNumForNotes conversion table | |
394 for (i = 0; i < XM_MaxInstruments; i++) | |
395 xmConvTable[i] = jsetNotSet; | |
396 | |
397 // Read sample headers | |
398 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | |
399 { | |
400 XMSample xmS; | |
401 | |
402 // Read header data | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
403 if (!dmf_read_le32(inFile, &xmS.size) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
404 !dmf_read_le32(inFile, &xmS.loopS) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
405 !dmf_read_le32(inFile, &xmS.loopL) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
406 !dmf_read_byte(inFile, &xmS.volume) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
407 !dmf_read_byte(inFile, (Uint8 *) &xmS.fineTune) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
408 !dmf_read_byte(inFile, &xmS.type) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
409 !dmf_read_byte(inFile, &xmS.panning) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
410 !dmf_read_byte(inFile, (Uint8 *) &xmS.relNote) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
411 !dmf_read_byte(inFile, &xmS.ARESERVED) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
412 !dmf_read_str(inFile, &xmS.sampleName, sizeof(xmS.sampleName))) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
413 { |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
414 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
415 "Error reading instrument sample header #%d/%d [%d]", |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
416 ninst, nsample, module->ninstruments); |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
417 } |
0 | 418 |
419 if (xmS.size > 0) | |
420 { | |
421 // Allocate sample instrument | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
422 JSSDEBUG("Allocating sample #%d/%d [%d]\n", |
0 | 423 ninst, nsample, module->ninstruments); |
424 | |
425 xmConvTable[nsample] = module->ninstruments; | |
426 pInst = module->instruments[module->ninstruments] = jssAllocateInstrument(); | |
427 if (pInst == NULL) | |
428 { | |
429 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
430 "Could not allocate sample #%d/%d [%d]\n", |
0 | 431 ninst, nsample, module->ninstruments); |
432 } | |
433 module->ninstruments++; | |
434 } else | |
435 pInst = NULL; | |
436 | |
437 // Check and convert sample information | |
438 if (pInst != NULL) | |
439 { | |
440 // Copy values | |
441 if (xmS.volume > XM_MaxSampleVolume) | |
442 { | |
443 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
444 "Samp #%d/%d: volume > MAX\n", ninst, nsample); |
0 | 445 xmS.volume = XM_MaxSampleVolume; |
446 } | |
447 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
448 pInst->volume = xmS.volume; |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
449 pInst->ERelNote = xmS.relNote; |
0 | 450 pInst->EFineTune = xmS.fineTune; |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
451 pInst->EPanning = xmS.panning; |
0 | 452 #ifndef JSS_LIGHT |
453 pInst->desc = jssASCIItoStr(xmS.sampleName, 0, sizeof(xmS.sampleName)); | |
454 #endif | |
455 | |
456 // Convert flags | |
457 switch (xmS.type & 0x03) | |
458 { | |
459 case 0: pInst->flags = 0; break; | |
460 case 1: pInst->flags = jsfLooped; break; | |
461 case 2: pInst->flags = jsfLooped | jsfBiDi; break; | |
462 default: | |
463 pInst->flags = 0; | |
464 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
465 "Samp #%d/%d: Invalid sample type 0x%x\n", |
0 | 466 ninst, nsample, xmS.type); |
467 break; | |
468 } | |
469 | |
470 if (xmS.type & 0x10) | |
471 { | |
472 // 16-bit sample | |
473 JSFSET(pInst->flags, jsf16bit); | |
474 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
475 pInst->size = xmS.size / sizeof(Uint16); |
0 | 476 pInst->loopS = xmS.loopS / sizeof(Uint16); |
477 pInst->loopE = ((xmS.loopS + xmS.loopL) / sizeof(Uint16)); | |
478 } | |
479 else | |
480 { | |
481 // 8-bit sample | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
482 pInst->size = xmS.size; |
0 | 483 pInst->loopS = xmS.loopS; |
484 pInst->loopE = (xmS.loopS + xmS.loopL); | |
485 } | |
486 | |
487 if (xmS.loopL == 0) | |
488 { | |
489 // Always unset loop, if loop length is zero | |
490 JSFUNSET(pInst->flags, jsfLooped); | |
491 } | |
492 | |
493 if (pInst->flags & jsfLooped) | |
494 { | |
495 if (pInst->loopS >= pInst->size) | |
496 { | |
497 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
498 "Samp #%d/%d: loopS >= size (%d >= %d)\n", |
0 | 499 ninst, nsample, pInst->loopS, pInst->size); |
500 JSFUNSET(pInst->flags, jsfLooped); | |
501 } | |
502 | |
503 if (pInst->loopE > pInst->size) | |
504 { | |
505 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
506 "Samp #%d/%d: loopE > size (%d > %d)\n", |
0 | 507 ninst, nsample, pInst->loopE, pInst->size); |
508 JSFUNSET(pInst->flags, jsfLooped); | |
509 } | |
510 } | |
511 | |
512 | |
513 // Allocate memory for sample data | |
514 if (pInst->flags & jsf16bit) | |
515 pInst->data = dmCalloc(pInst->size, sizeof(Uint16)); | |
516 else | |
517 pInst->data = dmCalloc(pInst->size, sizeof(Uint8)); | |
518 | |
519 if (pInst->data == NULL) | |
520 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
521 "Could not allocate sample data #%d/%d.\n", ninst, nsample); |
0 | 522 } |
523 } | |
524 | |
525 // Read sample data | |
526 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | |
527 if (xmConvTable[nsample] != jsetNotSet) | |
528 { | |
529 pInst = module->instruments[xmConvTable[nsample]]; | |
530 if (pInst) | |
531 { | |
532 JSSDEBUG("desc....: '%s'\n" | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
533 "size....: %d\n" |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
534 "loopS...: %d\n" |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
535 "loopE...: %d\n" |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
536 "volume..: %d\n" |
0 | 537 "flags...: %x\n", |
538 pInst->desc, | |
539 pInst->size, pInst->loopS, pInst->loopE, | |
540 pInst->volume, pInst->flags); | |
541 | |
542 if (pInst->flags & jsf16bit) | |
543 { | |
544 // Read sampledata | |
545 if (dmfread(pInst->data, sizeof(Uint16), pInst->size, inFile) != (size_t) pInst->size) | |
546 JSSERROR(DMERR_FREAD, DMERR_FREAD, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
547 "Error reading sampledata for instrument #%d/%d, %d words.\n", |
0 | 548 ninst, nsample, pInst->size); |
549 | |
550 // Convert data | |
551 jssDecodeSample16((Uint16 *) pInst->data, pInst->size, | |
552 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
553 (jsampDelta | jsampSwapEndianess) | |
554 #else | |
555 (jsampDelta) | |
556 #endif | |
557 ); | |
558 } | |
559 else | |
560 { | |
561 // Read sampledata | |
562 if (dmfread(pInst->data, sizeof(Uint8), pInst->size, inFile) != (size_t) pInst->size) | |
563 JSSERROR(DMERR_FREAD, DMERR_FREAD, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
564 "Error reading sampledata for instrument #%d/%d, %d bytes.\n", |
0 | 565 ninst, nsample, pInst->size); |
566 | |
567 // Convert data | |
568 jssDecodeSample8((Uint8 *) pInst->data, pInst->size, | |
569 (jsampDelta | jsampFlipSign)); | |
570 } | |
571 } | |
572 } | |
573 | |
574 // Apply new values to sNumForNotes values | |
575 for (i = 0; i < XM_MaxNotes; i++) | |
576 { | |
577 tmp = xmI2.sNumForNotes[i]; | |
578 if (tmp >= 0 && tmp < xmI1.nsamples) | |
579 pEInst->sNumForNotes[i] = xmConvTable[tmp]; | |
580 else | |
581 { | |
582 pEInst->sNumForNotes[i] = jsetNotSet; | |
583 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
584 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n", | |
585 ninst, i, tmp); | |
586 } | |
587 } | |
588 } | |
589 else | |
590 { | |
591 // We may STILL need to skip extra data after 1st instr. header | |
592 remainder = xmI1.headSize - (dmftell(inFile) - pos); | |
593 if (remainder > 0) | |
594 { | |
98 | 595 JSSDEBUG("xmI1#2 Skipping: %li\n", remainder); |
0 | 596 dmfseek(inFile, remainder, SEEK_CUR); |
597 } | |
598 } | |
599 | |
600 return 0; | |
601 } | |
602 | |
603 | |
604 /* Load XM-format module from given file-stream | |
605 */ | |
606 int jssLoadXM(DMResource *inFile, JSSModule **ppModule) | |
607 { | |
608 JSSModule *module; | |
609 XMHeader xmH; | |
610 int result, index, tmp; | |
611 | |
612 assert(ppModule != NULL); | |
613 assert(inFile != NULL); | |
614 *ppModule = NULL; | |
615 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
616 // Try to get the XM-header |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
617 if (!dmf_read_str(inFile, &xmH.idMagic, sizeof(xmH.idMagic)) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
618 !dmf_read_str(inFile, &xmH.songName, sizeof(xmH.songName)) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
619 !dmf_read_byte(inFile, &xmH.unUsed1A) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
620 !dmf_read_str(inFile, &xmH.trackerName, sizeof(xmH.trackerName)) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
621 !dmf_read_le16(inFile, &xmH.version) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
622 !dmf_read_le32(inFile, &xmH.headSize) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
623 !dmf_read_le16(inFile, &xmH.norders) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
624 !dmf_read_le16(inFile, &xmH.defRestartPos) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
625 !dmf_read_le16(inFile, &xmH.nchannels) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
626 !dmf_read_le16(inFile, &xmH.npatterns) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
627 !dmf_read_le16(inFile, &xmH.ninstruments) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
628 !dmf_read_le16(inFile, &xmH.flags) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
629 !dmf_read_le16(inFile, &xmH.defSpeed) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
630 !dmf_read_le16(inFile, &xmH.defTempo)) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
631 return DMERR_FREAD; |
0 | 632 |
633 | |
634 // Check the fields, none of these are considered fatal | |
635 if (strncmp(xmH.idMagic, "Extended Module: ", 17) != 0) | |
636 { | |
637 JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, | |
638 "Not a FT2 Extended Module (XM), ident mismatch!\n"); | |
639 } | |
640 | |
641 if (xmH.version != 0x0104) | |
642 { | |
643 JSSWARNING(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, | |
644 "Unsupported version of XM format 0x%04x instead of expected 0x0104.\n", | |
645 xmH.version); | |
646 } | |
647 | |
648 if (xmH.unUsed1A != 0x1a) | |
649 { | |
650 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
651 "Possibly modified or corrupted XM [%x]\n", xmH.unUsed1A); | |
652 } | |
653 | |
654 if (xmH.norders > XM_MaxOrders) | |
655 { | |
656 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
657 "Number of orders %d > %d, possibly broken module.\n", |
0 | 658 xmH.norders, XM_MaxOrders); |
659 xmH.norders = XM_MaxOrders; | |
660 } | |
661 | |
662 if (xmH.norders == 0) | |
663 { | |
664 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
665 "Number of orders was zero.\n"); | |
666 } | |
667 | |
668 if (xmH.npatterns > XM_MaxPatterns) | |
669 { | |
670 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
671 "Number of patterns %d > %d, possibly broken module.\n", |
0 | 672 xmH.npatterns, XM_MaxPatterns); |
673 xmH.npatterns = XM_MaxPatterns; | |
674 } | |
675 | |
676 if (xmH.npatterns == 0) | |
677 { | |
678 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
679 "Number of patterns was zero.\n"); | |
680 } | |
681 | |
682 if (xmH.nchannels <= 0 || xmH.nchannels > XM_MaxChannels) | |
683 { | |
684 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
685 "Number of channels was invalid, %d (should be 1 - %d).\n", |
0 | 686 xmH.nchannels, XM_MaxChannels); |
687 } | |
688 | |
689 if (xmH.ninstruments <= 0 || xmH.ninstruments > XM_MaxInstruments) | |
690 { | |
691 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
692 "Number of instruments was invalid, %d (should be 1 - %d).\n", |
0 | 693 xmH.ninstruments, XM_MaxInstruments); |
694 } | |
695 | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
696 // Read orders list |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
697 if (!dmf_read_str(inFile, &xmH.orderList, sizeof(xmH.orderList))) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
698 { |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
699 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
700 "Error reading pattern order list.\n"); |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
701 } |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
702 |
0 | 703 /* Okay, allocate a module structure |
704 */ | |
705 module = jssAllocateModule(); | |
706 if (module == NULL) | |
707 { | |
708 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
709 "Could not allocate memory for module structure.\n"); | |
710 } | |
711 *ppModule = module; | |
712 | |
713 | |
714 // Convert and check the header data | |
715 module->moduleType = jmdtXM; | |
716 module->intVersion = xmH.version; | |
717 #ifndef JSS_LIGHT | |
718 module->moduleName = jssASCIItoStr(xmH.songName, 0, sizeof(xmH.songName)); | |
719 module->trackerName = jssASCIItoStr(xmH.trackerName, 0, sizeof(xmH.trackerName)); | |
720 #endif | |
721 module->defSpeed = xmH.defSpeed; | |
722 module->defTempo = xmH.defTempo; | |
723 module->nextInstruments = xmH.ninstruments; | |
724 module->ninstruments = 0; | |
725 module->npatterns = xmH.npatterns; | |
726 module->norders = xmH.norders; | |
727 module->nchannels = xmH.nchannels; | |
728 module->defFlags = jmdfStereo | jmdfFT2Replay; | |
729 module->defRestartPos = xmH.defRestartPos; | |
730 | |
731 if ((xmH.flags & 1) == 0) | |
732 module->defFlags |= jmdfAmigaPeriods; | |
733 | |
734 // Setup the default pannings | |
735 for (index = 0; index < jsetNChannels; index++) | |
736 module->defPanning[index] = jchPanMiddle; | |
737 | |
738 /* Read patterns | |
739 */ | |
740 for (index = 0; index < module->npatterns; index++) | |
741 { | |
88 | 742 off_t pos, remainder; |
0 | 743 XMPattern xmP; |
744 | |
745 // Get the pattern header | |
98 | 746 pos = dmftell(inFile); |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
747 |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
748 if (!dmf_read_le32(inFile, &xmP.headSize) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
749 !dmf_read_byte(inFile, &xmP.packing) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
750 !dmf_read_le16(inFile, &xmP.nrows) || |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
751 !dmf_read_le16(inFile, &xmP.size)) |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
752 JSSERROR(DMERR_FREAD, DMERR_FREAD, |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
753 "Could not read pattern header data #%d.\n", |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
754 index); |
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
755 |
0 | 756 |
757 // Check the header | |
758 if (xmP.packing != 0) | |
759 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
760 "Pattern #%d packing type unsupported (%d)\n", |
0 | 761 index, xmP.packing); |
762 | |
763 if (xmP.nrows == 0) | |
764 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
765 "Pattern #%d has %d rows, invalid data.\n", |
0 | 766 index, xmP.nrows); |
767 | |
768 if (xmP.size > 0) | |
769 { | |
770 // Allocate and unpack pattern | |
771 module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels); | |
772 if (module->patterns[index] == NULL) | |
773 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
774 "Could not allocate memory for pattern #%d\n", index); |
0 | 775 |
776 result = jssXMUnpackPattern(inFile, xmP.size, module->patterns[index]); | |
777 if (result != 0) | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
778 JSSERROR(result, result, "Error in unpacking pattern #%d data\n", index); |
0 | 779 } |
780 | |
781 // Skip extra data (if the file is damaged) | |
88 | 782 remainder = xmP.headSize - (dmftell(inFile) - pos); |
783 if (remainder > 0) | |
0 | 784 { |
98 | 785 JSSDEBUG("xmP Skipping: %li\n", remainder); |
88 | 786 dmfseek(inFile, remainder, SEEK_CUR); |
0 | 787 } |
788 } | |
789 | |
790 // Allocate the empty pattern | |
791 module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels); | |
792 | |
793 /* Convert song orders list by replacing nonexisting patterns | |
794 * with pattern number jsetMaxPatterns. | |
795 */ | |
796 for (index = 0; index < module->norders; index++) | |
797 { | |
798 tmp = xmH.orderList[index]; | |
799 if (tmp >= module->npatterns || module->patterns[tmp] == NULL) | |
800 tmp = jsetMaxPatterns; | |
801 | |
802 module->orderList[index] = tmp; | |
803 } | |
804 | |
805 /* Read instruments | |
806 */ | |
807 for (index = 0; index < module->nextInstruments; index++) | |
808 { | |
809 result = jssXMLoadExtInstrument(inFile, index, module); | |
810 if (result != 0) | |
778
a4cf7716ba58
Clean up the XM module loader a bit, add more error checking.
Matti Hamalainen <ccr@tnsp.org>
parents:
658
diff
changeset
|
811 JSSERROR(result, result, "Errors while reading instrument #%d\n", index); |
0 | 812 } |
813 | |
814 return DMERR_OK; | |
815 } |