Mercurial > hg > dmlib
annotate jloadjss.c @ 96:6bf5220fa47e
Urgh .. use memset to silence some bogus GCC warnings about using
potentially uninitialized values, while that will not actually be possible.
In any case, it is annoying.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 02 Oct 2012 18:52:28 +0300 |
parents | 32250b436bca |
children | d3a9a3804079 |
rev | line source |
---|---|
0 | 1 /* |
2 * miniJSS - JSSMOD module loader | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2007-2009 Tecnic Software productions (TNSP) | |
5 */ | |
6 #include "jssmod.h" | |
7 #include <string.h> | |
8 | |
9 | |
10 #ifdef JM_SUP_PATMODE_ALL | |
11 #define JM_SUP_PATMODE_1 1 | |
12 #define JM_SUP_PATMODE_2 1 | |
13 #define JM_SUP_PATMODE_3 1 | |
14 #define JM_SUP_PATMODE_4 1 | |
15 #define JM_SUP_PATMODE_5 1 | |
16 #endif | |
17 | |
18 | |
19 static BOOL jsGetBufData(Uint8 **buf, size_t *bufLeft, void *data, const size_t dataSize) | |
20 { | |
21 if (*bufLeft >= dataSize) | |
22 { | |
23 memcpy(data, *buf, dataSize); | |
24 *buf += dataSize; | |
25 *bufLeft -= dataSize; | |
26 return TRUE; | |
27 } | |
28 else | |
29 return FALSE; | |
30 } | |
31 | |
32 | |
33 static BOOL jsGetBufByte(Uint8 **buf, size_t *bufLeft, Uint8 *data) | |
34 { | |
35 if (*bufLeft > 0) | |
36 { | |
37 *data = **buf; | |
38 (*buf)++; | |
39 (*bufLeft)--; | |
40 return TRUE; | |
41 } | |
42 else | |
43 return FALSE; | |
44 } | |
45 | |
46 | |
47 #define JSGETBUF(XV, XT) if (!jsGetBufData(buf, bufLeft, XV, sizeof(XT))) return DMERR_OUT_OF_DATA | |
48 #define JSGETBYTE(XV) if (!jsGetBufByte(buf, bufLeft, XV)) return DMERR_OUT_OF_DATA | |
49 | |
50 | |
51 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_1) || defined(JM_SUP_PATMODE_3) | |
52 static int jssGetConvertedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) | |
53 { | |
54 Uint8 tmp; | |
55 | |
56 JSGETBYTE(&tmp); | |
57 | |
58 if (tmp == 127) | |
59 note->note = jsetNoteOff; | |
60 else if (tmp == 0) | |
61 note->note = jsetNotSet; | |
62 else | |
63 note->note = tmp - 1; | |
64 | |
65 JSGETBYTE(&tmp); | |
66 note->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
67 | |
68 JSGETBYTE(&tmp); | |
69 note->volume = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
70 | |
71 JSGETBYTE(&tmp); | |
72 note->effect = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
73 | |
74 JSGETBYTE(&tmp); | |
75 note->param = (tmp == 0 && note->effect == jsetNotSet) ? jsetNotSet : tmp; | |
76 | |
77 return DMERR_OK; | |
78 } | |
79 #endif | |
80 | |
81 | |
82 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_2) || defined(JM_SUP_PATMODE_4) | |
83 static int jssGetCompressedNote(Uint8 **buf, size_t *bufLeft, JSSNote *note) | |
84 { | |
85 Uint8 packb, tmp; | |
86 | |
87 JSGETBYTE(&packb); | |
88 if (packb & 0x80) | |
89 { | |
90 if (packb & COMP_NOTE) | |
91 { | |
92 JSGETBYTE(&tmp); | |
93 if (tmp == 127) | |
94 note->note = jsetNoteOff; | |
95 else | |
96 note->note = tmp; | |
97 } | |
98 | |
99 if (packb & COMP_INSTRUMENT) | |
100 { | |
101 JSGETBYTE(&tmp); | |
102 note->instrument = tmp; | |
103 } | |
104 | |
105 if (packb & COMP_VOLUME) | |
106 { | |
107 JSGETBYTE(&tmp); | |
108 note->volume = tmp; | |
109 } | |
110 | |
111 if (packb & COMP_EFFECT) | |
112 { | |
113 JSGETBYTE(&tmp); | |
114 note->effect = tmp; | |
115 note->param = 0; | |
116 } | |
117 | |
118 if (packb & COMP_PARAM) | |
119 { | |
120 JSGETBYTE(&tmp); | |
121 note->param = tmp; | |
122 } | |
123 } | |
124 else | |
125 { | |
126 tmp = packb; | |
127 | |
128 if (tmp == 127) | |
129 note->note = jsetNoteOff; | |
130 else if (tmp == 0) | |
131 note->note = jsetNotSet; | |
132 else | |
133 note->note = tmp - 1; | |
134 | |
135 JSGETBYTE(&tmp); | |
136 note->instrument = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
137 | |
138 JSGETBYTE(&tmp); | |
139 note->volume = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
140 | |
141 JSGETBYTE(&tmp); | |
142 note->effect = (tmp > 0) ? tmp - 1 : jsetNotSet; | |
143 | |
144 JSGETBYTE(&tmp); | |
145 note->param = (tmp == 0 && note->effect == jsetNotSet) ? jsetNotSet : tmp; | |
146 } | |
147 | |
148 return DMERR_OK; | |
149 } | |
150 #endif | |
151 | |
152 | |
153 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_2) | |
154 static int jssGetPatternCompHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) | |
155 { | |
156 int row, channel; | |
157 | |
158 assert(buf != NULL); | |
159 assert(pattern != NULL); | |
160 | |
161 for (row = 0; row < pattern->nrows; row++) | |
162 for (channel = 0; channel < pattern->nchannels; channel++) | |
163 { | |
164 JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
165 int res = jssGetCompressedNote(&buf, bufLeft, note); | |
166 if (res != DMERR_OK) | |
167 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); | |
168 } | |
169 | |
170 return DMERR_OK; | |
171 } | |
172 #endif | |
173 | |
174 | |
175 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_4) | |
176 static int jssGetPatternCompVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) | |
177 { | |
178 int row, channel; | |
179 | |
180 assert(buf != NULL); | |
181 assert(pattern != NULL); | |
182 | |
183 for (channel = 0; channel < pattern->nchannels; channel++) | |
184 for (row = 0; row < pattern->nrows; row++) | |
185 { | |
186 JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
187 int res = jssGetCompressedNote(&buf, bufLeft, note); | |
188 if (res != DMERR_OK) | |
189 JSSERROR(res, res, "Error uncompressing note on row=%d, chn=%d\n", row, channel); | |
190 } | |
191 | |
192 return DMERR_OK; | |
193 } | |
194 #endif | |
195 | |
196 | |
197 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_1) | |
198 static int jssGetPatternRawHoriz(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) | |
199 { | |
200 int row, channel; | |
201 | |
202 assert(buf != NULL); | |
203 assert(pattern != NULL); | |
204 | |
205 for (row = 0; row < pattern->nrows; row++) | |
206 for (channel = 0; channel < pattern->nchannels; channel++) | |
207 { | |
208 JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
209 int res = jssGetConvertedNote(&buf, bufLeft, note); | |
210 if (res != DMERR_OK) | |
211 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); | |
212 } | |
213 | |
214 return DMERR_OK; | |
215 } | |
216 #endif | |
217 | |
218 | |
219 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_3) | |
220 static int jssGetPatternRawVert(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) | |
221 { | |
222 int row, channel; | |
223 | |
224 assert(buf != NULL); | |
225 assert(pattern != NULL); | |
226 | |
227 for (channel = 0; channel < pattern->nchannels; channel++) | |
228 for (row = 0; row < pattern->nrows; row++) | |
229 { | |
230 JSSNote *note = &pattern->data[(pattern->nchannels * row) + channel]; | |
231 int res = jssGetConvertedNote(&buf, bufLeft, note); | |
232 if (res != DMERR_OK) | |
233 JSSERROR(res, res, "Error converting note on row=%d, chn=%d\n", row, channel); | |
234 } | |
235 | |
236 return DMERR_OK; | |
237 } | |
238 #endif | |
239 | |
240 | |
241 #if defined(JM_SUP_PATMODE_ALL) || defined(JM_SUP_PATMODE_5) | |
242 static int jssGetPatternRawVertElem(Uint8 *buf, size_t *bufLeft, JSSPattern *pattern) | |
243 { | |
244 int row, channel; | |
245 | |
246 assert(buf != NULL); | |
247 assert(pattern != NULL); | |
248 | |
249 return DMERR_OK; | |
250 } | |
251 #endif | |
252 | |
253 | |
254 #undef JSGETBUF | |
255 #undef JSGETBYTE | |
256 #define JSGETBUF(XV, XT) do { \ | |
257 if (!jsGetBufData(&buf, &bufLeft, XV, sizeof(XT))) \ | |
258 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, \ | |
259 "Out of data at getting " # XT " (%d bytes)\n", sizeof(XT)); \ | |
260 } while (0) | |
261 #define JSGETBYTE(XV) if (!jsGetBufByte(&buf, &bufLeft, XV)) return DMERR_OUT_OF_DATA | |
262 | |
263 | |
264 #ifdef JM_SUP_EXT_INSTR | |
265 static void jssCopyEnvelope(JSSEnvelope *e, JSSMODEnvelope *je) | |
266 { | |
267 int i; | |
268 | |
269 e->flags = je->flags; | |
270 e->npoints = je->npoints; | |
271 e->sustain = je->sustain; | |
272 e->loopS = je->loopS; | |
273 e->loopE = je->loopE; | |
274 | |
275 for (i = 0; i < je->npoints; i++) | |
276 { | |
277 e->points[i].frame = je->points[i].frame; | |
278 e->points[i].value = je->points[i].value; | |
279 } | |
280 } | |
281 #endif | |
282 | |
283 | |
284 int jssLoadJSSMOD(Uint8 *bufStart, const size_t bufSize, JSSModule **ppModule) | |
285 { | |
286 JSSModule *module; | |
287 JSSMODHeader jssH; | |
288 Uint8 *buf = bufStart; | |
289 size_t bufLeft = bufSize; | |
290 int index; | |
291 | |
292 assert(ppModule != NULL); | |
293 assert(bufStart != NULL); | |
294 *ppModule = NULL; | |
295 | |
296 // Check the JSSMOD header | |
96
6bf5220fa47e
Urgh .. use memset to silence some bogus GCC warnings about using
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
297 memset(&jssH, 0, sizeof(jssH)); |
0 | 298 JSGETBUF(&jssH, JSSMODHeader); |
299 | |
300 if (memcmp(jssH.idMagic, "JM", 2) != 0) | |
301 { | |
302 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
303 "Not a valid JSSMOD file, header signature missing!\n"); | |
304 } | |
305 | |
306 if (jssH.idVersion != JSSMOD_VERSION) | |
307 { | |
308 JSSERROR(DMERR_VERSION, DMERR_VERSION, | |
309 "Unsupported version of JSSMOD 0x%4x, this version only supports 0x%4x!\n", | |
310 jssH.idVersion, JSSMOD_VERSION); | |
311 } | |
312 | |
313 // Allocate the module | |
314 module = jssAllocateModule(); | |
315 if (module == NULL) | |
316 { | |
317 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
318 "Could not allocate memory for module structure.\n"); | |
319 } | |
320 *ppModule = module; | |
321 | |
322 // Copy header information | |
323 module->norders = jssH.norders; | |
324 module->npatterns = jssH.npatterns; | |
325 module->nchannels = jssH.nchannels; | |
326 module->nextInstruments = jssH.nextInstruments; | |
327 module->ninstruments = jssH.ninstruments; | |
328 module->defFlags = jssH.defFlags; | |
329 module->intVersion = jssH.intVersion; | |
330 module->defRestartPos = jssH.defRestartPos; | |
331 module->defSpeed = jssH.defSpeed; | |
332 module->defTempo = jssH.defTempo; | |
333 | |
334 // Get the orders list | |
335 for (index = 0; index < module->norders; index++) | |
336 { | |
337 Sint16 order; | |
338 JSGETBUF(&order, Sint16); | |
339 module->orderList[index] = order; | |
340 } | |
341 | |
342 // Parse the patterns | |
343 for (index = 0; index < module->npatterns; index++) | |
344 { | |
345 JSSMODPattern jssP; | |
346 int result = DMERR_INVALID_DATA; | |
347 size_t bufSize; | |
348 | |
349 // Get header and check size | |
96
6bf5220fa47e
Urgh .. use memset to silence some bogus GCC warnings about using
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
350 memset(&jssP, 0, sizeof(jssP)); |
0 | 351 JSGETBUF(&jssP, JSSMODPattern); |
352 bufSize = jssP.size; | |
353 if (bufLeft < jssP.size) | |
354 { | |
355 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, | |
356 "Out of data for pattern #%d.\n", index); | |
357 } | |
358 | |
359 // Allocate pattern | |
360 module->patterns[index] = jssAllocatePattern(jssP.nrows, module->nchannels); | |
361 if (module->patterns[index] == NULL) | |
362 { | |
363 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
364 "Could not allocate memory for pattern #%d.\n", index); | |
365 } | |
366 | |
367 // Get pattern data | |
368 switch (jssH.patMode) | |
369 { | |
370 #ifdef JM_SUP_PATMODE_1 | |
371 case PATMODE_RAW_HORIZ: | |
372 result = jssGetPatternRawHoriz(buf, &bufSize, module->patterns[index]); | |
373 break; | |
374 #endif | |
375 #ifdef JM_SUP_PATMODE_2 | |
376 case PATMODE_COMP_HORIZ: | |
377 result = jssGetPatternCompHoriz(buf, &bufSize, module->patterns[index]); | |
378 break; | |
379 #endif | |
380 #ifdef JM_SUP_PATMODE_3 | |
381 case PATMODE_RAW_VERT: | |
382 result = jssGetPatternRawVert(buf, &bufSize, module->patterns[index]); | |
383 break; | |
384 #endif | |
385 #ifdef JM_SUP_PATMODE_4 | |
386 case PATMODE_COMP_VERT: | |
387 result = jssGetPatternCompVert(buf, &bufSize, module->patterns[index]); | |
388 break; | |
389 #endif | |
390 #ifdef JM_SUP_PATMODE_5 | |
391 case PATMODE_RAW_ELEM: | |
392 result = jssGetPatternRawVertElem(buf, &bufSize, module->patterns[index]); | |
393 break; | |
394 #endif | |
395 default: | |
396 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | |
397 "Unsupported pattern mode %d. Check compilation options.", jssH.patMode); | |
398 break; | |
399 } | |
400 | |
401 if (bufSize > 0) | |
402 { | |
403 JSSWARNING(DMERR_EXTRA_DATA, DMERR_EXTRA_DATA, | |
404 "Unparsed data after pattern (%d bytes), possibly broken file.\n", bufSize); | |
405 } | |
406 | |
407 if (result != DMERR_OK) | |
408 { | |
409 JSSERROR(result, result, "Error in unpacking pattern #%i data\n", index); | |
410 } | |
411 | |
412 buf += jssP.size; | |
413 bufLeft -= jssP.size; | |
414 } | |
415 | |
416 #ifdef JM_SUP_EXT_INSTR | |
417 // Read extended instruments | |
418 for (index = 0; index < module->nextInstruments; index++) | |
419 { | |
420 JSSMODExtInstrument jssE; | |
421 JSSExtInstrument *einst; | |
422 int i; | |
423 | |
96
6bf5220fa47e
Urgh .. use memset to silence some bogus GCC warnings about using
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
424 memset(&jssE, 0, sizeof(jssE)); |
0 | 425 JSGETBUF(&jssE, JSSMODExtInstrument); |
426 | |
427 if ((einst = jssAllocateExtInstrument()) == NULL) | |
428 { | |
429 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
430 "Could not allocate extended instrument structure #%i\n", index); | |
431 } | |
432 | |
433 module->extInstruments[index] = einst; | |
434 | |
435 einst->nsamples = jssE.nsamples; | |
436 einst->vibratoType = jssE.vibratoType; | |
437 einst->vibratoSweep = jssE.vibratoSweep; | |
438 einst->vibratoDepth = jssE.vibratoDepth; | |
439 einst->vibratoRate = jssE.vibratoRate; | |
440 einst->fadeOut = jssE.fadeOut; | |
441 | |
442 for (i = 0; i < jsetNNotes; i++) | |
443 { | |
444 int snum = jssE.sNumForNotes[i]; | |
445 einst->sNumForNotes[i] = (snum > 0) ? snum : jsetNotSet; | |
446 } | |
447 | |
448 jssCopyEnvelope(&(einst->volumeEnv), &jssE.volumeEnv); | |
449 jssCopyEnvelope(&(einst->panningEnv), &jssE.panningEnv); | |
450 } | |
451 | |
452 #ifdef JM_SUP_INSTR | |
453 // Read sample instrument headers | |
454 for (index = 0; index < module->ninstruments; index++) | |
455 { | |
456 JSSMODInstrument jssI; | |
457 JSSInstrument *inst; | |
458 | |
96
6bf5220fa47e
Urgh .. use memset to silence some bogus GCC warnings about using
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
459 memset(&jssI, 0, sizeof(jssI)); |
0 | 460 JSGETBUF(&jssI, JSSMODInstrument); |
461 | |
462 if ((inst = jssAllocateInstrument()) == NULL) | |
463 { | |
464 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
465 "Could not allocate instrument structure #%i\n", index); | |
466 } | |
467 | |
468 module->instruments[index] = inst; | |
469 | |
470 inst->size = jssI.size; | |
471 inst->loopS = jssI.loopS; | |
472 inst->loopE = jssI.loopE; | |
473 inst->volume = jssI.volume; | |
474 inst->flags = jssI.flags; | |
475 inst->C4BaseSpeed = jssI.C4BaseSpeed; | |
476 inst->ERelNote = jssI.ERelNote; | |
477 inst->EFineTune = jssI.EFineTune; | |
478 inst->EPanning = jssI.EPanning; | |
479 inst->hasData = jssI.hasData; | |
480 inst->convFlags = jssI.convFlags; | |
481 } | |
482 | |
483 #ifdef JM_SUP_SAMPLES | |
484 // Read sample data | |
485 for (index = 0; index < module->ninstruments; index++) | |
486 { | |
487 JSSInstrument *inst = module->instruments[index]; | |
488 | |
489 if (inst && inst->hasData) | |
490 { | |
491 size_t sz; | |
492 | |
493 // Calculate data size | |
494 if (inst->flags & jsf16bit) | |
495 sz = inst->size * sizeof(Uint16); | |
496 else | |
497 sz = inst->size * sizeof(Uint8); | |
498 | |
499 // Check if we can get as much? | |
500 if (bufLeft < sz) | |
501 { | |
502 JSSERROR(DMERR_OUT_OF_DATA, DMERR_OUT_OF_DATA, | |
503 "Out of data for instrument sample #%d (%d < %d)\n", | |
504 index, bufLeft, sz); | |
505 } | |
506 | |
507 // Allocate | |
508 if ((inst->data = dmMalloc(sz)) == NULL) | |
509 { | |
510 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | |
511 "Could not allocate sample data #%d\n", index); | |
512 } | |
513 | |
514 // Copy data | |
515 memcpy(inst->data, buf, sz); | |
516 buf += sz; | |
517 bufLeft -= sz; | |
518 | |
519 // Convert, if needed | |
520 if (inst->flags & jsf16bit) | |
521 jssDecodeSample16(inst->data, inst->size, inst->convFlags); | |
522 else | |
523 jssDecodeSample8(inst->data, inst->size, inst->convFlags); | |
524 } | |
525 } | |
526 #else | |
527 #warning Not including JSSMOD sample loading! | |
528 #endif // JM_SUP_SAMPLES | |
529 #else | |
530 #warning Not including JSSMOD instrument loading! | |
531 #endif // JM_SUP_INSTR | |
532 #else | |
533 #warning Not including JSSMOD ext.instrument loading! | |
534 #endif // JM_SUP_EXT_INSTR | |
535 | |
536 return DMERR_OK; | |
537 } |