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