comparison minijss/jloadxm.c @ 1183:ae17e0a004f4

Move jssXMLoadPatterns() closer to the pattern unpacking functions.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Mar 2015 11:34:07 +0200
parents 9d32da6ba785
children d144ebb1cdea
comparison
equal deleted inserted replaced
1182:9d32da6ba785 1183:ae17e0a004f4
232 232
233 return DMERR_OK; 233 return DMERR_OK;
234 } 234 }
235 235
236 236
237 static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH)
238 {
239 int index, result;
240 XMPattern xmP;
241
242 for (index = 0; index < module->npatterns; index++)
243 {
244 // Get the pattern header
245 off_t remainder, pos = dmftell(inFile);
246
247 if (!dmf_read_le32(inFile, &xmP.headSize) ||
248 !dmf_read_byte(inFile, &xmP.packing) ||
249 !dmf_read_le16(inFile, &xmP.nrows) ||
250 !dmf_read_le16(inFile, &xmP.size))
251 JSSERROR(DMERR_FREAD, DMERR_FREAD,
252 "Could not read pattern header data #%d.\n",
253 index);
254
255
256 // Check the header
257 if (xmP.packing != 0)
258 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
259 "Pattern #%d packing type unsupported (%d)\n",
260 index, xmP.packing);
261
262 if (xmP.nrows == 0)
263 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
264 "Pattern #%d has %d rows, invalid data.\n",
265 index, xmP.nrows);
266
267 if (xmP.size > 0)
268 {
269 // Allocate and unpack pattern
270 module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels);
271 if (module->patterns[index] == NULL)
272 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
273 "Could not allocate memory for pattern #%d\n", index);
274
275 switch (module->intVersion)
276 {
277 case 0x0104:
278 result = jssXMUnpackPattern104(inFile, xmP.size, module->patterns[index]);
279 break;
280 case 0x0102:
281 result = jssXMUnpackPattern102(inFile, xmP.size, module->patterns[index]);
282 break;
283 }
284
285 if (result != 0)
286 JSSERROR(result, result, "Error in unpacking pattern #%d data\n", index);
287 }
288
289 // Skip extra data (if the file is damaged)
290 remainder = xmP.headSize - (dmftell(inFile) - pos);
291 if (remainder > 0)
292 {
293 JSSDEBUG("xmP Skipping: %li\n", remainder);
294 dmfseek(inFile, remainder, SEEK_CUR);
295 }
296 }
297
298 // Allocate the empty pattern
299 module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels);
300
301 /* Convert song orders list by replacing nonexisting patterns
302 * with pattern number jsetMaxPatterns.
303 */
304 for (index = 0; index < module->norders; index++)
305 {
306 int tmp = xmH->orderList[index];
307 if (tmp >= module->npatterns ||
308 module->patterns[tmp] == NULL)
309 tmp = jsetMaxPatterns;
310
311 module->orderList[index] = tmp;
312 }
313
314 return DMERR_OK;
315 }
316
317
237 /* Convert XM envelope structure to JSS envelope structure 318 /* Convert XM envelope structure to JSS envelope structure
238 */ 319 */
239 static int jssXMConvertEnvelope( 320 static int jssXMConvertEnvelope(
240 JSSEnvelope *dst, XMEnvelope *src, 321 JSSEnvelope *dst, XMEnvelope *src,
241 const char *name, const int ninstr) 322 const char *name, const int ninstr)
665 // Read sample data if needed 746 // Read sample data if needed
666 if (module->intVersion == 0x0104) 747 if (module->intVersion == 0x0104)
667 { 748 {
668 if ((ret = jssXMLoadInstrumentSamples(inFile, module, einst, ninst)) != DMERR_OK) 749 if ((ret = jssXMLoadInstrumentSamples(inFile, module, einst, ninst)) != DMERR_OK)
669 return ret; 750 return ret;
670 }
671
672 return DMERR_OK;
673 }
674
675
676 static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH)
677 {
678 int index, result;
679 XMPattern xmP;
680
681 for (index = 0; index < module->npatterns; index++)
682 {
683 // Get the pattern header
684 off_t remainder, pos = dmftell(inFile);
685
686 if (!dmf_read_le32(inFile, &xmP.headSize) ||
687 !dmf_read_byte(inFile, &xmP.packing) ||
688 !dmf_read_le16(inFile, &xmP.nrows) ||
689 !dmf_read_le16(inFile, &xmP.size))
690 JSSERROR(DMERR_FREAD, DMERR_FREAD,
691 "Could not read pattern header data #%d.\n",
692 index);
693
694
695 // Check the header
696 if (xmP.packing != 0)
697 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
698 "Pattern #%d packing type unsupported (%d)\n",
699 index, xmP.packing);
700
701 if (xmP.nrows == 0)
702 JSSERROR(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
703 "Pattern #%d has %d rows, invalid data.\n",
704 index, xmP.nrows);
705
706 if (xmP.size > 0)
707 {
708 // Allocate and unpack pattern
709 module->patterns[index] = jssAllocatePattern(xmP.nrows, module->nchannels);
710 if (module->patterns[index] == NULL)
711 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
712 "Could not allocate memory for pattern #%d\n", index);
713
714 switch (module->intVersion)
715 {
716 case 0x0104:
717 result = jssXMUnpackPattern104(inFile, xmP.size, module->patterns[index]);
718 break;
719 case 0x0102:
720 result = jssXMUnpackPattern102(inFile, xmP.size, module->patterns[index]);
721 break;
722 }
723
724 if (result != 0)
725 JSSERROR(result, result, "Error in unpacking pattern #%d data\n", index);
726 }
727
728 // Skip extra data (if the file is damaged)
729 remainder = xmP.headSize - (dmftell(inFile) - pos);
730 if (remainder > 0)
731 {
732 JSSDEBUG("xmP Skipping: %li\n", remainder);
733 dmfseek(inFile, remainder, SEEK_CUR);
734 }
735 }
736
737 // Allocate the empty pattern
738 module->patterns[jsetMaxPatterns] = jssAllocatePattern(64, module->nchannels);
739
740 /* Convert song orders list by replacing nonexisting patterns
741 * with pattern number jsetMaxPatterns.
742 */
743 for (index = 0; index < module->norders; index++)
744 {
745 int tmp = xmH->orderList[index];
746 if (tmp >= module->npatterns ||
747 module->patterns[tmp] == NULL)
748 tmp = jsetMaxPatterns;
749
750 module->orderList[index] = tmp;
751 } 751 }
752 752
753 return DMERR_OK; 753 return DMERR_OK;
754 } 754 }
755 755