Mercurial > hg > dmlib
annotate tools/xm2jss.c @ 1201:83693e2797dd
Comments.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Mar 2015 16:51:34 +0200 |
parents | 4d27c4ec8d4d |
children | 59b5821b5cf9 |
rev | line source |
---|---|
0 | 1 /* |
2 * xm2jss - Convert XM module to JSSMOD | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2006-2009 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include <stdio.h> | |
9 #include <errno.h> | |
10 #include "jss.h" | |
11 #include "jssmod.h" | |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
12 #include "jssplr.h" |
0 | 13 #include "dmlib.h" |
14 #include "dmargs.h" | |
15 #include "dmres.h" | |
285
245b15cd1919
Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
16 #include "dmmutex.h" |
245b15cd1919
Don't link libSDL uselessly to utilities that do not actually use it.
Matti Hamalainen <ccr@tnsp.org>
parents:
202
diff
changeset
|
17 |
0 | 18 |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
19 #define jmpNMODEffectTable (36) |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
20 static const char jmpMODEffectTable[jmpNMODEffectTable] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
1099 | 21 #define JM_SAMPLE_MODE_MASK (jsampFlipSign | jsampSwapEndianess | jsampSplit | jsampDelta) |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
22 |
0 | 23 |
300 | 24 char *optInFilename = NULL, *optOutFilename = NULL; |
0 | 25 BOOL optIgnoreErrors = FALSE, |
26 optStripExtInstr = FALSE, | |
27 optStripInstr = FALSE, | |
28 optStripSamples = FALSE, | |
29 optOptimize = FALSE; | |
30 | |
31 int optPatternMode = PATMODE_COMP_HORIZ, | |
32 optSampMode16 = jsampDelta, | |
33 optSampMode8 = jsampFlipSign | jsampDelta; | |
34 | |
35 | |
36 static const char* patModeTable[PATMODE_LAST] = | |
37 { | |
38 "Raw horizontal", | |
39 "Compressed horizontal (similar to XM modules)", | |
40 "Raw vertical", | |
41 "Compressed vertical", | |
42 "Raw vertical for each element", | |
43 }; | |
44 | |
45 | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
856
diff
changeset
|
46 static const DMOptArg optList[] = |
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
856
diff
changeset
|
47 { |
0 | 48 { 0, '?', "help", "Show this help", OPT_NONE }, |
337 | 49 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, |
50 { 2, 'i', "ignore", "Ignore errors", OPT_NONE }, | |
51 { 3, 'p', "patterns", "Pattern storage mode", OPT_ARGREQ }, | |
52 { 4, 'E', "strip-ext-instr","Strip ext. instruments (implies -I -S)", OPT_NONE }, | |
53 { 5, 'I', "strip-instr", "Strip instruments (implies -S)", OPT_NONE }, | |
54 { 6, 'S', "strip-samples", "Strip instr. sampledata", OPT_NONE }, | |
0 | 55 { 7, '8', "smode8", "8-bit sample conversion flags", OPT_ARGREQ }, |
56 { 8, '1', "smode16", "16-bit sample conversion flags", OPT_ARGREQ }, | |
337 | 57 { 9, 'O', "optimize", "Optimize module", OPT_NONE }, |
0 | 58 }; |
59 | |
60 const int optListN = sizeof(optList) / sizeof(optList[0]); | |
61 | |
62 | |
63 void argShowHelp() | |
64 { | |
65 int i; | |
66 | |
337 | 67 dmPrintBanner(stdout, dmProgName, "[options] <input.xm> <output.jmod>"); |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
856
diff
changeset
|
68 dmArgsPrintHelp(stdout, optList, optListN, 0); |
0 | 69 |
70 printf("\n" | |
71 "Pattern storage modes:\n"); | |
72 | |
73 for (i = 1; i < PATMODE_LAST; i++) | |
74 printf(" %d = %s\n", i, patModeTable[i-1]); | |
75 | |
76 printf( | |
77 "\n" | |
78 "Sample data conversion flags (summative):\n" | |
79 " 1 = Delta encoding (DEF 8 & 16)\n" | |
80 " 2 = Flip signedness (DEF 8)\n" | |
81 " 4 = Swap endianess (affects 16-bit only)\n" | |
82 " 8 = Split and de-interleave hi/lo bytes (affects 16-bit only)\n" | |
83 "\n" | |
84 ); | |
85 } | |
86 | |
87 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
88 { | |
89 (void) optArg; | |
90 | |
91 switch (optN) | |
92 { | |
93 case 0: | |
94 argShowHelp(); | |
95 exit(0); | |
96 break; | |
97 | |
98 case 1: | |
337 | 99 dmVerbosity++; |
100 break; | |
101 | |
102 case 2: | |
0 | 103 optIgnoreErrors = TRUE; |
104 break; | |
105 | |
337 | 106 case 3: |
0 | 107 optPatternMode = atoi(optArg); |
108 if (optPatternMode <= 0 || optPatternMode >= PATMODE_LAST) | |
109 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
110 dmErrorMsg("Unknown pattern conversion mode %d\n", optPatternMode); |
0 | 111 return FALSE; |
112 } | |
113 break; | |
114 | |
337 | 115 case 4: optStripExtInstr = TRUE; break; |
116 case 5: optStripInstr = TRUE; break; | |
117 case 6: optStripSamples = TRUE; break; | |
118 | |
1099 | 119 case 7: optSampMode8 = atoi(optArg) & JM_SAMPLE_MODE_MASK; break; |
120 case 8: optSampMode16 = atoi(optArg) & JM_SAMPLE_MODE_MASK; break; | |
0 | 121 |
337 | 122 case 9: optOptimize = TRUE; break; |
0 | 123 |
124 default: | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
125 dmErrorMsg("Unknown argument '%s'.\n", currArg); |
0 | 126 return FALSE; |
127 } | |
128 | |
129 return TRUE; | |
130 } | |
131 | |
132 | |
133 BOOL argHandleFile(char *currArg) | |
134 { | |
135 // Was not option argument | |
300 | 136 if (!optInFilename) |
137 optInFilename = currArg; | |
337 | 138 else |
300 | 139 if (!optOutFilename) |
140 optOutFilename = currArg; | |
0 | 141 else |
142 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
143 dmErrorMsg("Too many filename arguments specified, '%s'.\n", currArg); |
0 | 144 return FALSE; |
145 } | |
146 | |
147 return TRUE; | |
148 } | |
149 | |
150 | |
151 /* These functions and the macro mess are meant to make the | |
152 * conversion routines themselves clearer and simpler. | |
153 */ | |
154 BOOL jsPutByte(Uint8 *patBuf, size_t patBufSize, size_t *npatBuf, Uint8 val) | |
155 { | |
156 if (*npatBuf >= patBufSize) | |
157 return FALSE; | |
158 else | |
159 { | |
160 patBuf[*npatBuf] = val; | |
161 (*npatBuf)++; | |
162 return TRUE; | |
163 } | |
164 } | |
165 | |
166 #define JSPUTBYTE(x) do { if (!jsPutByte(patBuf, patBufSize, patSize, x)) return DMERR_BOUNDS; } while (0) | |
167 | |
168 #define JSCOMP(x,z) do { if ((x) != jsetNotSet) { qflags |= (z); qcomp++; } } while (0) | |
169 | |
170 #define JSCOMPPUT(xf,xv,qv) do { \ | |
171 if (qflags & (xf)) { \ | |
172 if ((xv) < 0 || (xv) > 255) \ | |
173 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, \ | |
174 "%s value out of bounds %d.\n", qv, (xv)); \ | |
175 JSPUTBYTE(xv); \ | |
176 } \ | |
177 } while (0) | |
178 | |
179 #define JSCONVPUT(xv,qv) do { \ | |
180 if ((xv) != jsetNotSet) { \ | |
181 if ((xv) < 0 || (xv) > 254) \ | |
182 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, \ | |
183 "%s value out of bounds %d.\n", qv, (xv)); \ | |
184 JSPUTBYTE((xv) + 1); \ | |
185 } else { \ | |
186 JSPUTBYTE(0); \ | |
187 } \ | |
188 } while (0) | |
189 | |
190 | |
191 /* Convert a note | |
192 */ | |
193 static int jssConvertNote(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSNote *note) | |
194 { | |
195 Uint8 tmp; | |
196 if (note->note == jsetNotSet) | |
197 tmp = 0; | |
1194 | 198 else |
199 if (note->note == jsetNoteOff) | |
0 | 200 tmp = 127; |
201 else | |
202 tmp = note->note + 1; | |
203 | |
204 if (tmp > 0x7f) | |
205 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp); | |
206 | |
207 JSPUTBYTE(tmp & 0x7f); | |
208 | |
209 JSCONVPUT(note->instrument, "Instrument"); | |
210 JSCONVPUT(note->volume, "Volume"); | |
211 JSCONVPUT(note->effect, "Effect"); | |
212 | |
213 tmp = (note->param != jsetNotSet) ? note->param : 0; | |
214 JSPUTBYTE(tmp); | |
215 | |
216 return DMERR_OK; | |
217 } | |
218 | |
219 | |
220 /* Compress a note | |
221 */ | |
222 static int jssCompressNote(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSNote *note) | |
223 { | |
224 Uint8 qflags = 0; | |
225 int qcomp = 0; | |
1201 | 226 |
227 // Determine what would get stored, | |
228 // aka actually how much space we use | |
0 | 229 JSCOMP(note->note, COMP_NOTE); |
230 JSCOMP(note->instrument, COMP_INSTRUMENT); | |
231 JSCOMP(note->volume, COMP_VOLUME); | |
232 JSCOMP(note->effect, COMP_EFFECT); | |
233 if (note->param != jsetNotSet && note->param != 0) | |
234 { | |
235 qflags |= COMP_PARAM; | |
236 qcomp++; | |
237 } | |
238 | |
239 if (qcomp < 4) | |
240 { | |
1201 | 241 // Okay, it's less than 4 bytes, so use compressed |
0 | 242 JSPUTBYTE(qflags | 0x80); |
243 | |
244 if (note->note != jsetNotSet) | |
245 { | |
246 Uint8 tmp = (note->note != jsetNoteOff) ? note->note : 127; | |
247 if (tmp > 0x7f) | |
248 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp); | |
249 JSPUTBYTE(tmp); | |
250 } | |
251 | |
252 JSCOMPPUT(COMP_INSTRUMENT, note->instrument, "Instrument"); | |
253 JSCOMPPUT(COMP_VOLUME, note->volume, "Volume"); | |
254 JSCOMPPUT(COMP_EFFECT, note->effect, "Effect"); | |
255 JSCOMPPUT(COMP_PARAM, note->param, "Param"); | |
1201 | 256 } |
257 else | |
258 { | |
259 // Was 4 bytes or more, just dump it all in .. | |
0 | 260 return jssConvertNote(patBuf, patBufSize, patSize, note); |
1201 | 261 } |
262 | |
0 | 263 return DMERR_OK; |
264 } | |
265 | |
266 | |
267 /* Compress pattern | |
268 */ | |
269 static int jssConvertPatternCompHoriz(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
270 { | |
271 int row, channel; | |
272 *patSize = 0; | |
273 | |
274 for (row = 0; row < pattern->nrows; row++) | |
275 for (channel = 0; channel < pattern->nchannels; channel++) | |
276 { | |
277 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
278 const int res = jssCompressNote(patBuf, patBufSize, patSize, note); | |
279 if (res != DMERR_OK) | |
280 { | |
281 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
282 patBuf, patBufSize, *patSize, row, channel); | |
283 return res; | |
284 } | |
285 } | |
286 | |
287 return DMERR_OK; | |
288 } | |
289 | |
290 | |
291 static int jssConvertPatternCompVert(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
292 { | |
293 int row, channel; | |
294 *patSize = 0; | |
295 | |
296 for (channel = 0; channel < pattern->nchannels; channel++) | |
297 for (row = 0; row < pattern->nrows; row++) | |
298 { | |
299 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
300 const int res = jssCompressNote(patBuf, patBufSize, patSize, note); | |
301 if (res != DMERR_OK) | |
302 { | |
303 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
304 patBuf, patBufSize, *patSize, row, channel); | |
305 return res; | |
306 } | |
307 } | |
308 | |
309 return DMERR_OK; | |
310 } | |
311 | |
312 | |
313 /* Convert a pattern | |
314 */ | |
315 static int jssConvertPatternRawHoriz(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
316 { | |
317 int row, channel; | |
318 *patSize = 0; | |
319 | |
320 for (row = 0; row < pattern->nrows; row++) | |
321 for (channel = 0; channel < pattern->nchannels; channel++) | |
322 { | |
323 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
324 const int res = jssConvertNote(patBuf, patBufSize, patSize, note); | |
325 if (res != DMERR_OK) | |
326 { | |
327 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
328 patBuf, patBufSize, *patSize, row, channel); | |
329 return res; | |
330 } | |
331 } | |
332 | |
333 return DMERR_OK; | |
334 } | |
335 | |
336 | |
337 static int jssConvertPatternRawVert(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
338 { | |
339 int row, channel; | |
340 *patSize = 0; | |
341 | |
342 for (channel = 0; channel < pattern->nchannels; channel++) | |
343 for (row = 0; row < pattern->nrows; row++) | |
344 { | |
345 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
346 const int res = jssConvertNote(patBuf, patBufSize, patSize, note); | |
347 if (res != DMERR_OK) | |
348 { | |
349 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
350 patBuf, patBufSize, *patSize, row, channel); | |
351 return res; | |
352 } | |
353 } | |
354 | |
355 return DMERR_OK; | |
356 } | |
357 | |
358 | |
359 #define JSFOREACHNOTE1 \ | |
360 for (channel = 0; channel < pattern->nchannels; channel++) \ | |
361 for (row = 0; row < pattern->nrows; row++) { \ | |
362 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
363 | |
364 #define JSFOREACHNOTE2 } | |
365 | |
366 static int jssConvertPatternRawElem(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
367 { | |
368 Uint8 tmp; | |
369 int row, channel; | |
370 *patSize = 0; | |
371 | |
372 JSFOREACHNOTE1; | |
373 if (note->note == jsetNotSet) | |
374 tmp = 0; | |
1194 | 375 else |
376 if (note->note == jsetNoteOff) | |
0 | 377 tmp = 127; |
378 else | |
379 tmp = note->note + 1; | |
1194 | 380 |
0 | 381 if (tmp > 0x7f) |
382 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp); | |
1194 | 383 |
0 | 384 JSPUTBYTE(tmp); |
385 JSFOREACHNOTE2; | |
386 | |
387 JSFOREACHNOTE1; | |
388 JSCONVPUT(note->instrument, "Instrument"); | |
389 JSFOREACHNOTE2; | |
390 | |
391 JSFOREACHNOTE1; | |
392 JSCONVPUT(note->volume, "Volume"); | |
393 JSFOREACHNOTE2; | |
394 | |
395 JSFOREACHNOTE1; | |
396 JSCONVPUT(note->effect, "Effect"); | |
397 JSFOREACHNOTE2; | |
398 | |
399 JSFOREACHNOTE1; | |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
400 tmp = (note->param != jsetNotSet) ? note->param : 0; |
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
401 JSPUTBYTE(tmp); |
0 | 402 JSFOREACHNOTE2; |
403 | |
404 return DMERR_OK; | |
405 } | |
406 | |
407 #undef JSFOREACHNOTE1 | |
408 #undef JSFOREACHNOTE2 | |
409 | |
410 | |
411 static void jssCopyEnvelope(JSSMODEnvelope *je, JSSEnvelope *e) | |
412 { | |
413 int i; | |
414 | |
415 je->flags = e->flags; | |
416 je->npoints = e->npoints; | |
417 je->sustain = e->sustain; | |
418 je->loopS = e->loopS; | |
419 je->loopE = e->loopE; | |
420 | |
421 for (i = 0; i < e->npoints; i++) | |
422 { | |
423 je->points[i].frame = e->points[i].frame; | |
424 je->points[i].value = e->points[i].value; | |
425 } | |
426 } | |
427 | |
428 | |
429 /* Save a JSSMOD file | |
430 */ | |
431 int jssSaveJSSMOD(FILE *outFile, JSSModule *m, int patMode, int flags8, int flags16) | |
432 { | |
433 JSSMODHeader jssH; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
434 int i, pattern, order, instr, totalSize; |
0 | 435 const size_t patBufSize = 64*1024; // 64kB pattern buffer |
436 Uint8 *patBuf; | |
437 | |
438 // Check the module | |
439 if (m == NULL) | |
440 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, "Module pointer was NULL\n"); | |
441 | |
1194 | 442 if (m->nchannels < 1 || m->npatterns < 1 || m->norders < 1) |
0 | 443 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
444 "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n", |
0 | 445 m->nchannels, m->npatterns, m->norders); |
446 | |
447 // Create the JSSMOD header | |
448 jssH.idMagic[0] = 'J'; | |
449 jssH.idMagic[1] = 'M'; | |
450 jssH.idVersion = JSSMOD_VERSION; | |
451 jssH.norders = m->norders; | |
452 jssH.npatterns = m->npatterns; | |
453 jssH.nchannels = m->nchannels; | |
454 jssH.nextInstruments = m->nextInstruments; | |
455 jssH.ninstruments = m->ninstruments; | |
456 jssH.defFlags = m->defFlags; | |
457 jssH.intVersion = m->intVersion; | |
458 jssH.defRestartPos = m->defRestartPos; | |
459 jssH.defSpeed = m->defSpeed; | |
460 jssH.defTempo = m->defTempo; | |
461 jssH.patMode = patMode; | |
462 | |
463 // Write header | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
464 totalSize = sizeof(jssH); |
0 | 465 if (fwrite(&jssH, sizeof(jssH), 1, outFile) != 1) |
466 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD header!\n"); | |
467 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
468 dmMsg(1," * JSSMOD-header 0x%04x, %d bytes.\n", JSSMOD_VERSION, totalSize); |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
469 |
0 | 470 // Write orders list |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
471 for (totalSize = order = 0; order < m->norders; order++) |
0 | 472 { |
473 Uint16 tmp = m->orderList[order]; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
474 totalSize += sizeof(tmp); |
0 | 475 if (fwrite(&tmp, sizeof(tmp), 1, outFile) != 1) |
476 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD orders list.\n"); | |
477 } | |
478 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
479 dmMsg(1," * %d item orders list, %d bytes.\n", |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
480 m->norders, totalSize); |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
481 |
0 | 482 // Allocate pattern compression buffer |
483 if ((patBuf = dmMalloc(patBufSize)) == NULL) | |
484 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
485 "Error allocating memory for pattern compression buffer.\n"); | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
486 |
0 | 487 |
488 // Write patterns | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
489 for (totalSize = pattern = 0; pattern < m->npatterns; pattern++) |
0 | 490 { |
491 JSSMODPattern patHead; | |
492 size_t finalSize = 0; | |
493 | |
494 switch (patMode) | |
495 { | |
496 case PATMODE_RAW_HORIZ: | |
497 i = jssConvertPatternRawHoriz(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
498 break; | |
499 case PATMODE_COMP_HORIZ: | |
500 i = jssConvertPatternCompHoriz(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
501 break; | |
502 case PATMODE_RAW_VERT: | |
503 i = jssConvertPatternRawVert(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
504 break; | |
505 case PATMODE_COMP_VERT: | |
506 i = jssConvertPatternCompVert(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
507 break; | |
508 case PATMODE_RAW_ELEM: | |
509 i = jssConvertPatternRawElem(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
510 break; | |
511 default: | |
512 dmFree(patBuf); | |
513 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
514 "Unsupported pattern conversion mode %d.\n", patMode); | |
515 } | |
516 | |
517 if (i != DMERR_OK) | |
518 { | |
519 dmFree(patBuf); | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
520 JSSERROR(i, i, "Error converting pattern data #%d\n", pattern); |
0 | 521 } |
522 else | |
523 { | |
524 dmMsg(3, " - Pattern %d size %d bytes\n", pattern, finalSize); | |
525 patHead.nrows = m->patterns[pattern]->nrows; | |
526 patHead.size = finalSize; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
527 totalSize += finalSize + sizeof(patHead); |
0 | 528 |
529 if (fwrite(&patHead, sizeof(patHead), 1, outFile) != 1) | |
530 { | |
531 dmFree(patBuf); | |
532 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
533 "Error writing pattern #%d header\n", pattern); | |
534 } | |
535 | |
536 if (fwrite(patBuf, sizeof(Uint8), finalSize, outFile) != finalSize) | |
537 { | |
538 dmFree(patBuf); | |
539 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
540 "Error writing pattern #%d data\n", pattern); | |
541 } | |
542 } | |
543 } | |
544 dmFree(patBuf); | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
545 dmMsg(1," * %d patterns, %d bytes.\n", m->npatterns, totalSize); |
0 | 546 |
547 // Write extended instruments | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
548 for (totalSize = instr = 0; instr < m->nextInstruments; instr++) |
0 | 549 { |
550 JSSMODExtInstrument jssE; | |
551 JSSExtInstrument *einst = m->extInstruments[instr]; | |
552 | |
1167 | 553 dmMemset(&jssE, 0, sizeof(jssE)); |
0 | 554 |
555 if (einst) | |
556 { | |
557 // Create header | |
558 jssE.nsamples = einst->nsamples; | |
559 for (i = 0; i < jsetNNotes; i++) | |
560 { | |
561 int snum = einst->sNumForNotes[i]; | |
1118
de25e0f2b856
Fix sNumForNotes conversion/handling in JSSMOD loader and xm2jss.
Matti Hamalainen <ccr@tnsp.org>
parents:
1116
diff
changeset
|
562 jssE.sNumForNotes[i] = (snum != jsetNotSet) ? snum + 1: 0; |
0 | 563 } |
564 | |
565 jssCopyEnvelope(&jssE.volumeEnv, &(einst->volumeEnv)); | |
566 jssCopyEnvelope(&jssE.panningEnv, &(einst->panningEnv)); | |
567 jssE.vibratoType = einst->vibratoType; | |
568 jssE.vibratoSweep = einst->vibratoSweep; | |
569 jssE.vibratoDepth = einst->vibratoDepth; | |
570 jssE.fadeOut = einst->fadeOut; | |
571 } else | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
572 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Extended instrument #%d NULL!\n", instr); |
0 | 573 |
574 // Write to file | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
575 totalSize += sizeof(jssE); |
0 | 576 if (fwrite(&jssE, sizeof(jssE), 1, outFile) != 1) |
577 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
578 "Could not write JSSMOD extended instrument #%d to file!\n", instr); |
0 | 579 } |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
580 dmMsg(1," * %d Extended Instruments, %d bytes.\n", m->nextInstruments, totalSize); |
0 | 581 |
582 | |
583 // Write sample instrument headers | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
584 for (totalSize = instr = 0; instr < m->ninstruments; instr++) |
0 | 585 { |
586 JSSMODInstrument jssI; | |
1115 | 587 JSSInstrument *inst = m->instruments[instr]; |
0 | 588 |
1167 | 589 dmMemset(&jssI, 0, sizeof(jssI)); |
0 | 590 |
591 // Create header | |
1115 | 592 if (inst != NULL) |
0 | 593 { |
1115 | 594 jssI.size = inst->size; |
595 jssI.loopS = inst->loopS; | |
596 jssI.loopE = inst->loopE; | |
597 jssI.volume = inst->volume; | |
598 jssI.flags = inst->flags; | |
599 jssI.C4BaseSpeed = inst->C4BaseSpeed; | |
600 jssI.ERelNote = inst->ERelNote; | |
601 jssI.EFineTune = inst->EFineTune; | |
602 jssI.EPanning = inst->EPanning; | |
603 jssI.hasData = (inst->data != NULL) ? TRUE : FALSE; | |
604 jssI.convFlags = (inst->flags & jsf16bit) ? flags16 : flags8; | |
0 | 605 } |
606 else | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
607 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Instrument #%d NULL!\n", instr); |
0 | 608 |
609 // Write to file | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
610 totalSize += sizeof(jssI); |
0 | 611 if (fwrite(&jssI, sizeof(jssI), 1, outFile) != 1) |
612 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
613 "Could not write JSSMOD instrument #%d to file!\n", instr); |
0 | 614 } |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
615 dmMsg(1," * %d Instrument headers, %d bytes.\n", m->ninstruments, totalSize); |
0 | 616 |
617 // Write sample data | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
618 for (totalSize = instr = 0; instr < m->ninstruments; instr++) |
0 | 619 if (m->instruments[instr]) |
620 { | |
621 JSSInstrument *inst = m->instruments[instr]; | |
622 if (inst->data != NULL) | |
623 { | |
624 size_t res; | |
625 if (inst->flags & jsf16bit) | |
626 { | |
627 jssEncodeSample16(inst->data, inst->size, flags16); | |
628 res = fwrite(inst->data, sizeof(Uint16), inst->size, outFile); | |
629 } | |
630 else | |
631 { | |
632 jssEncodeSample8(inst->data, inst->size, flags8); | |
633 res = fwrite(inst->data, sizeof(Uint8), inst->size, outFile); | |
634 } | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
635 |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
636 totalSize += inst->size; |
86 | 637 if (res != (size_t) inst->size) |
0 | 638 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
639 "Could not write JSSMOD sample #%d to file!\n", instr); |
0 | 640 } |
641 } | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
642 dmMsg(1," * %d samples, %d bytes.\n", m->ninstruments, totalSize); |
0 | 643 |
644 return DMERR_OK; | |
645 } | |
646 | |
647 | |
648 /* Optimize a given module | |
649 */ | |
650 JSSModule *optimizeModule(JSSModule *m) | |
651 { | |
652 BOOL usedPatterns[jsetMaxPatterns + 1], | |
653 usedInstruments[jsetMaxInstruments + 1], | |
654 usedExtInstruments[jsetMaxInstruments + 1]; | |
655 int mapExtInstruments[jsetMaxInstruments + 1], | |
656 mapInstruments[jsetMaxInstruments + 1], | |
657 mapPatterns[jsetMaxPatterns + 1]; | |
658 JSSModule *r = NULL; | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
659 int i, n8, n16, unused; |
0 | 660 |
661 // Allocate a new module | |
662 if ((r = jssAllocateModule()) == NULL) | |
663 return NULL; | |
664 | |
665 // Copy things | |
666 r->moduleType = m->moduleType; | |
667 r->moduleName = dm_strdup(m->moduleName); | |
668 r->trackerName = dm_strdup(m->trackerName); | |
669 r->defSpeed = m->defSpeed; | |
670 r->defTempo = m->defTempo; | |
671 r->defFlags = m->defFlags; | |
672 r->defRestartPos = m->defRestartPos; | |
673 r->intVersion = m->intVersion; | |
674 r->nchannels = m->nchannels; | |
675 r->norders = m->norders; | |
676 for (i = 0; i < jsetNChannels; i++) | |
677 r->defPanning[i] = m->defPanning[i]; | |
678 | |
679 // Initialize values | |
680 for (i = 0; i <= jsetMaxInstruments; i++) | |
681 { | |
682 usedExtInstruments[i] = FALSE; | |
798 | 683 usedInstruments[i] = FALSE; |
684 mapExtInstruments[i] = jsetNotSet; | |
685 mapInstruments[i] = jsetNotSet; | |
0 | 686 } |
687 | |
688 for (i = 0; i <= jsetMaxPatterns; i++) | |
689 { | |
690 usedPatterns[i] = FALSE; | |
798 | 691 mapPatterns[i] = jsetNotSet; |
0 | 692 } |
693 | |
1094 | 694 // |
798 | 695 // Find out all actually used patterns and ext.instruments |
1100 | 696 // by going through all patterns specified in the order list |
1094 | 697 // |
0 | 698 for (i = 0; i < m->norders; i++) |
699 { | |
700 int pattern = m->orderList[i]; | |
701 if (pattern >= 0 && pattern < m->npatterns) | |
702 { | |
703 JSSPattern *p = m->patterns[pattern]; | |
704 if (p != NULL) | |
705 { | |
706 int row, channel; | |
707 JSSNote *n = p->data; | |
708 | |
1100 | 709 // Mark this pattern as used |
0 | 710 usedPatterns[pattern] = TRUE; |
711 | |
1100 | 712 // Check all notes in this pattern to see what instruments are used |
0 | 713 for (row = 0; row < p->nrows; row++) |
714 for (channel = 0; channel < p->nchannels; channel++, n++) | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
715 if (n->instrument != jsetNotSet) |
0 | 716 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
717 if (n->instrument >= 0 && n->instrument < m->nextInstruments) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
718 usedExtInstruments[n->instrument] = TRUE; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
719 else |
0 | 720 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
721 dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n", |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
722 pattern, row, channel, n->instrument + 1); |
0 | 723 } |
724 } | |
725 } | |
726 else | |
727 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
728 dmErrorMsg("Pattern 0x%x is used on order 0x%x, but has no data!\n", |
1094 | 729 pattern, i); |
807
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
730 |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
731 // Fix it. |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
732 m->orderList[i] = jsetMaxPatterns; |
0 | 733 } |
734 } | |
735 else | |
736 if (pattern != jsetMaxPatterns) | |
737 { | |
1101 | 738 dmErrorMsg("Order 0x%x has invalid pattern number 0x%x, changing to empty!\n", |
1094 | 739 i, pattern); |
807
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
740 |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
741 // Fix it. |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
742 m->orderList[i] = jsetMaxPatterns; |
0 | 743 } |
744 } | |
745 | |
1094 | 746 // |
1100 | 747 // Find used sample instruments |
1094 | 748 // |
1101 | 749 dmMsg(1, "Checking ext.instruments for used sample instruments...\n"); |
856
415cc781e127
Fix some brain farts in instrument remapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
807
diff
changeset
|
750 for (i = 0; i < jsetMaxInstruments; i++) |
0 | 751 if (usedExtInstruments[i] && m->extInstruments[i] != NULL) |
752 { | |
753 int note; | |
754 JSSExtInstrument *e = m->extInstruments[i]; | |
1094 | 755 |
0 | 756 for (note = 0; note < jsetNNotes; note++) |
757 if (e->sNumForNotes[note] != jsetNotSet) | |
758 { | |
759 int q = e->sNumForNotes[note]; | |
760 if (q >= 0 && q < m->ninstruments) | |
761 { | |
762 usedInstruments[q] = TRUE; | |
763 } | |
764 else | |
765 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
766 dmErrorMsg("Ext.instrument #%d sNumForNotes[%d] value out range (%d < %d).\n", |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
767 i + 1, m->ninstruments, q + 1); |
0 | 768 } |
769 } | |
770 } | |
1094 | 771 |
772 // | |
0 | 773 // Create pattern mappings |
1094 | 774 // |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
775 dmMsg(1, "Creating pattern remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
776 for (unused = i = 0; i <= jsetMaxPatterns; i++) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
777 if (usedPatterns[i]) |
0 | 778 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
779 JSSPattern *pat; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
780 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
781 if (i >= m->npatterns) |
0 | 782 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
783 dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
784 continue; |
0 | 785 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
786 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
787 if ((pat = m->patterns[i]) == NULL) |
0 | 788 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
789 dmErrorMsg("Pattern 0x%x used but is NULL.\n", i); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
790 continue; |
0 | 791 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
792 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
793 mapPatterns[i] = r->npatterns; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
794 r->patterns[r->npatterns] = m->patterns[i]; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
795 (r->npatterns)++; |
0 | 796 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
797 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
798 if (m->patterns[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
799 unused++; |
1094 | 800 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
801 dmMsg(1, "%d used patterns (%d unused).\n", |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
802 r->npatterns, unused); |
0 | 803 |
804 | |
1094 | 805 // |
0 | 806 // Re-map instruments |
1094 | 807 // |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
808 dmMsg(1, "Creating sample instrument remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
809 for (unused = n8 = n16 = i = 0; i < jsetMaxInstruments; i++) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
810 if (usedInstruments[i]) |
0 | 811 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
812 JSSInstrument *ip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
813 if (optStripInstr) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
814 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
815 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
816 if (i >= m->ninstruments) |
0 | 817 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
818 dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n", |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
819 i + 1, m->ninstruments); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
820 continue; |
0 | 821 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
822 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
823 if ((ip = m->instruments[i]) == NULL) |
0 | 824 { |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
825 dmErrorMsg("Instrument 0x%x used but is NULL.\n", i + 1); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
826 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
827 } |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
828 |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
829 dmPrint(2, "%02x -> %02x : ", i + 1, r->ninstruments + 1); |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
830 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
831 mapInstruments[i] = r->ninstruments; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
832 r->instruments[r->ninstruments] = ip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
833 (r->ninstruments)++; |
0 | 834 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
835 if (ip->flags & jsf16bit) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
836 n16++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
837 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
838 n8++; |
0 | 839 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
840 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
841 if (m->instruments[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
842 unused++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
843 |
0 | 844 dmPrint(2, "\n"); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
845 dmMsg(1, "Total of %d [16-bit] + %d [8-bit] samples = %d instruments (%d unused).\n", |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
846 n16, n8, r->ninstruments, unused); |
0 | 847 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
848 // |
0 | 849 // Re-map ext.instruments |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
850 // |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
851 dmMsg(1, "Creating ext.instrument remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
852 for (unused = i = 0; i < jsetMaxInstruments; i++) |
0 | 853 if (usedExtInstruments[i]) |
854 { | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
855 JSSExtInstrument *eip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
856 int note; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
857 if (optStripExtInstr) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
858 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
859 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
860 if (i >= m->nextInstruments) |
0 | 861 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
862 dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n", |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
863 i + 1, m->nextInstruments); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
864 continue; |
0 | 865 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
866 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
867 if ((eip = m->extInstruments[i]) == NULL) |
0 | 868 { |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
869 dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", i + 1); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
870 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
871 } |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
872 |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
873 dmPrint(2, "%02x -> %02x : ", i + 1, r->nextInstruments + 1); |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
874 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
875 mapExtInstruments[i] = r->nextInstruments; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
876 r->extInstruments[r->nextInstruments] = eip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
877 (r->nextInstruments)++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
878 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
879 // Re-map sNumForNotes table for this ext.instrument |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
880 for (note = 0; note < jsetNNotes; note++) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
881 { |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
882 int q = eip->sNumForNotes[note]; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
883 if (q != jsetNotSet) |
0 | 884 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
885 int map; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
886 if (q >= 0 && q < jsetMaxInstruments) |
0 | 887 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
888 map = mapInstruments[q]; |
0 | 889 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
890 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
891 { |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
892 map = jsetNotSet; |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
893 dmErrorMsg("Einst=%d, note=%d, sNumForNote=%d (%d max)\n", |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
894 i + 1, note, q + 1, r->ninstruments); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
895 } |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
896 |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
897 dmPrint(3, "%02x.%02x ", q + 1, map + 1); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
898 eip->sNumForNotes[note] = map; |
0 | 899 } |
900 } | |
901 } | |
902 else | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
903 if (m->extInstruments[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
904 unused++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
905 |
0 | 906 dmPrint(2, "\n"); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
907 dmMsg(1, "%d extended instruments (%d unused).\n", |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
908 r->nextInstruments, unused); |
1094 | 909 |
910 // | |
911 // Remap pattern data with remapped instrument data | |
912 // | |
0 | 913 for (i = 0; i < r->npatterns; i++) |
914 { | |
915 int row, channel; | |
916 JSSPattern *p = r->patterns[i]; | |
917 JSSNote *n = p->data; | |
918 | |
919 for (row = 0; row < p->nrows; row++) | |
920 for (channel = 0; channel < p->nchannels; channel++, n++) | |
921 { | |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
922 char effect; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
923 |
1109 | 924 // If not stripping extended instruments, check for |
925 // the validity of the used instrument and remap | |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
926 if (!optStripExtInstr) |
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
927 { |
856
415cc781e127
Fix some brain farts in instrument remapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
807
diff
changeset
|
928 if (n->instrument >= 0 && n->instrument < jsetMaxInstruments) |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
929 n->instrument = mapExtInstruments[n->instrument]; |
202
85614db5f577
Warn about invalid instruments.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
930 |
1116 | 931 if (n->instrument != jsetNotSet && |
932 r->extInstruments[n->instrument] == NULL) | |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
933 { |
1116 | 934 dmErrorMsg("Non-existing instrument used #%d, INTERNAL ERROR.\n", |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
935 n->instrument + 1); |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
936 } |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
937 } |
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
938 |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
939 JMPGETEFFECT(effect, n->effect); |
1094 | 940 |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
941 switch (effect) |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
942 { |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
943 case 'C': // Cxx = Set volume |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
944 if (n->volume == jsetNotSet) |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
945 { |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
946 n->volume = n->param; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
947 n->effect = jsetNotSet; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
948 n->param = jsetNotSet; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
949 } |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
950 break; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
951 } |
0 | 952 } |
953 } | |
954 | |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
955 // |
0 | 956 // Remap orders list |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
957 // |
0 | 958 for (i = 0; i < m->norders; i++) |
959 { | |
960 r->orderList[i] = mapPatterns[m->orderList[i]]; | |
961 } | |
962 | |
963 return r; | |
964 } | |
965 | |
966 | |
967 int main(int argc, char *argv[]) | |
968 { | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
969 DMResource *inFile = NULL; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
970 FILE *outFile = NULL; |
0 | 971 JSSModule *sm, *dm; |
972 int result; | |
973 | |
974 dmInitProg("xm2jss", "XM to JSSMOD converter", "0.6", NULL, NULL); | |
975 dmVerbosity = 0; | |
976 | |
977 // Parse arguments | |
978 if (!dmArgsProcess(argc, argv, optList, optListN, | |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
856
diff
changeset
|
979 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
0 | 980 exit(1); |
981 | |
300 | 982 // Check arguments |
983 if (optInFilename == NULL || optOutFilename == NULL) | |
984 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
985 dmErrorMsg("Input or output file not specified. Try --help.\n"); |
300 | 986 return 1; |
987 } | |
0 | 988 |
989 // Read the source file | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
990 if ((result = dmf_create_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
0 | 991 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
992 dmErrorMsg("Error opening input file '%s', %d: %s\n", |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
993 optInFilename, result, dmErrorStr(result)); |
0 | 994 return 1; |
995 } | |
996 | |
997 // Initialize miniJSS | |
998 jssInit(); | |
999 | |
1000 // Read file | |
1001 dmMsg(1, "Reading XM-format file ...\n"); | |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
1002 result = jssLoadXM(inFile, &sm, FALSE); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1003 dmf_close(inFile); |
0 | 1004 if (result != 0) |
1005 { | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
1006 dmErrorMsg("Error while loading XM file (%d), ", result); |
0 | 1007 if (optIgnoreErrors) |
1008 fprintf(stderr, "ignoring. This may cause problems.\n"); | |
1009 else | |
1010 { | |
1011 fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n"); | |
1012 return 2; | |
1013 } | |
1014 } | |
1015 | |
1016 // Check stripping settings | |
1017 if (optStripExtInstr) optStripInstr = TRUE; | |
1018 if (optStripInstr) optStripSamples = TRUE; | |
1019 | |
1020 // Remove samples | |
1021 if (optStripSamples) | |
1022 { | |
1023 int i; | |
1024 | |
1025 dmMsg(1, "Stripping samples...\n"); | |
1026 for (i = 0; i < sm->ninstruments; i++) | |
1027 { | |
1028 dmFree(sm->instruments[i]->data); | |
1029 sm->instruments[i]->data = NULL; | |
1030 } | |
1031 } | |
1032 | |
1033 // Remove instruments | |
1034 if (optStripInstr) | |
1035 { | |
1036 int i; | |
1037 | |
1038 dmMsg(1, "Stripping instruments...\n"); | |
1039 for (i = 0; i < sm->ninstruments; i++) | |
1040 { | |
1041 dmFree(sm->instruments[i]); | |
1042 sm->instruments[i] = NULL; | |
1043 } | |
1044 sm->ninstruments = 0; | |
1045 } | |
1046 | |
1047 // Remove ext.instruments | |
1048 if (optStripExtInstr) | |
1049 { | |
1050 int i; | |
1051 | |
1052 dmMsg(1, "Stripping ext.instruments...\n"); | |
1053 for (i = 0; i < sm->nextInstruments; i++) | |
1054 { | |
1055 dmFree(sm->extInstruments[i]); | |
1056 sm->extInstruments[i] = NULL; | |
1057 } | |
1058 sm->nextInstruments = 0; | |
1059 } | |
1109 | 1060 |
0 | 1061 // Run the optimization procedure |
1062 if (optOptimize) | |
1063 { | |
1064 dmMsg(1, "Optimizing module data...\n"); | |
1065 dm = optimizeModule(sm); | |
1066 } else | |
1067 dm = sm; | |
1068 | |
1069 // Write output file | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1070 if ((outFile = fopen(optOutFilename, "wb")) == NULL) |
0 | 1071 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
1072 dmErrorMsg("Error creating output file '%s', %d: %s\n", |
300 | 1073 optOutFilename, errno, strerror(errno)); |
0 | 1074 return 1; |
1075 } | |
1076 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1077 dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n", |
0 | 1078 optPatternMode, optSampMode8, optSampMode16); |
1079 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1080 result = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16); |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1081 |
1114
7aaab1533c90
Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents:
1113
diff
changeset
|
1082 dmFree(sm); |
7aaab1533c90
Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents:
1113
diff
changeset
|
1083 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1084 fclose(outFile); |
0 | 1085 |
1086 if (result != 0) | |
1087 { | |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1088 dmErrorMsg( |
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1089 "Error while saving JSSMOD file, %d: %s\n" |
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1090 "WARNING: The resulting file may be broken!\n", |
300 | 1091 result, dmErrorStr(result)); |
0 | 1092 } |
300 | 1093 else |
1094 { | |
1095 dmMsg(1, "Conversion complete.\n"); | |
1096 } | |
0 | 1097 return 0; |
1098 } |