comparison minijss/jloadxm.c @ 1226:18baa42db260

Rename some function arguments to better reflect their meaning.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 06 Mar 2015 10:04:11 +0200
parents e8f0305f8e6b
children 2be42ba9fc39
comparison
equal deleted inserted replaced
1225:e8f0305f8e6b 1226:18baa42db260
502 } 502 }
503 503
504 504
505 static int jssXMLoadSampleInstrument( 505 static int jssXMLoadSampleInstrument(
506 DMResource *inFile, JSSModule *module, 506 DMResource *inFile, JSSModule *module,
507 JSSExtInstrument *einst, int ninst, int nsample) 507 JSSExtInstrument *einst, int neinst, int nsample)
508 { 508 {
509 XMSample xmS; 509 XMSample xmS;
510 JSSInstrument *inst; 510 JSSInstrument *inst;
511 511
512 // Read header data 512 // Read header data
521 !dmf_read_byte(inFile, &xmS.ARESERVED) || 521 !dmf_read_byte(inFile, &xmS.ARESERVED) ||
522 !dmf_read_str(inFile, &xmS.sampleName, sizeof(xmS.sampleName))) 522 !dmf_read_str(inFile, &xmS.sampleName, sizeof(xmS.sampleName)))
523 { 523 {
524 JSSERROR(DMERR_FREAD, DMERR_FREAD, 524 JSSERROR(DMERR_FREAD, DMERR_FREAD,
525 "Error reading instrument sample header #%d/%d [%d]", 525 "Error reading instrument sample header #%d/%d [%d]",
526 ninst, nsample, module->ninstruments); 526 neinst, nsample, module->ninstruments);
527 } 527 }
528 528
529 if (xmS.size <= 0) 529 if (xmS.size <= 0)
530 return DMERR_OK; 530 return DMERR_OK;
531 531
532 // Allocate sample instrument 532 // Allocate sample instrument
533 JSSDEBUG("Allocating sample #%d/%d [%d]\n", 533 JSSDEBUG("Allocating sample #%d/%d [%d]\n",
534 ninst, nsample, module->ninstruments); 534 neinst, nsample, module->ninstruments);
535 535
536 einst->instConvTable[nsample] = module->ninstruments; 536 einst->instConvTable[nsample] = module->ninstruments;
537 inst = module->instruments[module->ninstruments] = jssAllocateInstrument(); 537 inst = module->instruments[module->ninstruments] = jssAllocateInstrument();
538 if (inst == NULL) 538 if (inst == NULL)
539 { 539 {
540 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, 540 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
541 "Could not allocate sample #%d/%d [%d]\n", 541 "Could not allocate sample #%d/%d [%d]\n",
542 ninst, nsample, module->ninstruments); 542 neinst, nsample, module->ninstruments);
543 } 543 }
544 module->ninstruments++; 544 module->ninstruments++;
545 545
546 // Copy values 546 // Copy values
547 if (xmS.volume > XM_MaxSampleVolume) 547 if (xmS.volume > XM_MaxSampleVolume)
548 { 548 {
549 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 549 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
550 "Samp #%d/%d: volume > MAX\n", ninst, nsample); 550 "Samp #%d/%d: volume > MAX\n", neinst, nsample);
551 xmS.volume = XM_MaxSampleVolume; 551 xmS.volume = XM_MaxSampleVolume;
552 } 552 }
553 553
554 inst->volume = xmS.volume; 554 inst->volume = xmS.volume;
555 inst->ERelNote = xmS.relNote; 555 inst->ERelNote = xmS.relNote;
598 { 598 {
599 if (inst->loopS >= inst->size) 599 if (inst->loopS >= inst->size)
600 { 600 {
601 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 601 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
602 "Samp #%d/%d: loopS >= size (%d >= %d)\n", 602 "Samp #%d/%d: loopS >= size (%d >= %d)\n",
603 ninst, nsample, inst->loopS, inst->size); 603 neinst, nsample, inst->loopS, inst->size);
604 JSFUNSET(inst->flags, jsfLooped); 604 JSFUNSET(inst->flags, jsfLooped);
605 } 605 }
606 606
607 if (inst->loopE > inst->size) 607 if (inst->loopE > inst->size)
608 { 608 {
609 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 609 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
610 "Samp #%d/%d: loopE > size (%d > %d)\n", 610 "Samp #%d/%d: loopE > size (%d > %d)\n",
611 ninst, nsample, inst->loopE, inst->size); 611 neinst, nsample, inst->loopE, inst->size);
612 JSFUNSET(inst->flags, jsfLooped); 612 JSFUNSET(inst->flags, jsfLooped);
613 } 613 }
614 } 614 }
615 615
616 return DMERR_OK; 616 return DMERR_OK;
674 } 674 }
675 675
676 676
677 static int jssXMLoadInstrumentSamples( 677 static int jssXMLoadInstrumentSamples(
678 DMResource *inFile, JSSModule *module, 678 DMResource *inFile, JSSModule *module,
679 JSSExtInstrument *einst, int ninst) 679 JSSExtInstrument *einst, int neinst)
680 { 680 {
681 int nsample, ret; 681 int nsample, ret;
682 682
683 for (nsample = 0; nsample < einst->nsamples; nsample++) 683 for (nsample = 0; nsample < einst->nsamples; nsample++)
684 if (einst->instConvTable[nsample] != jsetNotSet) 684 if (einst->instConvTable[nsample] != jsetNotSet)
685 { 685 {
686 JSSInstrument *inst = module->instruments[einst->instConvTable[nsample]]; 686 JSSInstrument *inst = module->instruments[einst->instConvTable[nsample]];
687 if ((ret = jssXMLoadSampleData(inFile, inst, ninst, nsample)) != DMERR_OK) 687 if ((ret = jssXMLoadSampleData(inFile, inst, neinst, nsample)) != DMERR_OK)
688 return ret; 688 return ret;
689 } 689 }
690 690
691 return DMERR_OK; 691 return DMERR_OK;
692 } 692 }
714 } 714 }
715 715
716 716
717 /* Load XM-format extended instrument from file-stream into JSS module's given inst 717 /* Load XM-format extended instrument from file-stream into JSS module's given inst
718 */ 718 */
719 static int jssXMLoadExtInstrument(DMResource *inFile, int ninst, JSSModule *module) 719 static int jssXMLoadExtInstrument(DMResource *inFile, int neinst, JSSModule *module)
720 { 720 {
721 XMInstrument1 xmI1; 721 XMInstrument1 xmI1;
722 off_t remainder, pos = dmftell(inFile); 722 off_t remainder, pos = dmftell(inFile);
723 JSSExtInstrument *einst; 723 JSSExtInstrument *einst;
724 XMInstrument2 xmI2; 724 XMInstrument2 xmI2;
729 !dmf_read_str(inFile, &xmI1.instName, sizeof(xmI1.instName)) || 729 !dmf_read_str(inFile, &xmI1.instName, sizeof(xmI1.instName)) ||
730 !dmf_read_byte(inFile, &xmI1.instType) || 730 !dmf_read_byte(inFile, &xmI1.instType) ||
731 !dmf_read_le16(inFile, &xmI1.nsamples)) 731 !dmf_read_le16(inFile, &xmI1.nsamples))
732 { 732 {
733 JSSERROR(DMERR_FREAD, DMERR_FREAD, 733 JSSERROR(DMERR_FREAD, DMERR_FREAD,
734 "Failed to read primary extended instrument header #%d\n", ninst); 734 "Failed to read primary extended instrument header #%d\n", neinst);
735 } 735 }
736 736
737 // If there are samples, there is header #2 737 // If there are samples, there is header #2
738 if (xmI1.nsamples == 0) 738 if (xmI1.nsamples == 0)
739 { 739 {
749 749
750 // Allocate instrument 750 // Allocate instrument
751 if ((einst = jssAllocateExtInstrument()) == NULL) 751 if ((einst = jssAllocateExtInstrument()) == NULL)
752 { 752 {
753 JSSERROR(DMERR_MALLOC, DMERR_MALLOC, 753 JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
754 "Could not allocate extended instrument structure #%d\n", ninst); 754 "Could not allocate extended instrument structure #%d\n", neinst);
755 } 755 }
756 756
757 module->extInstruments[ninst] = einst; 757 module->extInstruments[neinst] = einst;
758 758
759 // Get instrument header #2 759 // Get instrument header #2
760 if (!dmf_read_le32(inFile, &xmI2.headSize) || 760 if (!dmf_read_le32(inFile, &xmI2.headSize) ||
761 !dmf_read_str(inFile, &xmI2.sNumForNotes, sizeof(xmI2.sNumForNotes))) 761 !dmf_read_str(inFile, &xmI2.sNumForNotes, sizeof(xmI2.sNumForNotes)))
762 { 762 {
763 JSSERROR(DMERR_FREAD, DMERR_FREAD, 763 JSSERROR(DMERR_FREAD, DMERR_FREAD,
764 "Could not read secondary instrument header part #1 for #%d.\n", ninst); 764 "Could not read secondary instrument header part #1 for #%d.\n", neinst);
765 } 765 }
766 766
767 if (!jssXMLoadEnvelopePoints(inFile, &xmI2.volumeEnv) || 767 if (!jssXMLoadEnvelopePoints(inFile, &xmI2.volumeEnv) ||
768 !jssXMLoadEnvelopePoints(inFile, &xmI2.panningEnv)) 768 !jssXMLoadEnvelopePoints(inFile, &xmI2.panningEnv))
769 { 769 {
770 JSSERROR(DMERR_FREAD, DMERR_FREAD, 770 JSSERROR(DMERR_FREAD, DMERR_FREAD,
771 "Could not read envelope point data for instrument #%d.\n", ninst); 771 "Could not read envelope point data for instrument #%d.\n", neinst);
772 } 772 }
773 773
774 if (!dmf_read_byte(inFile, &xmI2.volumeEnv.npoints) || 774 if (!dmf_read_byte(inFile, &xmI2.volumeEnv.npoints) ||
775 !dmf_read_byte(inFile, &xmI2.panningEnv.npoints) || 775 !dmf_read_byte(inFile, &xmI2.panningEnv.npoints) ||
776 776
787 787
788 !dmf_read_le16(inFile, &xmI2.fadeOut) || 788 !dmf_read_le16(inFile, &xmI2.fadeOut) ||
789 !dmf_read_le16(inFile, &xmI2.ARESERVED)) 789 !dmf_read_le16(inFile, &xmI2.ARESERVED))
790 { 790 {
791 JSSERROR(DMERR_FREAD, DMERR_FREAD, 791 JSSERROR(DMERR_FREAD, DMERR_FREAD,
792 "Could not read secondary instrument header part #2 for #%d.\n", ninst); 792 "Could not read secondary instrument header part #2 for #%d.\n", neinst);
793 } 793 }
794 794
795 // Skip the extra data after header #2 795 // Skip the extra data after header #2
796 remainder = xmI1.headSize - (dmftell(inFile) - pos); 796 remainder = xmI1.headSize - (dmftell(inFile) - pos);
797 if (remainder > 0) 797 if (remainder > 0)
802 802
803 // Check and convert all ext instrument information 803 // Check and convert all ext instrument information
804 #ifndef JSS_LIGHT 804 #ifndef JSS_LIGHT
805 einst->desc = jssASCIItoStr(xmI1.instName, 0, sizeof(xmI1.instName)); 805 einst->desc = jssASCIItoStr(xmI1.instName, 0, sizeof(xmI1.instName));
806 #endif 806 #endif
807 jssXMConvertEnvelope(&einst->volumeEnv, &xmI2.volumeEnv, "vol", ninst); 807 jssXMConvertEnvelope(&einst->volumeEnv, &xmI2.volumeEnv, "vol", neinst);
808 jssXMConvertEnvelope(&einst->panningEnv, &xmI2.panningEnv, "pan", ninst); 808 jssXMConvertEnvelope(&einst->panningEnv, &xmI2.panningEnv, "pan", neinst);
809 809
810 switch (xmI2.vibratoType) 810 switch (xmI2.vibratoType)
811 { 811 {
812 case 0: einst->vibratoType = jvibSine; break; 812 case 0: einst->vibratoType = jvibSine; break;
813 case 1: einst->vibratoType = jvibRamp; break; 813 case 1: einst->vibratoType = jvibRamp; break;
814 case 2: einst->vibratoType = jvibSquare; break; 814 case 2: einst->vibratoType = jvibSquare; break;
815 case 3: einst->vibratoType = jvibRandom; break; 815 case 3: einst->vibratoType = jvibRandom; break;
816 default: 816 default:
817 einst->vibratoType = jvibSine; 817 einst->vibratoType = jvibSine;
818 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 818 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
819 "Invalid extinstrument vibrato type %d for inst #%d\n", ninst); 819 "Invalid extinstrument vibrato type %d for inst #%d\n", neinst);
820 break; 820 break;
821 } 821 }
822 einst->vibratoSweep = xmI2.vibratoSweep; 822 einst->vibratoSweep = xmI2.vibratoSweep;
823 einst->vibratoDepth = xmI2.vibratoDepth; 823 einst->vibratoDepth = xmI2.vibratoDepth;
824 einst->vibratoRate = xmI2.vibratoRate; 824 einst->vibratoRate = xmI2.vibratoRate;
830 einst->instConvTable[i] = jsetNotSet; 830 einst->instConvTable[i] = jsetNotSet;
831 831
832 // Read sample headers 832 // Read sample headers
833 for (nsample = 0; nsample < xmI1.nsamples; nsample++) 833 for (nsample = 0; nsample < xmI1.nsamples; nsample++)
834 { 834 {
835 if ((ret = jssXMLoadSampleInstrument(inFile, module, einst, ninst, nsample)) != DMERR_OK) 835 if ((ret = jssXMLoadSampleInstrument(inFile, module, einst, neinst, nsample)) != DMERR_OK)
836 return ret; 836 return ret;
837 } 837 }
838 838
839 // Apply new values to sNumForNotes values 839 // Apply new values to sNumForNotes values
840 for (i = 0; i < XM_MaxNotes; i++) 840 for (i = 0; i < XM_MaxNotes; i++)
845 else 845 else
846 { 846 {
847 einst->sNumForNotes[i] = jsetNotSet; 847 einst->sNumForNotes[i] = jsetNotSet;
848 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA, 848 JSSWARNING(DMERR_INVALID_DATA, DMERR_INVALID_DATA,
849 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n", 849 "Ext.instrument #%d sNumForNotes[%d] out of range (%d).\n",
850 ninst, i, tmp); 850 neinst, i, tmp);
851 } 851 }
852 } 852 }
853 853
854 // Read sample data if needed 854 // Read sample data if needed
855 if (module->intVersion == 0x0104) 855 if (module->intVersion == 0x0104)
856 { 856 {
857 if ((ret = jssXMLoadInstrumentSamples(inFile, module, einst, ninst)) != DMERR_OK) 857 if ((ret = jssXMLoadInstrumentSamples(inFile, module, einst, neinst)) != DMERR_OK)
858 return ret; 858 return ret;
859 } 859 }
860 860
861 return DMERR_OK; 861 return DMERR_OK;
862 } 862 }