Mercurial > hg > dmlib
annotate tools/xm2jss.c @ 1195:4d27c4ec8d4d
Use %d instead of %i in formats.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Mar 2015 15:58:24 +0200 |
parents | 1f2cfa0119aa |
children | 83693e2797dd |
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; | |
226 | |
227 JSCOMP(note->note, COMP_NOTE); | |
228 JSCOMP(note->instrument, COMP_INSTRUMENT); | |
229 JSCOMP(note->volume, COMP_VOLUME); | |
230 JSCOMP(note->effect, COMP_EFFECT); | |
231 if (note->param != jsetNotSet && note->param != 0) | |
232 { | |
233 qflags |= COMP_PARAM; | |
234 qcomp++; | |
235 } | |
236 | |
237 if (qcomp < 4) | |
238 { | |
239 JSPUTBYTE(qflags | 0x80); | |
240 | |
241 if (note->note != jsetNotSet) | |
242 { | |
243 Uint8 tmp = (note->note != jsetNoteOff) ? note->note : 127; | |
244 if (tmp > 0x7f) | |
245 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp); | |
246 JSPUTBYTE(tmp); | |
247 } | |
248 | |
249 JSCOMPPUT(COMP_INSTRUMENT, note->instrument, "Instrument"); | |
250 JSCOMPPUT(COMP_VOLUME, note->volume, "Volume"); | |
251 JSCOMPPUT(COMP_EFFECT, note->effect, "Effect"); | |
252 JSCOMPPUT(COMP_PARAM, note->param, "Param"); | |
253 } else | |
254 return jssConvertNote(patBuf, patBufSize, patSize, note); | |
255 | |
256 return DMERR_OK; | |
257 } | |
258 | |
259 | |
260 /* Compress pattern | |
261 */ | |
262 static int jssConvertPatternCompHoriz(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
263 { | |
264 int row, channel; | |
265 *patSize = 0; | |
266 | |
267 for (row = 0; row < pattern->nrows; row++) | |
268 for (channel = 0; channel < pattern->nchannels; channel++) | |
269 { | |
270 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
271 const int res = jssCompressNote(patBuf, patBufSize, patSize, note); | |
272 if (res != DMERR_OK) | |
273 { | |
274 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
275 patBuf, patBufSize, *patSize, row, channel); | |
276 return res; | |
277 } | |
278 } | |
279 | |
280 return DMERR_OK; | |
281 } | |
282 | |
283 | |
284 static int jssConvertPatternCompVert(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
285 { | |
286 int row, channel; | |
287 *patSize = 0; | |
288 | |
289 for (channel = 0; channel < pattern->nchannels; channel++) | |
290 for (row = 0; row < pattern->nrows; row++) | |
291 { | |
292 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
293 const int res = jssCompressNote(patBuf, patBufSize, patSize, note); | |
294 if (res != DMERR_OK) | |
295 { | |
296 JSSERROR(res, res, "Note compression failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
297 patBuf, patBufSize, *patSize, row, channel); | |
298 return res; | |
299 } | |
300 } | |
301 | |
302 return DMERR_OK; | |
303 } | |
304 | |
305 | |
306 /* Convert a pattern | |
307 */ | |
308 static int jssConvertPatternRawHoriz(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
309 { | |
310 int row, channel; | |
311 *patSize = 0; | |
312 | |
313 for (row = 0; row < pattern->nrows; row++) | |
314 for (channel = 0; channel < pattern->nchannels; channel++) | |
315 { | |
316 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
317 const int res = jssConvertNote(patBuf, patBufSize, patSize, note); | |
318 if (res != DMERR_OK) | |
319 { | |
320 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
321 patBuf, patBufSize, *patSize, row, channel); | |
322 return res; | |
323 } | |
324 } | |
325 | |
326 return DMERR_OK; | |
327 } | |
328 | |
329 | |
330 static int jssConvertPatternRawVert(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
331 { | |
332 int row, channel; | |
333 *patSize = 0; | |
334 | |
335 for (channel = 0; channel < pattern->nchannels; channel++) | |
336 for (row = 0; row < pattern->nrows; row++) | |
337 { | |
338 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
339 const int res = jssConvertNote(patBuf, patBufSize, patSize, note); | |
340 if (res != DMERR_OK) | |
341 { | |
342 JSSERROR(res, res, "Note conversion failed [patBuf=%p, patBufSize=%d, patSize=%d, row=%d, chn=%d]\n", | |
343 patBuf, patBufSize, *patSize, row, channel); | |
344 return res; | |
345 } | |
346 } | |
347 | |
348 return DMERR_OK; | |
349 } | |
350 | |
351 | |
352 #define JSFOREACHNOTE1 \ | |
353 for (channel = 0; channel < pattern->nchannels; channel++) \ | |
354 for (row = 0; row < pattern->nrows; row++) { \ | |
355 const JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
356 | |
357 #define JSFOREACHNOTE2 } | |
358 | |
359 static int jssConvertPatternRawElem(Uint8 *patBuf, const size_t patBufSize, size_t *patSize, const JSSPattern *pattern) | |
360 { | |
361 Uint8 tmp; | |
362 int row, channel; | |
363 *patSize = 0; | |
364 | |
365 JSFOREACHNOTE1; | |
366 if (note->note == jsetNotSet) | |
367 tmp = 0; | |
1194 | 368 else |
369 if (note->note == jsetNoteOff) | |
0 | 370 tmp = 127; |
371 else | |
372 tmp = note->note + 1; | |
1194 | 373 |
0 | 374 if (tmp > 0x7f) |
375 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, "Note value out of bounds %d > 0x7f.\n", tmp); | |
1194 | 376 |
0 | 377 JSPUTBYTE(tmp); |
378 JSFOREACHNOTE2; | |
379 | |
380 JSFOREACHNOTE1; | |
381 JSCONVPUT(note->instrument, "Instrument"); | |
382 JSFOREACHNOTE2; | |
383 | |
384 JSFOREACHNOTE1; | |
385 JSCONVPUT(note->volume, "Volume"); | |
386 JSFOREACHNOTE2; | |
387 | |
388 JSFOREACHNOTE1; | |
389 JSCONVPUT(note->effect, "Effect"); | |
390 JSFOREACHNOTE2; | |
391 | |
392 JSFOREACHNOTE1; | |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
393 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
|
394 JSPUTBYTE(tmp); |
0 | 395 JSFOREACHNOTE2; |
396 | |
397 return DMERR_OK; | |
398 } | |
399 | |
400 #undef JSFOREACHNOTE1 | |
401 #undef JSFOREACHNOTE2 | |
402 | |
403 | |
404 static void jssCopyEnvelope(JSSMODEnvelope *je, JSSEnvelope *e) | |
405 { | |
406 int i; | |
407 | |
408 je->flags = e->flags; | |
409 je->npoints = e->npoints; | |
410 je->sustain = e->sustain; | |
411 je->loopS = e->loopS; | |
412 je->loopE = e->loopE; | |
413 | |
414 for (i = 0; i < e->npoints; i++) | |
415 { | |
416 je->points[i].frame = e->points[i].frame; | |
417 je->points[i].value = e->points[i].value; | |
418 } | |
419 } | |
420 | |
421 | |
422 /* Save a JSSMOD file | |
423 */ | |
424 int jssSaveJSSMOD(FILE *outFile, JSSModule *m, int patMode, int flags8, int flags16) | |
425 { | |
426 JSSMODHeader jssH; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
427 int i, pattern, order, instr, totalSize; |
0 | 428 const size_t patBufSize = 64*1024; // 64kB pattern buffer |
429 Uint8 *patBuf; | |
430 | |
431 // Check the module | |
432 if (m == NULL) | |
433 JSSERROR(DMERR_NULLPTR, DMERR_NULLPTR, "Module pointer was NULL\n"); | |
434 | |
1194 | 435 if (m->nchannels < 1 || m->npatterns < 1 || m->norders < 1) |
0 | 436 JSSERROR(DMERR_BOUNDS, DMERR_BOUNDS, |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
437 "Module had invalid values (nchannels=%d, npatterns=%d, norders=%d)\n", |
0 | 438 m->nchannels, m->npatterns, m->norders); |
439 | |
440 // Create the JSSMOD header | |
441 jssH.idMagic[0] = 'J'; | |
442 jssH.idMagic[1] = 'M'; | |
443 jssH.idVersion = JSSMOD_VERSION; | |
444 jssH.norders = m->norders; | |
445 jssH.npatterns = m->npatterns; | |
446 jssH.nchannels = m->nchannels; | |
447 jssH.nextInstruments = m->nextInstruments; | |
448 jssH.ninstruments = m->ninstruments; | |
449 jssH.defFlags = m->defFlags; | |
450 jssH.intVersion = m->intVersion; | |
451 jssH.defRestartPos = m->defRestartPos; | |
452 jssH.defSpeed = m->defSpeed; | |
453 jssH.defTempo = m->defTempo; | |
454 jssH.patMode = patMode; | |
455 | |
456 // Write header | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
457 totalSize = sizeof(jssH); |
0 | 458 if (fwrite(&jssH, sizeof(jssH), 1, outFile) != 1) |
459 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD header!\n"); | |
460 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
461 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
|
462 |
0 | 463 // Write orders list |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
464 for (totalSize = order = 0; order < m->norders; order++) |
0 | 465 { |
466 Uint16 tmp = m->orderList[order]; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
467 totalSize += sizeof(tmp); |
0 | 468 if (fwrite(&tmp, sizeof(tmp), 1, outFile) != 1) |
469 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, "Could not write JSSMOD orders list.\n"); | |
470 } | |
471 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
472 dmMsg(1," * %d item orders list, %d bytes.\n", |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
473 m->norders, totalSize); |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
474 |
0 | 475 // Allocate pattern compression buffer |
476 if ((patBuf = dmMalloc(patBufSize)) == NULL) | |
477 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
478 "Error allocating memory for pattern compression buffer.\n"); | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
479 |
0 | 480 |
481 // Write patterns | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
482 for (totalSize = pattern = 0; pattern < m->npatterns; pattern++) |
0 | 483 { |
484 JSSMODPattern patHead; | |
485 size_t finalSize = 0; | |
486 | |
487 switch (patMode) | |
488 { | |
489 case PATMODE_RAW_HORIZ: | |
490 i = jssConvertPatternRawHoriz(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
491 break; | |
492 case PATMODE_COMP_HORIZ: | |
493 i = jssConvertPatternCompHoriz(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
494 break; | |
495 case PATMODE_RAW_VERT: | |
496 i = jssConvertPatternRawVert(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
497 break; | |
498 case PATMODE_COMP_VERT: | |
499 i = jssConvertPatternCompVert(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
500 break; | |
501 case PATMODE_RAW_ELEM: | |
502 i = jssConvertPatternRawElem(patBuf, patBufSize, &finalSize, m->patterns[pattern]); | |
503 break; | |
504 default: | |
505 dmFree(patBuf); | |
506 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
507 "Unsupported pattern conversion mode %d.\n", patMode); | |
508 } | |
509 | |
510 if (i != DMERR_OK) | |
511 { | |
512 dmFree(patBuf); | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
513 JSSERROR(i, i, "Error converting pattern data #%d\n", pattern); |
0 | 514 } |
515 else | |
516 { | |
517 dmMsg(3, " - Pattern %d size %d bytes\n", pattern, finalSize); | |
518 patHead.nrows = m->patterns[pattern]->nrows; | |
519 patHead.size = finalSize; | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
520 totalSize += finalSize + sizeof(patHead); |
0 | 521 |
522 if (fwrite(&patHead, sizeof(patHead), 1, outFile) != 1) | |
523 { | |
524 dmFree(patBuf); | |
525 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
526 "Error writing pattern #%d header\n", pattern); | |
527 } | |
528 | |
529 if (fwrite(patBuf, sizeof(Uint8), finalSize, outFile) != finalSize) | |
530 { | |
531 dmFree(patBuf); | |
532 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
533 "Error writing pattern #%d data\n", pattern); | |
534 } | |
535 } | |
536 } | |
537 dmFree(patBuf); | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
538 dmMsg(1," * %d patterns, %d bytes.\n", m->npatterns, totalSize); |
0 | 539 |
540 // Write extended instruments | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
541 for (totalSize = instr = 0; instr < m->nextInstruments; instr++) |
0 | 542 { |
543 JSSMODExtInstrument jssE; | |
544 JSSExtInstrument *einst = m->extInstruments[instr]; | |
545 | |
1167 | 546 dmMemset(&jssE, 0, sizeof(jssE)); |
0 | 547 |
548 if (einst) | |
549 { | |
550 // Create header | |
551 jssE.nsamples = einst->nsamples; | |
552 for (i = 0; i < jsetNNotes; i++) | |
553 { | |
554 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
|
555 jssE.sNumForNotes[i] = (snum != jsetNotSet) ? snum + 1: 0; |
0 | 556 } |
557 | |
558 jssCopyEnvelope(&jssE.volumeEnv, &(einst->volumeEnv)); | |
559 jssCopyEnvelope(&jssE.panningEnv, &(einst->panningEnv)); | |
560 jssE.vibratoType = einst->vibratoType; | |
561 jssE.vibratoSweep = einst->vibratoSweep; | |
562 jssE.vibratoDepth = einst->vibratoDepth; | |
563 jssE.fadeOut = einst->fadeOut; | |
564 } else | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
565 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Extended instrument #%d NULL!\n", instr); |
0 | 566 |
567 // Write to file | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
568 totalSize += sizeof(jssE); |
0 | 569 if (fwrite(&jssE, sizeof(jssE), 1, outFile) != 1) |
570 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
571 "Could not write JSSMOD extended instrument #%d to file!\n", instr); |
0 | 572 } |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
573 dmMsg(1," * %d Extended Instruments, %d bytes.\n", m->nextInstruments, totalSize); |
0 | 574 |
575 | |
576 // Write sample instrument headers | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
577 for (totalSize = instr = 0; instr < m->ninstruments; instr++) |
0 | 578 { |
579 JSSMODInstrument jssI; | |
1115 | 580 JSSInstrument *inst = m->instruments[instr]; |
0 | 581 |
1167 | 582 dmMemset(&jssI, 0, sizeof(jssI)); |
0 | 583 |
584 // Create header | |
1115 | 585 if (inst != NULL) |
0 | 586 { |
1115 | 587 jssI.size = inst->size; |
588 jssI.loopS = inst->loopS; | |
589 jssI.loopE = inst->loopE; | |
590 jssI.volume = inst->volume; | |
591 jssI.flags = inst->flags; | |
592 jssI.C4BaseSpeed = inst->C4BaseSpeed; | |
593 jssI.ERelNote = inst->ERelNote; | |
594 jssI.EFineTune = inst->EFineTune; | |
595 jssI.EPanning = inst->EPanning; | |
596 jssI.hasData = (inst->data != NULL) ? TRUE : FALSE; | |
597 jssI.convFlags = (inst->flags & jsf16bit) ? flags16 : flags8; | |
0 | 598 } |
599 else | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
600 JSSWARNING(DMERR_NULLPTR, DMERR_NULLPTR, "Instrument #%d NULL!\n", instr); |
0 | 601 |
602 // Write to file | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
603 totalSize += sizeof(jssI); |
0 | 604 if (fwrite(&jssI, sizeof(jssI), 1, outFile) != 1) |
605 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
606 "Could not write JSSMOD instrument #%d to file!\n", instr); |
0 | 607 } |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
608 dmMsg(1," * %d Instrument headers, %d bytes.\n", m->ninstruments, totalSize); |
0 | 609 |
610 // Write sample data | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
611 for (totalSize = instr = 0; instr < m->ninstruments; instr++) |
0 | 612 if (m->instruments[instr]) |
613 { | |
614 JSSInstrument *inst = m->instruments[instr]; | |
615 if (inst->data != NULL) | |
616 { | |
617 size_t res; | |
618 if (inst->flags & jsf16bit) | |
619 { | |
620 jssEncodeSample16(inst->data, inst->size, flags16); | |
621 res = fwrite(inst->data, sizeof(Uint16), inst->size, outFile); | |
622 } | |
623 else | |
624 { | |
625 jssEncodeSample8(inst->data, inst->size, flags8); | |
626 res = fwrite(inst->data, sizeof(Uint8), inst->size, outFile); | |
627 } | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
628 |
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
629 totalSize += inst->size; |
86 | 630 if (res != (size_t) inst->size) |
0 | 631 JSSERROR(DMERR_FWRITE, DMERR_FWRITE, |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
632 "Could not write JSSMOD sample #%d to file!\n", instr); |
0 | 633 } |
634 } | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
635 dmMsg(1," * %d samples, %d bytes.\n", m->ninstruments, totalSize); |
0 | 636 |
637 return DMERR_OK; | |
638 } | |
639 | |
640 | |
641 /* Optimize a given module | |
642 */ | |
643 JSSModule *optimizeModule(JSSModule *m) | |
644 { | |
645 BOOL usedPatterns[jsetMaxPatterns + 1], | |
646 usedInstruments[jsetMaxInstruments + 1], | |
647 usedExtInstruments[jsetMaxInstruments + 1]; | |
648 int mapExtInstruments[jsetMaxInstruments + 1], | |
649 mapInstruments[jsetMaxInstruments + 1], | |
650 mapPatterns[jsetMaxPatterns + 1]; | |
651 JSSModule *r = NULL; | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
652 int i, n8, n16, unused; |
0 | 653 |
654 // Allocate a new module | |
655 if ((r = jssAllocateModule()) == NULL) | |
656 return NULL; | |
657 | |
658 // Copy things | |
659 r->moduleType = m->moduleType; | |
660 r->moduleName = dm_strdup(m->moduleName); | |
661 r->trackerName = dm_strdup(m->trackerName); | |
662 r->defSpeed = m->defSpeed; | |
663 r->defTempo = m->defTempo; | |
664 r->defFlags = m->defFlags; | |
665 r->defRestartPos = m->defRestartPos; | |
666 r->intVersion = m->intVersion; | |
667 r->nchannels = m->nchannels; | |
668 r->norders = m->norders; | |
669 for (i = 0; i < jsetNChannels; i++) | |
670 r->defPanning[i] = m->defPanning[i]; | |
671 | |
672 // Initialize values | |
673 for (i = 0; i <= jsetMaxInstruments; i++) | |
674 { | |
675 usedExtInstruments[i] = FALSE; | |
798 | 676 usedInstruments[i] = FALSE; |
677 mapExtInstruments[i] = jsetNotSet; | |
678 mapInstruments[i] = jsetNotSet; | |
0 | 679 } |
680 | |
681 for (i = 0; i <= jsetMaxPatterns; i++) | |
682 { | |
683 usedPatterns[i] = FALSE; | |
798 | 684 mapPatterns[i] = jsetNotSet; |
0 | 685 } |
686 | |
1094 | 687 // |
798 | 688 // Find out all actually used patterns and ext.instruments |
1100 | 689 // by going through all patterns specified in the order list |
1094 | 690 // |
0 | 691 for (i = 0; i < m->norders; i++) |
692 { | |
693 int pattern = m->orderList[i]; | |
694 if (pattern >= 0 && pattern < m->npatterns) | |
695 { | |
696 JSSPattern *p = m->patterns[pattern]; | |
697 if (p != NULL) | |
698 { | |
699 int row, channel; | |
700 JSSNote *n = p->data; | |
701 | |
1100 | 702 // Mark this pattern as used |
0 | 703 usedPatterns[pattern] = TRUE; |
704 | |
1100 | 705 // Check all notes in this pattern to see what instruments are used |
0 | 706 for (row = 0; row < p->nrows; row++) |
707 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
|
708 if (n->instrument != jsetNotSet) |
0 | 709 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
710 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
|
711 usedExtInstruments[n->instrument] = TRUE; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
712 else |
0 | 713 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
714 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
|
715 pattern, row, channel, n->instrument + 1); |
0 | 716 } |
717 } | |
718 } | |
719 else | |
720 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
721 dmErrorMsg("Pattern 0x%x is used on order 0x%x, but has no data!\n", |
1094 | 722 pattern, i); |
807
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
723 |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
724 // Fix it. |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
725 m->orderList[i] = jsetMaxPatterns; |
0 | 726 } |
727 } | |
728 else | |
729 if (pattern != jsetMaxPatterns) | |
730 { | |
1101 | 731 dmErrorMsg("Order 0x%x has invalid pattern number 0x%x, changing to empty!\n", |
1094 | 732 i, pattern); |
807
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
733 |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
734 // Fix it. |
d83dc7e47076
Fix some order list and pattern handling things.
Matti Hamalainen <ccr@tnsp.org>
parents:
798
diff
changeset
|
735 m->orderList[i] = jsetMaxPatterns; |
0 | 736 } |
737 } | |
738 | |
1094 | 739 // |
1100 | 740 // Find used sample instruments |
1094 | 741 // |
1101 | 742 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
|
743 for (i = 0; i < jsetMaxInstruments; i++) |
0 | 744 if (usedExtInstruments[i] && m->extInstruments[i] != NULL) |
745 { | |
746 int note; | |
747 JSSExtInstrument *e = m->extInstruments[i]; | |
1094 | 748 |
0 | 749 for (note = 0; note < jsetNNotes; note++) |
750 if (e->sNumForNotes[note] != jsetNotSet) | |
751 { | |
752 int q = e->sNumForNotes[note]; | |
753 if (q >= 0 && q < m->ninstruments) | |
754 { | |
755 usedInstruments[q] = TRUE; | |
756 } | |
757 else | |
758 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
759 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
|
760 i + 1, m->ninstruments, q + 1); |
0 | 761 } |
762 } | |
763 } | |
1094 | 764 |
765 // | |
0 | 766 // Create pattern mappings |
1094 | 767 // |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
768 dmMsg(1, "Creating pattern remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
769 for (unused = i = 0; i <= jsetMaxPatterns; i++) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
770 if (usedPatterns[i]) |
0 | 771 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
772 JSSPattern *pat; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
773 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
774 if (i >= m->npatterns) |
0 | 775 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
776 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
|
777 continue; |
0 | 778 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
779 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
780 if ((pat = m->patterns[i]) == NULL) |
0 | 781 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
782 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
|
783 continue; |
0 | 784 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
785 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
786 mapPatterns[i] = r->npatterns; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
787 r->patterns[r->npatterns] = m->patterns[i]; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
788 (r->npatterns)++; |
0 | 789 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
790 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
791 if (m->patterns[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
792 unused++; |
1094 | 793 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
794 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
|
795 r->npatterns, unused); |
0 | 796 |
797 | |
1094 | 798 // |
0 | 799 // Re-map instruments |
1094 | 800 // |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
801 dmMsg(1, "Creating sample instrument remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
802 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
|
803 if (usedInstruments[i]) |
0 | 804 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
805 JSSInstrument *ip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
806 if (optStripInstr) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
807 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
808 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
809 if (i >= m->ninstruments) |
0 | 810 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
811 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
|
812 i + 1, m->ninstruments); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
813 continue; |
0 | 814 } |
1097
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 ((ip = m->instruments[i]) == NULL) |
0 | 817 { |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
818 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
|
819 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
820 } |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
821 |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
822 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
|
823 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
824 mapInstruments[i] = r->ninstruments; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
825 r->instruments[r->ninstruments] = ip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
826 (r->ninstruments)++; |
0 | 827 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
828 if (ip->flags & jsf16bit) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
829 n16++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
830 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
831 n8++; |
0 | 832 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
833 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
834 if (m->instruments[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
835 unused++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
836 |
0 | 837 dmPrint(2, "\n"); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
838 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
|
839 n16, n8, r->ninstruments, unused); |
0 | 840 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
841 // |
0 | 842 // Re-map ext.instruments |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
843 // |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
844 dmMsg(1, "Creating ext.instrument remaps...\n"); |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
845 for (unused = i = 0; i < jsetMaxInstruments; i++) |
0 | 846 if (usedExtInstruments[i]) |
847 { | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
848 JSSExtInstrument *eip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
849 int note; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
850 if (optStripExtInstr) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
851 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
852 |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
853 if (i >= m->nextInstruments) |
0 | 854 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
855 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
|
856 i + 1, m->nextInstruments); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
857 continue; |
0 | 858 } |
1097
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 ((eip = m->extInstruments[i]) == NULL) |
0 | 861 { |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
862 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
|
863 continue; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
864 } |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
865 |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
866 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
|
867 |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
868 mapExtInstruments[i] = r->nextInstruments; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
869 r->extInstruments[r->nextInstruments] = eip; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
870 (r->nextInstruments)++; |
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 // 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
|
873 for (note = 0; note < jsetNNotes; note++) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
874 { |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
875 int q = eip->sNumForNotes[note]; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
876 if (q != jsetNotSet) |
0 | 877 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
878 int map; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
879 if (q >= 0 && q < jsetMaxInstruments) |
0 | 880 { |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
881 map = mapInstruments[q]; |
0 | 882 } |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
883 else |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
884 { |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
885 map = jsetNotSet; |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
886 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
|
887 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
|
888 } |
1111
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
889 |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
890 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
|
891 eip->sNumForNotes[note] = map; |
0 | 892 } |
893 } | |
894 } | |
895 else | |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
896 if (m->extInstruments[i] != NULL) |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
897 unused++; |
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
898 |
0 | 899 dmPrint(2, "\n"); |
1097
0e3cb9e61170
Code cleanup, in preparation for bug hunting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1094
diff
changeset
|
900 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
|
901 r->nextInstruments, unused); |
1094 | 902 |
903 // | |
904 // Remap pattern data with remapped instrument data | |
905 // | |
0 | 906 for (i = 0; i < r->npatterns; i++) |
907 { | |
908 int row, channel; | |
909 JSSPattern *p = r->patterns[i]; | |
910 JSSNote *n = p->data; | |
911 | |
912 for (row = 0; row < p->nrows; row++) | |
913 for (channel = 0; channel < p->nchannels; channel++, n++) | |
914 { | |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
915 char effect; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
916 |
1109 | 917 // If not stripping extended instruments, check for |
918 // 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
|
919 if (!optStripExtInstr) |
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
920 { |
856
415cc781e127
Fix some brain farts in instrument remapping.
Matti Hamalainen <ccr@tnsp.org>
parents:
807
diff
changeset
|
921 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
|
922 n->instrument = mapExtInstruments[n->instrument]; |
202
85614db5f577
Warn about invalid instruments.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
923 |
1116 | 924 if (n->instrument != jsetNotSet && |
925 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
|
926 { |
1116 | 927 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
|
928 n->instrument + 1); |
f9a96fc2a932
Adjust messages to show user-visible (1 based) instrument numbers.
Matti Hamalainen <ccr@tnsp.org>
parents:
1109
diff
changeset
|
929 } |
338
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
930 } |
cd57ba1130eb
Fixes and improvements in the JMOD saving and loading.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
931 |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
932 JMPGETEFFECT(effect, n->effect); |
1094 | 933 |
184
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
934 switch (effect) |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
935 { |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
936 case 'C': // Cxx = Set volume |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
937 if (n->volume == jsetNotSet) |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
938 { |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
939 n->volume = n->param; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
940 n->effect = jsetNotSet; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
941 n->param = jsetNotSet; |
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 break; |
50f55def91e5
Add a minor optimization in the conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
944 } |
0 | 945 } |
946 } | |
947 | |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
948 // |
0 | 949 // Remap orders list |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
950 // |
0 | 951 for (i = 0; i < m->norders; i++) |
952 { | |
953 r->orderList[i] = mapPatterns[m->orderList[i]]; | |
954 } | |
955 | |
956 return r; | |
957 } | |
958 | |
959 | |
960 int main(int argc, char *argv[]) | |
961 { | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
962 DMResource *inFile = NULL; |
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
963 FILE *outFile = NULL; |
0 | 964 JSSModule *sm, *dm; |
965 int result; | |
966 | |
967 dmInitProg("xm2jss", "XM to JSSMOD converter", "0.6", NULL, NULL); | |
968 dmVerbosity = 0; | |
969 | |
970 // Parse arguments | |
971 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
|
972 argHandleOpt, argHandleFile, OPTH_BAILOUT)) |
0 | 973 exit(1); |
974 | |
300 | 975 // Check arguments |
976 if (optInFilename == NULL || optOutFilename == NULL) | |
977 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
978 dmErrorMsg("Input or output file not specified. Try --help.\n"); |
300 | 979 return 1; |
980 } | |
0 | 981 |
982 // Read the source file | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
983 if ((result = dmf_create_stdio(optInFilename, "rb", &inFile)) != DMERR_OK) |
0 | 984 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
985 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
|
986 optInFilename, result, dmErrorStr(result)); |
0 | 987 return 1; |
988 } | |
989 | |
990 // Initialize miniJSS | |
991 jssInit(); | |
992 | |
993 // Read file | |
994 dmMsg(1, "Reading XM-format file ...\n"); | |
797
f066e9dccf29
Oops, fix some inverted booleans.
Matti Hamalainen <ccr@tnsp.org>
parents:
796
diff
changeset
|
995 result = jssLoadXM(inFile, &sm, FALSE); |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
996 dmf_close(inFile); |
0 | 997 if (result != 0) |
998 { | |
1195
4d27c4ec8d4d
Use %d instead of %i in formats.
Matti Hamalainen <ccr@tnsp.org>
parents:
1194
diff
changeset
|
999 dmErrorMsg("Error while loading XM file (%d), ", result); |
0 | 1000 if (optIgnoreErrors) |
1001 fprintf(stderr, "ignoring. This may cause problems.\n"); | |
1002 else | |
1003 { | |
1004 fprintf(stderr, "giving up. Use --ignore if you want to try to convert anyway.\n"); | |
1005 return 2; | |
1006 } | |
1007 } | |
1008 | |
1009 // Check stripping settings | |
1010 if (optStripExtInstr) optStripInstr = TRUE; | |
1011 if (optStripInstr) optStripSamples = TRUE; | |
1012 | |
1013 // Remove samples | |
1014 if (optStripSamples) | |
1015 { | |
1016 int i; | |
1017 | |
1018 dmMsg(1, "Stripping samples...\n"); | |
1019 for (i = 0; i < sm->ninstruments; i++) | |
1020 { | |
1021 dmFree(sm->instruments[i]->data); | |
1022 sm->instruments[i]->data = NULL; | |
1023 } | |
1024 } | |
1025 | |
1026 // Remove instruments | |
1027 if (optStripInstr) | |
1028 { | |
1029 int i; | |
1030 | |
1031 dmMsg(1, "Stripping instruments...\n"); | |
1032 for (i = 0; i < sm->ninstruments; i++) | |
1033 { | |
1034 dmFree(sm->instruments[i]); | |
1035 sm->instruments[i] = NULL; | |
1036 } | |
1037 sm->ninstruments = 0; | |
1038 } | |
1039 | |
1040 // Remove ext.instruments | |
1041 if (optStripExtInstr) | |
1042 { | |
1043 int i; | |
1044 | |
1045 dmMsg(1, "Stripping ext.instruments...\n"); | |
1046 for (i = 0; i < sm->nextInstruments; i++) | |
1047 { | |
1048 dmFree(sm->extInstruments[i]); | |
1049 sm->extInstruments[i] = NULL; | |
1050 } | |
1051 sm->nextInstruments = 0; | |
1052 } | |
1109 | 1053 |
0 | 1054 // Run the optimization procedure |
1055 if (optOptimize) | |
1056 { | |
1057 dmMsg(1, "Optimizing module data...\n"); | |
1058 dm = optimizeModule(sm); | |
1059 } else | |
1060 dm = sm; | |
1061 | |
1062 // Write output file | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1063 if ((outFile = fopen(optOutFilename, "wb")) == NULL) |
0 | 1064 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
860
diff
changeset
|
1065 dmErrorMsg("Error creating output file '%s', %d: %s\n", |
300 | 1066 optOutFilename, errno, strerror(errno)); |
0 | 1067 return 1; |
1068 } | |
1069 | |
9
c42ee907de9c
Various improvements in xm2jss output.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1070 dmMsg(1, "Writing JSSMOD-format file [patMode=0x%04x, samp8=0x%02x, samp16=0x%02x]\n", |
0 | 1071 optPatternMode, optSampMode8, optSampMode16); |
1072 | |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1073 result = jssSaveJSSMOD(outFile, dm, optPatternMode, optSampMode8, optSampMode16); |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1074 |
1114
7aaab1533c90
Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents:
1113
diff
changeset
|
1075 dmFree(sm); |
7aaab1533c90
Free the source module data at end.
Matti Hamalainen <ccr@tnsp.org>
parents:
1113
diff
changeset
|
1076 |
730
3d813c81f33c
More work on resources API.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
1077 fclose(outFile); |
0 | 1078 |
1079 if (result != 0) | |
1080 { | |
1113
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1081 dmErrorMsg( |
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1082 "Error while saving JSSMOD file, %d: %s\n" |
28dcf10fb8a9
Comments, cosmetics, combine one error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
1111
diff
changeset
|
1083 "WARNING: The resulting file may be broken!\n", |
300 | 1084 result, dmErrorStr(result)); |
0 | 1085 } |
300 | 1086 else |
1087 { | |
1088 dmMsg(1, "Conversion complete.\n"); | |
1089 } | |
0 | 1090 return 0; |
1091 } |