comparison tools/xm2jss.c @ 1097:0e3cb9e61170

Code cleanup, in preparation for bug hunting.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Mar 2015 02:36:45 +0200
parents 8fc475bbfa42
children f309a382d6e7
comparison
equal deleted inserted replaced
1096:96e0798f602b 1097:0e3cb9e61170
646 usedExtInstruments[jsetMaxInstruments + 1]; 646 usedExtInstruments[jsetMaxInstruments + 1];
647 int mapExtInstruments[jsetMaxInstruments + 1], 647 int mapExtInstruments[jsetMaxInstruments + 1],
648 mapInstruments[jsetMaxInstruments + 1], 648 mapInstruments[jsetMaxInstruments + 1],
649 mapPatterns[jsetMaxPatterns + 1]; 649 mapPatterns[jsetMaxPatterns + 1];
650 JSSModule *r = NULL; 650 JSSModule *r = NULL;
651 int i, n8, n16; 651 int i, n8, n16, unused;
652 652
653 // Allocate a new module 653 // Allocate a new module
654 if ((r = jssAllocateModule()) == NULL) 654 if ((r = jssAllocateModule()) == NULL)
655 return NULL; 655 return NULL;
656 656
701 usedPatterns[pattern] = TRUE; 701 usedPatterns[pattern] = TRUE;
702 702
703 // Check all notes 703 // Check all notes
704 for (row = 0; row < p->nrows; row++) 704 for (row = 0; row < p->nrows; row++)
705 for (channel = 0; channel < p->nchannels; channel++, n++) 705 for (channel = 0; channel < p->nchannels; channel++, n++)
706 if (n->instrument != jsetNotSet)
706 { 707 {
707 if (n->instrument != jsetNotSet) 708 if (n->instrument >= 0 && n->instrument < m->nextInstruments)
709 usedExtInstruments[n->instrument] = TRUE;
710 else
708 { 711 {
709 if (optStripExtInstr || (n->instrument >= 0 && n->instrument < m->nextInstruments)) 712 dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n",
710 usedExtInstruments[n->instrument] = TRUE; 713 pattern, row, channel, n->instrument);
711 else
712 dmMsg(2, "Pattern 0x%x, row=0x%x, chn=%d has invalid instrument 0x%x\n",
713 pattern, row, channel, n->instrument);
714 } 714 }
715 } 715 }
716 } 716 }
717 else 717 else
718 { 718 {
761 761
762 // 762 //
763 // Create pattern mappings 763 // Create pattern mappings
764 // 764 //
765 r->npatterns = 0; 765 r->npatterns = 0;
766 dmMsg(1, "Unused patterns: "); 766 dmMsg(1, "Creating pattern remaps...\n");
767 767
768 for (i = 0; i <= jsetMaxPatterns; i++) 768 for (unused = i = 0; i <= jsetMaxPatterns; i++)
769 if (usedPatterns[i])
770 {
771 JSSPattern *pat;
772
773 if (i >= m->npatterns)
774 {
775 dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns);
776 continue;
777 }
778
779 if ((pat = m->patterns[i]) == NULL)
780 {
781 dmErrorMsg("Pattern 0x%x used but is NULL.\n", i);
782 continue;
783 }
784
785 mapPatterns[i] = r->npatterns;
786 r->patterns[r->npatterns] = m->patterns[i];
787 (r->npatterns)++;
788 }
789 else
769 if (m->patterns[i] != NULL) 790 if (m->patterns[i] != NULL)
770 { 791 unused++;
771 if (!usedPatterns[i]) 792
772 { 793 dmMsg(1, "%d used patterns (%d unused).\n",
773 dmPrint(2, "0x%x, ", i); 794 r->npatterns, unused);
774 } 795
796
797 //
798 // Re-map instruments
799 //
800 dmMsg(1, "Creating sample instrument remaps...\n");
801 for (unused = n8 = n16 = i = 0; i < jsetMaxInstruments; i++)
802 if (usedInstruments[i])
803 {
804 JSSInstrument *ip;
805 if (optStripInstr)
806 continue;
807
808 if (i >= m->ninstruments)
809 {
810 dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n",
811 i, m->ninstruments);
812 continue;
813 }
814
815 if ((ip = m->instruments[i]) == NULL)
816 {
817 dmErrorMsg("Instrument 0x%x used but is NULL.\n", i);
818 continue;
819 }
820
821 dmPrint(2, "%02x -> %02x : ", i, r->ninstruments);
822 mapInstruments[i] = r->ninstruments;
823 r->instruments[r->ninstruments] = ip;
824 (r->ninstruments)++;
825
826 if (ip->flags & jsf16bit)
827 n16++;
775 else 828 else
776 { 829 n8++;
777 if (i >= m->npatterns) 830 }
778 dmErrorMsg("Pattern 0x%x >= 0x%x, but used!\n", i, m->npatterns); 831 else
779 832 if (m->instruments[i] != NULL)
780 mapPatterns[i] = r->npatterns; 833 unused++;
781 r->patterns[r->npatterns] = m->patterns[i]; 834
782 (r->npatterns)++;
783 }
784 }
785 dmPrint(2, "\n"); 835 dmPrint(2, "\n");
786 836 dmMsg(1, "Total of %d [16-bit] + %d [8-bit] samples = %d instruments (%d unused).\n",
787 dmMsg(1, "%d used patterns, %d unused.\n", 837 n16, n8, r->ninstruments, unused);
788 r->npatterns, m->npatterns - r->npatterns); 838
789 839 //
790
791 //
792 // Re-map instruments
793 //
794 dmMsg(1, "Unused instruments: ");
795 for (n8 = n16 = i = 0; i < jsetMaxInstruments; i++)
796 if (m->instruments[i] != NULL)
797 {
798 if (!usedInstruments[i] && !optStripInstr)
799 {
800 dmPrint(2, "0x%x, ", i);
801 }
802 else
803 {
804 JSSInstrument *ip = m->instruments[i];
805 if (i >= m->ninstruments)
806 dmErrorMsg("Instrument 0x%x >= 0x%x, but used!\n", i, m->ninstruments);
807
808 mapInstruments[i] = r->ninstruments;
809 r->instruments[r->ninstruments] = ip;
810 (r->ninstruments)++;
811
812 if (ip->flags & jsf16bit)
813 n16++;
814 else
815 n8++;
816 }
817 }
818 dmPrint(2, "\n");
819 dmMsg(1, "Total of (%d) 16-bit, (%d) 8-bit samples, (%d) instruments.\n",
820 n16, n8, r->ninstruments);
821
822 // Re-map ext.instruments 840 // Re-map ext.instruments
823 dmMsg(1, "Unused ext.instruments: "); 841 //
824 for (i = 0; i < jsetMaxInstruments; i++) 842 dmMsg(1, "Creating ext.instrument remaps...\n");
843 for (unused = i = 0; i < jsetMaxInstruments; i++)
825 if (usedExtInstruments[i]) 844 if (usedExtInstruments[i])
826 { 845 {
827 if (i >= m->nextInstruments && !optStripExtInstr) 846 JSSExtInstrument *eip;
847 int note;
848 if (optStripExtInstr)
849 continue;
850
851 if (i >= m->nextInstruments)
828 { 852 {
829 dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n", 853 dmErrorMsg("Ext.instrument 0x%x >= 0x%x, but used!\n",
830 i, m->nextInstruments); 854 i, m->nextInstruments);
831 } 855 continue;
832 else 856 }
833 if (m->extInstruments[i] != NULL) 857
834 { 858 if ((eip = m->extInstruments[i]) == NULL)
835 JSSExtInstrument *e = m->extInstruments[i]; 859 {
836 int note; 860 dmErrorMsg("Extended instrument 0x%x used but is NULL.\n", i);
837 861 continue;
838 mapExtInstruments[i] = r->nextInstruments; 862 }
839 r->extInstruments[r->nextInstruments] = e; 863
840 (r->nextInstruments)++; 864 dmPrint(2, "%02x -> %02x : ", i, r->nextInstruments);
841 865 mapExtInstruments[i] = r->nextInstruments;
842 // Re-map sNumForNotes 866 r->extInstruments[r->nextInstruments] = eip;
843 for (note = 0; note < jsetNNotes; note++) 867 (r->nextInstruments)++;
844 { 868
845 int q = e->sNumForNotes[note]; 869 // Re-map sNumForNotes table for this ext.instrument
846 if (q != jsetNotSet) 870 for (note = 0; note < jsetNNotes; note++)
871 {
872 int q = eip->sNumForNotes[note];
873 if (q != jsetNotSet)
874 {
875 int map;
876 if (q >= 0 && q < jsetMaxInstruments)
847 { 877 {
848 int map; 878 map = mapInstruments[q];
849 if (q >= 0 && q < jsetMaxInstruments)
850 {
851 map = mapInstruments[q];
852 }
853 else
854 {
855 map = jsetNotSet;
856 dmErrorMsg("e=%d, note=%d, q=%d/%d\n", i, note, q, r->ninstruments);
857 }
858 e->sNumForNotes[note] = map;
859 } 879 }
860 } 880 else
861 } 881 {
862 else 882 map = jsetNotSet;
863 { 883 dmErrorMsg("e=%d, note=%d, q=%d/%d\n", i, note, q, r->ninstruments);
864 dmPrint(2, "[0x%x==NULL], ", i); 884 }
865 mapExtInstruments[i] = jsetNotSet; 885 dmPrint(3, "%02x.%02x ", q, map);
886 eip->sNumForNotes[note] = map;
887 }
866 } 888 }
867 } 889 }
868 else 890 else
869 { 891 if (m->extInstruments[i] != NULL)
870 if (i < m->nextInstruments && m->extInstruments[i] != NULL) 892 unused++;
871 { 893
872 dmPrint(2, "0x%x, ", i);
873 }
874 }
875 dmPrint(2, "\n"); 894 dmPrint(2, "\n");
876 dmMsg(1, "%d extended instruments.\n", r->nextInstruments); 895 dmMsg(1, "%d extended instruments (%d unused).\n",
896 r->nextInstruments, unused);
877 897
878 // 898 //
879 // Remap pattern data with remapped instrument data 899 // Remap pattern data with remapped instrument data
880 // 900 //
881 for (i = 0; i < r->npatterns; i++) 901 for (i = 0; i < r->npatterns; i++)