Mercurial > hg > dmlib
annotate minijss/jloadxm.c @ 658:c430112449a7
Move miniJSS into a subdirectory.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 16 Apr 2013 07:32:29 +0300 |
parents | jloadxm.c@d4b84101e480 |
children | a4cf7716ba58 |
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; | |
88 int fineTune; | |
89 Uint8 type, panning; | |
90 int relNote; | |
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 */ | |
107 #define JSGETBYTE(XV) do { \ | |
108 size--; \ | |
109 if (size < 0) \ | |
110 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ | |
111 "Unexpected end of packed pattern data.\n"); \ | |
112 XV = dmfgetc(inFile); \ | |
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, | |
206 "Unparsed data after pattern (%i bytes), possibly broken file.\n", size); | |
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, | |
248 "Inst#%i/%s-env: nPoints > MAX, possibly broken file.\n", instr, e); | |
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, | |
255 "Inst#%i/%s-env: iSustain > nPoints (%i > %i), possibly broken file.\n", | |
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, | |
263 "Inst#%i/%s-env: loopE > nPoints (%i > %i), possibly broken file.\n", | |
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, | |
271 "Inst#%i/%s-env: loopS > loopE (%i > %i), possibly broken file.\n", | |
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); | |
290 dmf_read_le32(inFile, &xmI1.headSize); | |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
291 dmf_read_str(inFile, &xmI1.instName, sizeof(xmI1.instName)); |
0 | 292 xmI1.instType = dmfgetc(inFile); |
293 dmf_read_le16(inFile, &xmI1.nsamples); | |
294 | |
295 // If there are samples, there is header #2 | |
296 if (xmI1.nsamples > 0) | |
297 { | |
298 int i, nsample, tmp; | |
299 int xmConvTable[XM_MaxInstruments + 1]; | |
300 JSSExtInstrument *pEInst; | |
301 JSSInstrument *pInst; | |
302 XMInstrument2 xmI2; | |
303 | |
304 // Allocate instrument | |
305 if ((pEInst = jssAllocateExtInstrument()) == NULL) | |
306 { | |
307 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
308 "Could not allocate extended instrument structure #%i\n", ninst); | |
309 } | |
310 | |
311 module->extInstruments[ninst] = pEInst; | |
312 | |
313 // Get instrument header #2 | |
314 dmf_read_le32(inFile, &xmI2.headSize); | |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
315 dmf_read_str(inFile, &xmI2.sNumForNotes, sizeof(xmI2.sNumForNotes)); |
0 | 316 |
317 for (i = 0; i < XM_MaxEnvPoints; i++) | |
318 { | |
319 dmf_read_le16(inFile, &xmI2.volumeEnv.points[i].frame); | |
320 dmf_read_le16(inFile, &xmI2.volumeEnv.points[i].value); | |
321 } | |
322 | |
323 for (i = 0; i < XM_MaxEnvPoints; i++) | |
324 { | |
325 dmf_read_le16(inFile, &xmI2.panningEnv.points[i].frame); | |
326 dmf_read_le16(inFile, &xmI2.panningEnv.points[i].value); | |
327 } | |
328 | |
329 xmI2.volumeEnv.npoints = dmfgetc(inFile); | |
330 xmI2.panningEnv.npoints = dmfgetc(inFile); | |
331 | |
332 xmI2.volumeEnv.sustain = dmfgetc(inFile); | |
333 xmI2.volumeEnv.loopS = dmfgetc(inFile); | |
334 xmI2.volumeEnv.loopE = dmfgetc(inFile); | |
335 | |
336 xmI2.panningEnv.sustain = dmfgetc(inFile); | |
337 xmI2.panningEnv.loopS = dmfgetc(inFile); | |
338 xmI2.panningEnv.loopE = dmfgetc(inFile); | |
339 | |
340 xmI2.volumeEnv.flags = dmfgetc(inFile); | |
341 xmI2.panningEnv.flags = dmfgetc(inFile); | |
342 | |
343 xmI2.vibratoType = dmfgetc(inFile); | |
344 xmI2.vibratoSweep = dmfgetc(inFile); | |
345 xmI2.vibratoDepth = dmfgetc(inFile); | |
346 xmI2.vibratoRate = dmfgetc(inFile); | |
347 | |
348 dmf_read_le16(inFile, &xmI2.fadeOut); | |
349 dmf_read_le16(inFile, &xmI2.ARESERVED); | |
350 | |
351 // Skip the extra data after header #2 | |
352 remainder = xmI1.headSize - (dmftell(inFile) - pos); | |
353 if (remainder > 0) | |
354 { | |
98 | 355 JSSDEBUG("xmI1#1 Skipping: %li\n", remainder); |
0 | 356 dmfseek(inFile, remainder, SEEK_CUR); |
357 } | |
358 | |
359 // Check and convert all ext instrument information | |
360 #ifndef JSS_LIGHT | |
361 pEInst->desc = jssASCIItoStr(xmI1.instName, 0, sizeof(xmI1.instName)); | |
362 #endif | |
363 jssXMConvertEnvelope(&pEInst->volumeEnv, &xmI2.volumeEnv, "vol", ninst); | |
364 jssXMConvertEnvelope(&pEInst->panningEnv, &xmI2.panningEnv, "pan", ninst); | |
365 | |
366 switch (xmI2.vibratoType) | |
367 { | |
368 case 0: pEInst->vibratoType = jvibSine; break; | |
369 case 1: pEInst->vibratoType = jvibRamp; break; | |
370 case 2: pEInst->vibratoType = jvibSquare; break; | |
371 case 3: pEInst->vibratoType = jvibRandom; break; | |
372 default: | |
373 pEInst->vibratoType = jvibSine; | |
374 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
375 "Invalid extinstrument vibrato type %d for inst #%d\n", ninst); | |
376 break; | |
377 } | |
378 pEInst->vibratoSweep = xmI2.vibratoSweep; | |
379 pEInst->vibratoDepth = xmI2.vibratoDepth; | |
380 pEInst->vibratoRate = xmI2.vibratoRate; | |
381 pEInst->fadeOut = xmI2.fadeOut; | |
382 pEInst->nsamples = xmI1.nsamples; | |
383 | |
384 // Initialize the SNumForNotes conversion table | |
385 for (i = 0; i < XM_MaxInstruments; i++) | |
386 xmConvTable[i] = jsetNotSet; | |
387 | |
388 // Read sample headers | |
389 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | |
390 { | |
391 XMSample xmS; | |
392 | |
393 // Read header data | |
394 dmf_read_le32(inFile, &xmS.size); | |
395 dmf_read_le32(inFile, &xmS.loopS); | |
396 dmf_read_le32(inFile, &xmS.loopL); | |
397 xmS.volume = dmfgetc(inFile); | |
398 xmS.fineTune = (signed char) dmfgetc(inFile); | |
399 xmS.type = dmfgetc(inFile); | |
400 xmS.panning = dmfgetc(inFile); | |
401 xmS.relNote = (signed char) dmfgetc(inFile); | |
402 xmS.ARESERVED = dmfgetc(inFile); | |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
403 dmf_read_str(inFile, &xmS.sampleName, sizeof(xmS.sampleName)); |
0 | 404 |
405 if (xmS.size > 0) | |
406 { | |
407 // Allocate sample instrument | |
408 JSSDEBUG("Allocating sample #%i/%i [%i]\n", | |
409 ninst, nsample, module->ninstruments); | |
410 | |
411 xmConvTable[nsample] = module->ninstruments; | |
412 pInst = module->instruments[module->ninstruments] = jssAllocateInstrument(); | |
413 if (pInst == NULL) | |
414 { | |
415 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
416 "Could not allocate sample #%i/%i [%i]\n", | |
417 ninst, nsample, module->ninstruments); | |
418 } | |
419 module->ninstruments++; | |
420 } else | |
421 pInst = NULL; | |
422 | |
423 // Check and convert sample information | |
424 if (pInst != NULL) | |
425 { | |
426 // Copy values | |
427 if (xmS.volume > XM_MaxSampleVolume) | |
428 { | |
429 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
430 "Samp #%i/%i: volume > MAX\n", ninst, nsample); | |
431 xmS.volume = XM_MaxSampleVolume; | |
432 } | |
433 | |
434 pInst->volume = xmS.volume; | |
435 pInst->ERelNote = xmS.relNote; | |
436 pInst->EFineTune = xmS.fineTune; | |
437 pInst->EPanning = xmS.panning; | |
438 #ifndef JSS_LIGHT | |
439 pInst->desc = jssASCIItoStr(xmS.sampleName, 0, sizeof(xmS.sampleName)); | |
440 #endif | |
441 | |
442 // Convert flags | |
443 switch (xmS.type & 0x03) | |
444 { | |
445 case 0: pInst->flags = 0; break; | |
446 case 1: pInst->flags = jsfLooped; break; | |
447 case 2: pInst->flags = jsfLooped | jsfBiDi; break; | |
448 default: | |
449 pInst->flags = 0; | |
450 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
451 "Samp #%i/%i: Invalid sample type 0x%x\n", | |
452 ninst, nsample, xmS.type); | |
453 break; | |
454 } | |
455 | |
456 if (xmS.type & 0x10) | |
457 { | |
458 // 16-bit sample | |
459 JSFSET(pInst->flags, jsf16bit); | |
460 | |
461 pInst->size = xmS.size / sizeof(Uint16); | |
462 pInst->loopS = xmS.loopS / sizeof(Uint16); | |
463 pInst->loopE = ((xmS.loopS + xmS.loopL) / sizeof(Uint16)); | |
464 } | |
465 else | |
466 { | |
467 // 8-bit sample | |
468 pInst->size = xmS.size; | |
469 pInst->loopS = xmS.loopS; | |
470 pInst->loopE = (xmS.loopS + xmS.loopL); | |
471 } | |
472 | |
473 if (xmS.loopL == 0) | |
474 { | |
475 // Always unset loop, if loop length is zero | |
476 JSFUNSET(pInst->flags, jsfLooped); | |
477 } | |
478 | |
479 if (pInst->flags & jsfLooped) | |
480 { | |
481 if (pInst->loopS >= pInst->size) | |
482 { | |
483 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
484 "Samp #%i/%i: loopS >= size (%d >= %d)\n", | |
485 ninst, nsample, pInst->loopS, pInst->size); | |
486 JSFUNSET(pInst->flags, jsfLooped); | |
487 } | |
488 | |
489 if (pInst->loopE > pInst->size) | |
490 { | |
491 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
492 "Samp #%i/%i: loopE > size (%d > %d)\n", | |
493 ninst, nsample, pInst->loopE, pInst->size); | |
494 JSFUNSET(pInst->flags, jsfLooped); | |
495 } | |
496 } | |
497 | |
498 | |
499 // Allocate memory for sample data | |
500 if (pInst->flags & jsf16bit) | |
501 pInst->data = dmCalloc(pInst->size, sizeof(Uint16)); | |
502 else | |
503 pInst->data = dmCalloc(pInst->size, sizeof(Uint8)); | |
504 | |
505 if (pInst->data == NULL) | |
506 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
507 "Could not allocate sample data #%i/%i.\n", ninst, nsample); | |
508 } | |
509 } | |
510 | |
511 // Read sample data | |
512 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | |
513 if (xmConvTable[nsample] != jsetNotSet) | |
514 { | |
515 pInst = module->instruments[xmConvTable[nsample]]; | |
516 if (pInst) | |
517 { | |
518 JSSDEBUG("desc....: '%s'\n" | |
519 "size....: %i\n" | |
520 "loopS...: %i\n" | |
521 "loopE...: %i\n" | |
522 "volume..: %i\n" | |
523 "flags...: %x\n", | |
524 pInst->desc, | |
525 pInst->size, pInst->loopS, pInst->loopE, | |
526 pInst->volume, pInst->flags); | |
527 | |
528 if (pInst->flags & jsf16bit) | |
529 { | |
530 // Read sampledata | |
531 if (dmfread(pInst->data, sizeof(Uint16), pInst->size, inFile) != (size_t) pInst->size) | |
532 JSSERROR(DMERR_FREAD, DMERR_FREAD, | |
533 "Error reading sampledata for instrument #%i/%i, %i words.\n", | |
534 ninst, nsample, pInst->size); | |
535 | |
536 // Convert data | |
537 jssDecodeSample16((Uint16 *) pInst->data, pInst->size, | |
538 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
539 (jsampDelta | jsampSwapEndianess) | |
540 #else | |
541 (jsampDelta) | |
542 #endif | |
543 ); | |
544 } | |
545 else | |
546 { | |
547 // Read sampledata | |
548 if (dmfread(pInst->data, sizeof(Uint8), pInst->size, inFile) != (size_t) pInst->size) | |
549 JSSERROR(DMERR_FREAD, DMERR_FREAD, | |
550 "Error reading sampledata for instrument #%i/%i, %i bytes.\n", | |
551 ninst, nsample, pInst->size); | |
552 | |
553 // Convert data | |
554 jssDecodeSample8((Uint8 *) pInst->data, pInst->size, | |
555 (jsampDelta | jsampFlipSign)); | |
556 } | |
557 } | |
558 } | |
559 | |
560 // Apply new values to sNumForNotes values | |
561 for (i = 0; i < XM_MaxNotes; i++) | |
562 { | |
563 tmp = xmI2.sNumForNotes[i]; | |
564 if (tmp >= 0 && tmp < xmI1.nsamples) | |
565 pEInst->sNumForNotes[i] = xmConvTable[tmp]; | |
566 else | |
567 { | |
568 pEInst->sNumForNotes[i] = jsetNotSet; | |
569 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
570 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n", | |
571 ninst, i, tmp); | |
572 } | |
573 } | |
574 } | |
575 else | |
576 { | |
577 // We may STILL need to skip extra data after 1st instr. header | |
578 remainder = xmI1.headSize - (dmftell(inFile) - pos); | |
579 if (remainder > 0) | |
580 { | |
98 | 581 JSSDEBUG("xmI1#2 Skipping: %li\n", remainder); |
0 | 582 dmfseek(inFile, remainder, SEEK_CUR); |
583 } | |
584 } | |
585 | |
586 return 0; | |
587 } | |
588 | |
589 | |
590 /* Load XM-format module from given file-stream | |
591 */ | |
592 int jssLoadXM(DMResource *inFile, JSSModule **ppModule) | |
593 { | |
594 JSSModule *module; | |
595 XMHeader xmH; | |
596 int result, index, tmp; | |
597 | |
598 assert(ppModule != NULL); | |
599 assert(inFile != NULL); | |
600 *ppModule = NULL; | |
601 | |
602 /* Get XM-header and check it | |
603 */ | |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
604 dmf_read_str(inFile, &xmH.idMagic, sizeof(xmH.idMagic)); |
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
605 dmf_read_str(inFile, &xmH.songName, sizeof(xmH.songName)); |
0 | 606 xmH.unUsed1A = dmfgetc(inFile); |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
607 dmf_read_str(inFile, &xmH.trackerName, sizeof(xmH.trackerName)); |
0 | 608 dmf_read_le16(inFile, &xmH.version); |
609 dmf_read_le32(inFile, &xmH.headSize); | |
610 dmf_read_le16(inFile, &xmH.norders); | |
611 dmf_read_le16(inFile, &xmH.defRestartPos); | |
612 dmf_read_le16(inFile, &xmH.nchannels); | |
613 dmf_read_le16(inFile, &xmH.npatterns); | |
614 dmf_read_le16(inFile, &xmH.ninstruments); | |
615 dmf_read_le16(inFile, &xmH.flags); | |
616 dmf_read_le16(inFile, &xmH.defSpeed); | |
617 dmf_read_le16(inFile, &xmH.defTempo); | |
554
d4b84101e480
Remove useless (Uint8 *) typecasts now that dmf_{read,write}_str() uses void.
Matti Hamalainen <ccr@tnsp.org>
parents:
552
diff
changeset
|
618 dmf_read_str(inFile, &xmH.orderList, sizeof(xmH.orderList)); |
0 | 619 |
620 | |
621 // Check the fields, none of these are considered fatal | |
622 if (strncmp(xmH.idMagic, "Extended Module: ", 17) != 0) | |
623 { | |
624 JSSERROR(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, | |
625 "Not a FT2 Extended Module (XM), ident mismatch!\n"); | |
626 } | |
627 | |
628 if (xmH.version != 0x0104) | |
629 { | |
630 JSSWARNING(DMERR_NOT_SUPPORTED, DMERR_NOT_SUPPORTED, | |
631 "Unsupported version of XM format 0x%04x instead of expected 0x0104.\n", | |
632 xmH.version); | |
633 } | |
634 | |
635 if (xmH.unUsed1A != 0x1a) | |
636 { | |
637 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
638 "Possibly modified or corrupted XM [%x]\n", xmH.unUsed1A); | |
639 } | |
640 | |
641 if (xmH.norders > XM_MaxOrders) | |
642 { | |
643 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
644 "Number of orders %i > %i, possibly broken module.\n", | |
645 xmH.norders, XM_MaxOrders); | |
646 xmH.norders = XM_MaxOrders; | |
647 } | |
648 | |
649 if (xmH.norders == 0) | |
650 { | |
651 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
652 "Number of orders was zero.\n"); | |
653 } | |
654 | |
655 if (xmH.npatterns > XM_MaxPatterns) | |
656 { | |
657 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
658 "Number of patterns %i > %i, possibly broken module.\n", | |
659 xmH.npatterns, XM_MaxPatterns); | |
660 xmH.npatterns = XM_MaxPatterns; | |
661 } | |
662 | |
663 if (xmH.npatterns == 0) | |
664 { | |
665 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
666 "Number of patterns was zero.\n"); | |
667 } | |
668 | |
669 if (xmH.nchannels <= 0 || xmH.nchannels > XM_MaxChannels) | |
670 { | |
671 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
672 "Number of channels was invalid, %i (should be 1 - %i).\n", | |
673 xmH.nchannels, XM_MaxChannels); | |
674 } | |
675 | |
676 if (xmH.ninstruments <= 0 || xmH.ninstruments > XM_MaxInstruments) | |
677 { | |
678 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
679 "Number of instruments was invalid, %i (should be 1 - %i).\n", | |
680 xmH.ninstruments, XM_MaxInstruments); | |
681 } | |
682 | |
683 /* Okay, allocate a module structure | |
684 */ | |
685 module = jssAllocateModule(); | |
686 if (module == NULL) | |
687 { | |
688 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
689 "Could not allocate memory for module structure.\n"); | |
690 } | |
691 *ppModule = module; | |
692 | |
693 | |
694 // Convert and check the header data | |
695 module->moduleType = jmdtXM; | |
696 module->intVersion = xmH.version; | |
697 #ifndef JSS_LIGHT | |
698 module->moduleName = jssASCIItoStr(xmH.songName, 0, sizeof(xmH.songName)); | |
699 module->trackerName = jssASCIItoStr(xmH.trackerName, 0, sizeof(xmH.trackerName)); | |
700 #endif | |
701 module->defSpeed = xmH.defSpeed; | |
702 module->defTempo = xmH.defTempo; | |
703 module->nextInstruments = xmH.ninstruments; | |
704 module->ninstruments = 0; | |
705 module->npatterns = xmH.npatterns; | |
706 module->norders = xmH.norders; | |
707 module->nchannels = xmH.nchannels; | |
708 module->defFlags = jmdfStereo | jmdfFT2Replay; | |
709 module->defRestartPos = xmH.defRestartPos; | |
710 | |
711 if ((xmH.flags & 1) == 0) | |
712 module->defFlags |= jmdfAmigaPeriods; | |
713 | |
714 // Setup the default pannings | |
715 for (index = 0; index < jsetNChannels; index++) | |
716 module->defPanning[index] = jchPanMiddle; | |
717 | |
718 /* Read patterns | |
719 */ | |
720 for (index = 0; index < module->npatterns; index++) | |
721 { | |
88 | 722 off_t pos, remainder; |
0 | 723 XMPattern xmP; |
724 | |
725 // Get the pattern header | |
98 | 726 pos = dmftell(inFile); |
0 | 727 dmf_read_le32(inFile, &xmP.headSize); |
728 xmP.packing = dmfgetc(inFile); | |
729 dmf_read_le16(inFile, &xmP.nrows); | |
730 dmf_read_le16(inFile, &xmP.size); | |
731 | |
732 // Check the header | |
733 if (xmP.packing != 0) | |
734 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
735 "Pattern #%i packing type unsupported (%i)\n", | |
736 index, xmP.packing); | |
737 | |
738 if (xmP.nrows == 0) | |
739 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
740 "Pattern #%i has %i rows, invalid data.\n", | |
741 index, xmP.nrows); | |
742 | |
743 if (xmP.size > 0) | |
744 { | |
745 // Allocate and unpack pattern | |
746 module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels); | |
747 if (module->patterns[index] == NULL) | |
748 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
749 "Could not allocate memory for pattern #%i\n", index); | |
750 | |
751 result = jssXMUnpackPattern(inFile, xmP.size, module->patterns[index]); | |
752 if (result != 0) | |
753 JSSERROR(result, result, "Error in unpacking pattern #%i data\n", index); | |
754 } | |
755 | |
756 // Skip extra data (if the file is damaged) | |
88 | 757 remainder = xmP.headSize - (dmftell(inFile) - pos); |
758 if (remainder > 0) | |
0 | 759 { |
98 | 760 JSSDEBUG("xmP Skipping: %li\n", remainder); |
88 | 761 dmfseek(inFile, remainder, SEEK_CUR); |
0 | 762 } |
763 } | |
764 | |
765 // Allocate the empty pattern | |
766 module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels); | |
767 | |
768 /* Convert song orders list by replacing nonexisting patterns | |
769 * with pattern number jsetMaxPatterns. | |
770 */ | |
771 for (index = 0; index < module->norders; index++) | |
772 { | |
773 tmp = xmH.orderList[index]; | |
774 if (tmp >= module->npatterns || module->patterns[tmp] == NULL) | |
775 tmp = jsetMaxPatterns; | |
776 | |
777 module->orderList[index] = tmp; | |
778 } | |
779 | |
780 /* Read instruments | |
781 */ | |
782 for (index = 0; index < module->nextInstruments; index++) | |
783 { | |
784 result = jssXMLoadExtInstrument(inFile, index, module); | |
785 if (result != 0) | |
786 JSSERROR(result, result, "Errors while reading instrument #%i\n", index); | |
787 } | |
788 | |
789 return DMERR_OK; | |
790 } |