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