Mercurial > hg > dmlib
comparison minijss/jloadxm.c @ 1176:afdbf62ce8ad
Revamp instrument sample loading somewhat.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 05 Mar 2015 09:57:02 +0200 |
parents | 165c673ee012 |
children | dc26df8b642c |
comparison
equal
deleted
inserted
replaced
1175:165c673ee012 | 1176:afdbf62ce8ad |
---|---|
29 { | 29 { |
30 char idMagic[17]; // XM header ID "Extended Module: " | 30 char idMagic[17]; // XM header ID "Extended Module: " |
31 char songName[20]; // Module song name | 31 char songName[20]; // Module song name |
32 Uint8 unUsed1A; // ALWAYS 0x1a | 32 Uint8 unUsed1A; // ALWAYS 0x1a |
33 char trackerName[20]; // ID-string of tracker software | 33 char trackerName[20]; // ID-string of tracker software |
34 Uint16 version; // XM-version 0x0104 | 34 Uint16 version; // XM-version tag |
35 Uint32 headSize; // Module header size, FROM THIS POINT! | 35 Uint32 headSize; // Module header size, FROM THIS POINT! |
36 Uint16 norders, // Number of orders | 36 Uint16 norders, // Number of orders |
37 defRestartPos, // Default song restart position | 37 defRestartPos, // Default song restart position |
38 nchannels, // Number of channels | 38 nchannels, // Number of channels |
39 npatterns, // Number of patterns | 39 npatterns, // Number of patterns |
294 | 294 |
295 return DMERR_OK; | 295 return DMERR_OK; |
296 } | 296 } |
297 | 297 |
298 | 298 |
299 static int jssXMLoadInstrument(DMResource *inFile, JSSModule *module, int ninst, int nsample, int *xmConvTable) | 299 static int jssXMLoadSampleInstrument( |
300 DMResource *inFile, JSSModule *module, | |
301 JSSExtInstrument *einst, int ninst, int nsample) | |
300 { | 302 { |
301 XMSample xmS; | 303 XMSample xmS; |
302 JSSInstrument *inst; | 304 JSSInstrument *inst; |
303 | 305 |
304 // Read header data | 306 // Read header data |
323 | 325 |
324 // Allocate sample instrument | 326 // Allocate sample instrument |
325 JSSDEBUG("Allocating sample #%d/%d [%d]\n", | 327 JSSDEBUG("Allocating sample #%d/%d [%d]\n", |
326 ninst, nsample, module->ninstruments); | 328 ninst, nsample, module->ninstruments); |
327 | 329 |
328 xmConvTable[nsample] = module->ninstruments; | 330 einst->instConvTable[nsample] = module->ninstruments; |
329 inst = module->instruments[module->ninstruments] = jssAllocateInstrument(); | 331 inst = module->instruments[module->ninstruments] = jssAllocateInstrument(); |
330 if (inst == NULL) | 332 if (inst == NULL) |
331 { | 333 { |
332 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, | 334 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, |
333 "Could not allocate sample #%d/%d [%d]\n", | 335 "Could not allocate sample #%d/%d [%d]\n", |
459 { | 461 { |
460 ret = jssDecodeSample8( | 462 ret = jssDecodeSample8( |
461 (Uint8 *) inst->data, inst->size, | 463 (Uint8 *) inst->data, inst->size, |
462 (jsampDelta | jsampFlipSign)); | 464 (jsampDelta | jsampFlipSign)); |
463 } | 465 } |
466 | |
464 return ret; | 467 return ret; |
468 } | |
469 | |
470 | |
471 static int jssXMLoadInstrumentSamples( | |
472 DMResource *inFile, JSSModule *module, | |
473 JSSExtInstrument *einst, int ninst) | |
474 { | |
475 int nsample, ret; | |
476 | |
477 for (nsample = 0; nsample < einst->nsamples; nsample++) | |
478 if (einst->instConvTable[nsample] != jsetNotSet) | |
479 { | |
480 JSSInstrument *inst = module->instruments[einst->instConvTable[nsample]]; | |
481 if ((ret = jssXMLoadSampleData(inFile, inst, ninst, nsample)) != DMERR_OK) | |
482 return ret; | |
483 } | |
484 | |
485 return DMERR_OK; | |
465 } | 486 } |
466 | 487 |
467 | 488 |
468 static BOOL jssXMLoadEnvelopePoints(DMResource *inFile, XMEnvelope *env) | 489 static BOOL jssXMLoadEnvelopePoints(DMResource *inFile, XMEnvelope *env) |
469 { | 490 { |
491 */ | 512 */ |
492 static int jssXMLoadExtInstrument(DMResource *inFile, int ninst, JSSModule *module) | 513 static int jssXMLoadExtInstrument(DMResource *inFile, int ninst, JSSModule *module) |
493 { | 514 { |
494 XMInstrument1 xmI1; | 515 XMInstrument1 xmI1; |
495 off_t remainder, pos = dmftell(inFile); | 516 off_t remainder, pos = dmftell(inFile); |
496 int xmConvTable[XM_MaxInstruments + 1]; | |
497 JSSExtInstrument *einst; | 517 JSSExtInstrument *einst; |
498 XMInstrument2 xmI2; | 518 XMInstrument2 xmI2; |
499 int i, nsample, ret; | 519 int i, nsample, ret; |
500 | 520 |
501 // Get instrument header #1 | 521 // Get instrument header #1 |
599 einst->fadeOut = xmI2.fadeOut; | 619 einst->fadeOut = xmI2.fadeOut; |
600 einst->nsamples = xmI1.nsamples; | 620 einst->nsamples = xmI1.nsamples; |
601 | 621 |
602 // Initialize the SNumForNotes conversion table | 622 // Initialize the SNumForNotes conversion table |
603 for (i = 0; i < XM_MaxInstruments; i++) | 623 for (i = 0; i < XM_MaxInstruments; i++) |
604 xmConvTable[i] = jsetNotSet; | 624 einst->instConvTable[i] = jsetNotSet; |
605 | 625 |
606 // Read sample headers | 626 // Read sample headers |
607 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | 627 for (nsample = 0; nsample < xmI1.nsamples; nsample++) |
608 { | 628 { |
609 if ((ret = jssXMLoadInstrument(inFile, module, ninst, nsample, xmConvTable)) != DMERR_OK) | 629 if ((ret = jssXMLoadSampleInstrument(inFile, module, einst, ninst, nsample)) != DMERR_OK) |
610 return ret; | 630 return ret; |
611 } | 631 } |
612 | |
613 // Read sample data | |
614 for (nsample = 0; nsample < xmI1.nsamples; nsample++) | |
615 if (xmConvTable[nsample] != jsetNotSet) | |
616 { | |
617 JSSInstrument *inst = module->instruments[xmConvTable[nsample]]; | |
618 if ((ret = jssXMLoadSampleData(inFile, inst, ninst, nsample)) != DMERR_OK) | |
619 return ret; | |
620 } | |
621 | |
622 | 632 |
623 // Apply new values to sNumForNotes values | 633 // Apply new values to sNumForNotes values |
624 for (i = 0; i < XM_MaxNotes; i++) | 634 for (i = 0; i < XM_MaxNotes; i++) |
625 { | 635 { |
626 int tmp = xmI2.sNumForNotes[i]; | 636 int tmp = xmI2.sNumForNotes[i]; |
627 if (tmp >= 0 && tmp < xmI1.nsamples) | 637 if (tmp >= 0 && tmp < xmI1.nsamples) |
628 einst->sNumForNotes[i] = xmConvTable[tmp]; | 638 einst->sNumForNotes[i] = einst->instConvTable[tmp]; |
629 else | 639 else |
630 { | 640 { |
631 einst->sNumForNotes[i] = jsetNotSet; | 641 einst->sNumForNotes[i] = jsetNotSet; |
632 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, | 642 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, |
633 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n", | 643 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n", |
634 ninst, i, tmp); | 644 ninst, i, tmp); |
635 } | 645 } |
636 } | 646 } |
637 | 647 |
648 if ((ret = jssXMLoadInstrumentSamples(inFile, module, einst, ninst)) != DMERR_OK) | |
649 return ret; | |
638 return DMERR_OK; | 650 return DMERR_OK; |
639 } | 651 } |
640 | 652 |
641 | 653 |
642 static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH) | 654 static int jssXMLoadPatterns(DMResource *inFile, JSSModule *module, XMHeader *xmH) |
728 */ | 740 */ |
729 int jssLoadXM(DMResource *inFile, JSSModule **ppModule, BOOL probe) | 741 int jssLoadXM(DMResource *inFile, JSSModule **ppModule, BOOL probe) |
730 { | 742 { |
731 JSSModule *module; | 743 JSSModule *module; |
732 XMHeader xmH; | 744 XMHeader xmH; |
733 int index, result; | 745 int index, ret; |
734 | 746 |
735 assert(ppModule != NULL); | 747 assert(ppModule != NULL); |
736 assert(inFile != NULL); | 748 assert(inFile != NULL); |
737 *ppModule = NULL; | 749 *ppModule = NULL; |
738 | 750 |
878 // Setup the default pannings | 890 // Setup the default pannings |
879 for (index = 0; index < jsetNChannels; index++) | 891 for (index = 0; index < jsetNChannels; index++) |
880 module->defPanning[index] = jchPanMiddle; | 892 module->defPanning[index] = jchPanMiddle; |
881 | 893 |
882 // Load rest of the module | 894 // Load rest of the module |
883 if ((result = jssXMLoadPatterns(inFile, module, &xmH)) != DMERR_OK) | 895 switch (xmH.version) |
884 return result; | 896 { |
885 | 897 case 0x0104: |
886 if ((result = jssXMLoadInstruments(inFile, module)) != DMERR_OK) | 898 if ((ret = jssXMLoadPatterns(inFile, module, &xmH)) != DMERR_OK) |
887 return result; | 899 goto out; |
888 | 900 |
889 return DMERR_OK; | 901 if ((ret = jssXMLoadInstruments(inFile, module)) != DMERR_OK) |
890 } | 902 goto out; |
903 break; | |
904 | |
905 } | |
906 | |
907 out: | |
908 | |
909 return ret; | |
910 } |