home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.70_stereo.zip / src / gsong.c < prev    next >
C/C++ Source or Header  |  2008-07-13  |  47KB  |  1,658 lines

  1. //
  2. // GOATTRACKER v2 song data model, loading/saving/conversion
  3. //
  4.  
  5. #define GSONG_C
  6.  
  7. #include "goattrk2.h"
  8.  
  9. INSTR instr[MAX_INSTR];
  10. unsigned char ltable[MAX_TABLES][MAX_TABLELEN];
  11. unsigned char rtable[MAX_TABLES][MAX_TABLELEN];
  12. unsigned char songorder[MAX_SONGS][MAX_CHN][MAX_SONGLEN+2];
  13. unsigned char pattern[MAX_PATT][MAX_PATTROWS*4+4];
  14. char songname[MAX_STR];
  15. char authorname[MAX_STR];
  16. char copyrightname[MAX_STR];
  17. int pattlen[MAX_PATT];
  18. int songlen[MAX_SONGS][MAX_CHN];
  19. int highestusedpattern;
  20. int highestusedinstr;
  21. int determinechannels();
  22.  
  23. int savesong(void)
  24. {
  25.   int c;
  26.   char ident[] = {'G', 'T', 'S', '5'};
  27.   FILE *handle;
  28.  
  29.   if (strlen(songfilename) < MAX_FILENAME-4)
  30.   {
  31.     int extfound = 0;
  32.     for (c = strlen(songfilename)-1; c >= 0; c--)
  33.     {
  34.       if (songfilename[c] == '.') extfound = 1;
  35.     }
  36.     if (!extfound) strcat(songfilename, ".sng");
  37.   }
  38.   handle = fopen(songfilename, "wb");
  39.   if (handle)
  40.   {
  41.     int d;
  42.     int length;
  43.     int amount;
  44.     int writebytes;
  45.     fwrite(ident, 4, 1, handle);
  46.  
  47.     // Determine amount of patterns & instruments
  48.     countpatternlengths();
  49.     for (c = 1; c < MAX_INSTR; c++)
  50.     {
  51.       if ((instr[c].ad) || (instr[c].sr) || (instr[c].ptr[0]) || (instr[c].ptr[1]) ||
  52.           (instr[c].ptr[2]) || (instr[c].vibdelay) || (instr[c].ptr[3]))
  53.       {
  54.         if (c > highestusedinstr) highestusedinstr = c;
  55.       }
  56.     }
  57.  
  58.     // Write infotexts
  59.     fwrite(songname, sizeof songname, 1, handle);
  60.     fwrite(authorname, sizeof authorname, 1, handle);
  61.     fwrite(copyrightname, sizeof copyrightname, 1, handle);
  62.  
  63.     // Determine amount of songs to be saved
  64.     c = MAX_SONGS - 1;
  65.     for (;;)
  66.     {
  67.       if ((songlen[c][0])&&
  68.          (songlen[c][1])&&
  69.          (songlen[c][2])) break;
  70.       if (c == 0) break;
  71.       c--;
  72.     }
  73.     amount = c + 1;
  74.  
  75.     fwrite8(handle, amount);
  76.     // Write songorderlists
  77.     for (d = 0; d < amount; d++)
  78.     {
  79.       for (c = 0; c < MAX_CHN; c++)
  80.       {
  81.         length = songlen[d][c]+1;
  82.         fwrite8(handle, length);
  83.         writebytes = length;
  84.         writebytes++;
  85.         fwrite(songorder[d][c], writebytes, 1, handle);
  86.       }
  87.     }
  88.     // Write amount of instruments
  89.     fwrite8(handle, highestusedinstr);
  90.     // Write instruments
  91.     for (c = 1; c <= highestusedinstr; c++)
  92.     {
  93.       fwrite8(handle, instr[c].ad);
  94.       fwrite8(handle, instr[c].sr);
  95.       fwrite8(handle, instr[c].ptr[WTBL]);
  96.       fwrite8(handle, instr[c].ptr[PTBL]);
  97.       fwrite8(handle, instr[c].ptr[FTBL]);
  98.       fwrite8(handle, instr[c].ptr[STBL]);
  99.       fwrite8(handle, instr[c].vibdelay);
  100.       fwrite8(handle, instr[c].gatetimer);
  101.       fwrite8(handle, instr[c].firstwave);
  102.       fwrite(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
  103.     }
  104.     // Write tables
  105.     for (c = 0; c < MAX_TABLES; c++)
  106.     {
  107.       writebytes = gettablelen(c);
  108.       fwrite8(handle, writebytes);
  109.       fwrite(ltable[c], writebytes, 1, handle);
  110.       fwrite(rtable[c], writebytes, 1, handle);
  111.     }
  112.     // Write patterns
  113.     amount = highestusedpattern + 1;
  114.     fwrite8(handle, amount);
  115.     for (c = 0; c < amount; c++)
  116.     {
  117.       length = pattlen[c]+1;
  118.       fwrite8(handle, length);
  119.       fwrite(pattern[c], length * 4, 1, handle);
  120.     }
  121.     fclose(handle);
  122.     strcpy(loadedsongfilename, songfilename);
  123.     return 1;
  124.   }
  125.   return 0;
  126. }
  127.  
  128. int saveinstrument(void)
  129. {
  130.   int c;
  131.   char ident[] = {'G', 'T', 'I', '5'};
  132.   FILE *handle;
  133.  
  134.   if (strlen(instrfilename) < MAX_FILENAME-4)
  135.   {
  136.     int extfound = 0;
  137.     for (c = strlen(instrfilename)-1; c >= 0; c--)
  138.     {
  139.       if (instrfilename[c] == '.') extfound = 1;
  140.     }
  141.     if (!extfound) strcat(instrfilename, ".ins");
  142.   }
  143.  
  144.   handle = fopen(instrfilename, "wb");
  145.   if (handle)
  146.   {
  147.     fwrite(ident, 4, 1, handle);
  148.  
  149.     // Write instrument
  150.     fwrite8(handle, instr[einum].ad);
  151.     fwrite8(handle, instr[einum].sr);
  152.     fwrite8(handle, instr[einum].ptr[WTBL]);
  153.     fwrite8(handle, instr[einum].ptr[PTBL]);
  154.     fwrite8(handle, instr[einum].ptr[FTBL]);
  155.     fwrite8(handle, instr[einum].ptr[STBL]);
  156.     fwrite8(handle, instr[einum].vibdelay);
  157.     fwrite8(handle, instr[einum].gatetimer);
  158.     fwrite8(handle, instr[einum].firstwave);
  159.     fwrite(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle);
  160.     for (c = 0; c < MAX_TABLES; c++)
  161.     {
  162.       if (instr[einum].ptr[c])
  163.       {
  164.         int pos = instr[einum].ptr[c] - 1;
  165.         int len = gettablepartlen(c, pos);
  166.         fwrite8(handle, len);
  167.         fwrite(<able[c][pos], len, 1, handle);
  168.         fwrite(&rtable[c][pos], len, 1, handle);
  169.       }
  170.       else fwrite8(handle, 0);
  171.     }
  172.     fclose(handle);
  173.     return 1;
  174.   }
  175.   return 0;
  176. }
  177.  
  178. void loadsong(void)
  179. {
  180.   int c;
  181.   int ok = 0;
  182.   char ident[4];
  183.   FILE *handle;
  184.   int channelstoload = MAX_CHN;
  185.  
  186.   handle = fopen(songfilename, "rb");
  187.  
  188.   if (handle)
  189.   {
  190.     fread(ident, 4, 1, handle);
  191.     if ((!memcmp(ident, "GTS3", 4)) || (!memcmp(ident, "GTS4", 4)) || (!memcmp(ident, "GTS5", 4)))
  192.     {
  193.       int d;
  194.       int length;
  195.       int amount;
  196.       int loadsize;
  197.       clearsong(1,1,1,1,1);
  198.       ok = 1;
  199.  
  200.       // Read infotexts
  201.       fread(songname, sizeof songname, 1, handle);
  202.       fread(authorname, sizeof authorname, 1, handle);
  203.       fread(copyrightname, sizeof copyrightname, 1, handle);
  204.  
  205.       // Read songorderlists
  206.       channelstoload = determinechannels(handle);
  207.       amount = fread8(handle);
  208.       for (d = 0; d < amount; d++)
  209.       {
  210.         for (c = 0; c < channelstoload; c++)
  211.         {
  212.           length = fread8(handle);
  213.           loadsize = length;
  214.           loadsize++;
  215.           fread(songorder[d][c], loadsize, 1, handle);
  216.         }
  217.       }
  218.       // Read instruments
  219.       amount = fread8(handle);
  220.       for (c = 1; c <= amount; c++)
  221.       {
  222.         instr[c].ad = fread8(handle);
  223.         instr[c].sr = fread8(handle);
  224.         instr[c].ptr[WTBL] = fread8(handle);
  225.         instr[c].ptr[PTBL] = fread8(handle);
  226.         instr[c].ptr[FTBL] = fread8(handle);
  227.         instr[c].ptr[STBL] = fread8(handle);
  228.         instr[c].vibdelay = fread8(handle);
  229.         instr[c].gatetimer = fread8(handle);
  230.         instr[c].firstwave = fread8(handle);
  231.         fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
  232.       }
  233.       // Read tables
  234.       for (c = 0; c < MAX_TABLES; c++)
  235.       {
  236.         loadsize = fread8(handle);
  237.         fread(ltable[c], loadsize, 1, handle);
  238.         fread(rtable[c], loadsize, 1, handle);
  239.       }
  240.       // Read patterns
  241.       amount = fread8(handle);
  242.       for (c = 0; c < amount; c++)
  243.       {
  244.         length = fread8(handle) * 4;
  245.         fread(pattern[c], length, 1, handle);
  246.       }
  247.       countpatternlengths();
  248.       songchange();
  249.     }
  250.  
  251.     // Goattracker v2.xx (3-table) import
  252.     if (!memcmp(ident, "GTS2", 4))
  253.     {
  254.       int d;
  255.       int length;
  256.       int amount;
  257.       int loadsize;
  258.       clearsong(1,1,1,1,1);
  259.       ok = 1;
  260.  
  261.       // Read infotexts
  262.       fread(songname, sizeof songname, 1, handle);
  263.       fread(authorname, sizeof authorname, 1, handle);
  264.       fread(copyrightname, sizeof copyrightname, 1, handle);
  265.  
  266.       // Read songorderlists
  267.       channelstoload = determinechannels(handle);
  268.       amount = fread8(handle);
  269.       for (d = 0; d < amount; d++)
  270.       {
  271.         for (c = 0; c < channelstoload; c++)
  272.         {
  273.           length = fread8(handle);
  274.           loadsize = length;
  275.           loadsize++;
  276.           fread(songorder[d][c], loadsize, 1, handle);
  277.         }
  278.       }
  279.       // Read instruments
  280.       amount = fread8(handle);
  281.       for (c = 1; c <= amount; c++)
  282.       {
  283.         instr[c].ad = fread8(handle);
  284.         instr[c].sr = fread8(handle);
  285.         instr[c].ptr[WTBL] = fread8(handle);
  286.         instr[c].ptr[PTBL] = fread8(handle);
  287.         instr[c].ptr[FTBL] = fread8(handle);
  288.         instr[c].vibdelay = fread8(handle);
  289.         instr[c].ptr[STBL] = makespeedtable(fread8(handle), finevibrato, 0) + 1;
  290.         instr[c].gatetimer = fread8(handle);
  291.         instr[c].firstwave = fread8(handle);
  292.         fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
  293.       }
  294.       // Read tables
  295.       for (c = 0; c < MAX_TABLES-1; c++)
  296.       {
  297.         loadsize = fread8(handle);
  298.         fread(ltable[c], loadsize, 1, handle);
  299.         fread(rtable[c], loadsize, 1, handle);
  300.       }
  301.       // Read patterns
  302.       amount = fread8(handle);
  303.       for (c = 0; c < amount; c++)
  304.       {
  305.         int d;
  306.         length = fread8(handle) * 4;
  307.         fread(pattern[c], length, 1, handle);
  308.  
  309.         // Convert speedtable-requiring commands
  310.         for (d = 0; d < length; d++)
  311.         {
  312.           switch (pattern[c][d*4+2])
  313.           {
  314.             case CMD_FUNKTEMPO:
  315.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_FUNKTEMPO, 0) + 1;
  316.             break;
  317.  
  318.             case CMD_PORTAUP:
  319.             case CMD_PORTADOWN:
  320.             case CMD_TONEPORTA:
  321.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_PORTAMENTO, 0) + 1;
  322.             break;
  323.  
  324.             case CMD_VIBRATO:
  325.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], finevibrato, 0) + 1;
  326.             break;
  327.           }
  328.         }
  329.       }
  330.       countpatternlengths();
  331.       songchange();
  332.     }
  333.     // Goattracker 1.xx import
  334.     if (!memcmp(ident, "GTS!", 4))
  335.     {
  336.       int d;
  337.       int length;
  338.       int amount;
  339.       int loadsize;
  340.       int fw = 0;
  341.       int fp = 0;
  342.       int ff = 0;
  343.       int fi = 0;
  344.       int numfilter = 0;
  345.       unsigned char filtertable[256];
  346.       unsigned char filtermap[64];
  347.       int arpmap[32][256];
  348.       unsigned char pulse[32], pulseadd[32], pulselimitlow[32], pulselimithigh[32];
  349.       int filterjumppos[64];
  350.  
  351.       clearsong(1,1,1,1,1);
  352.       ok = 1;
  353.  
  354.       // Read infotexts
  355.       fread(songname, sizeof songname, 1, handle);
  356.       fread(authorname, sizeof authorname, 1, handle);
  357.       fread(copyrightname, sizeof copyrightname, 1, handle);
  358.  
  359.       // Read songorderlists
  360.       channelstoload = determinechannels(handle);
  361.       amount = fread8(handle);
  362.       for (d = 0; d < amount; d++)
  363.       {
  364.         for (c = 0; c < channelstoload; c++)
  365.         {
  366.           length = fread8(handle);
  367.           loadsize = length;
  368.           loadsize++;
  369.           fread(songorder[d][c], loadsize, 1, handle);
  370.         }
  371.       }
  372.  
  373.       // Convert instruments
  374.       for (c = 1; c < 32; c++)
  375.       {
  376.         unsigned char wavelen;
  377.  
  378.         instr[c].ad = fread8(handle);
  379.         instr[c].sr = fread8(handle);
  380.         pulse[c] = fread8(handle);
  381.         pulseadd[c] = fread8(handle);
  382.         pulselimitlow[c] = fread8(handle);
  383.         pulselimithigh[c] = fread8(handle);
  384.         instr[c].ptr[FTBL] = fread8(handle); // Will be converted later
  385.         if (instr[c].ptr[FTBL] > numfilter) numfilter = instr[c].ptr[FTBL];
  386.         if (pulse[c] & 1) instr[c].gatetimer |= 0x80; // "No hardrestart" flag
  387.         pulse[c] &= 0xfe;
  388.         wavelen = fread8(handle)/2;
  389.         fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle);
  390.         instr[c].ptr[WTBL] = fw+1;
  391.  
  392.         // Convert wavetable
  393.         for (d = 0; d < wavelen; d++)
  394.         {
  395.           if (fw < MAX_TABLELEN)
  396.           {
  397.             ltable[WTBL][fw] = fread8(handle);
  398.             rtable[WTBL][fw] = fread8(handle);
  399.             if (ltable[WTBL][fw] == 0xff)
  400.               if (rtable[WTBL][fw]) rtable[WTBL][fw] += instr[c].ptr[WTBL]-1;
  401.             if ((ltable[WTBL][fw] >= 0x8) && (ltable[WTBL][fw] <= 0xf))
  402.               ltable[WTBL][fw] |= 0xe0;
  403.             fw++;
  404.           }
  405.           else
  406.           {
  407.             fread8(handle);
  408.             fread8(handle);
  409.           }
  410.         }
  411.  
  412.         // Remove empty wavetable afterwards
  413.         if ((wavelen == 2) && (!ltable[WTBL][fw-2]) && (!rtable[WTBL][fw-2]))
  414.         {
  415.           instr[c].ptr[WTBL] = 0;
  416.           fw -= 2;
  417.           ltable[WTBL][fw] = 0;
  418.           rtable[WTBL][fw] = 0;
  419.           ltable[WTBL][fw+1] = 0;
  420.           rtable[WTBL][fw+1] = 0;
  421.         }
  422.  
  423.         // Convert pulsetable
  424.         if (pulse[c])
  425.         {
  426.           int pulsetime, pulsedist, hlpos;
  427.  
  428.           // Check for duplicate pulse settings
  429.           for (d = 1; d < c; d++)
  430.           {
  431.             if ((pulse[d] == pulse[c]) && (pulseadd[d] == pulseadd[c]) && (pulselimitlow[d] == pulselimitlow[c]) &&
  432.                 (pulselimithigh[d] == pulselimithigh[c]))
  433.             {
  434.               instr[c].ptr[PTBL] = instr[d].ptr[PTBL];
  435.               goto PULSEDONE;
  436.             }
  437.           }
  438.  
  439.           // Initial pulse setting
  440.           if (fp >= MAX_TABLELEN) goto PULSEDONE;
  441.           instr[c].ptr[PTBL] = fp+1;
  442.           ltable[PTBL][fp] = 0x80 | (pulse[c] >> 4);
  443.           rtable[PTBL][fp] = pulse[c] << 4;
  444.           fp++;
  445.  
  446.           // Pulse modulation
  447.           if (pulseadd[c])
  448.           {
  449.             int startpulse = pulse[c]*16;
  450.             int currentpulse = pulse[c]*16;
  451.             // Phase 1: From startpos to high limit
  452.             pulsedist = pulselimithigh[c]*16 - currentpulse;
  453.             if (pulsedist > 0)
  454.             {
  455.               pulsetime = pulsedist/pulseadd[c];
  456.               currentpulse += pulsetime*pulseadd[c];
  457.               while (pulsetime)
  458.               {
  459.                 int acttime = pulsetime;
  460.                 if (acttime > 127) acttime = 127;
  461.                 if (fp >= MAX_TABLELEN) goto PULSEDONE;
  462.                 ltable[PTBL][fp] = acttime;
  463.                 rtable[PTBL][fp] = pulseadd[c] / 2;
  464.                 fp++;
  465.                 pulsetime -= acttime;
  466.               }
  467.             }
  468.  
  469.             hlpos = fp;
  470.             // Phase 2: from high limit to low limit
  471.             pulsedist = currentpulse - pulselimitlow[c]*16;
  472.             if (pulsedist > 0)
  473.             {
  474.               pulsetime = pulsedist/pulseadd[c];
  475.               currentpulse -= pulsetime*pulseadd[c];
  476.               while (pulsetime)
  477.               {
  478.                 int acttime = pulsetime;
  479.                 if (acttime > 127) acttime = 127;
  480.                 if (fp >= MAX_TABLELEN) goto PULSEDONE;
  481.                 ltable[PTBL][fp] = acttime;
  482.                 rtable[PTBL][fp] = -(pulseadd[c] / 2);
  483.                 fp++;
  484.                 pulsetime -= acttime;
  485.               }
  486.             }
  487.  
  488.             // Phase 3: from low limit back to startpos/high limit
  489.             if ((startpulse < pulselimithigh[c]*16) && (startpulse > currentpulse))
  490.             {
  491.               pulsedist = startpulse - currentpulse;
  492.               if (pulsedist > 0)
  493.               {
  494.                 pulsetime = pulsedist/pulseadd[c];
  495.                 while (pulsetime)
  496.                 {
  497.                   int acttime = pulsetime;
  498.                   if (acttime > 127) acttime = 127;
  499.                   if (fp >= MAX_TABLELEN) goto PULSEDONE;
  500.                   ltable[PTBL][fp] = acttime;
  501.                   rtable[PTBL][fp] = pulseadd[c] / 2;
  502.                   fp++;
  503.                   pulsetime -= acttime;
  504.                 }
  505.               }
  506.               // Pulse jump back to beginning
  507.               if (fp >= MAX_TABLELEN) goto PULSEDONE;
  508.               ltable[PTBL][fp] = 0xff;
  509.               rtable[PTBL][fp] = instr[c].ptr[PTBL] + 1;
  510.               fp++;
  511.             }
  512.             else
  513.             {
  514.               pulsedist = pulselimithigh[c]*16 - currentpulse;
  515.               if (pulsedist > 0)
  516.               {
  517.                 pulsetime = pulsedist/pulseadd[c];
  518.                 while (pulsetime)
  519.                 {
  520.                   int acttime = pulsetime;
  521.                   if (acttime > 127) acttime = 127;
  522.                   if (fp >= MAX_TABLELEN) goto PULSEDONE;
  523.                   ltable[PTBL][fp] = acttime;
  524.                   rtable[PTBL][fp] = pulseadd[c] / 2;
  525.                   fp++;
  526.                   pulsetime -= acttime;
  527.                 }
  528.               }
  529.               // Pulse jump back to beginning
  530.               if (fp >= MAX_TABLELEN) goto PULSEDONE;
  531.               ltable[PTBL][fp] = 0xff;
  532.               rtable[PTBL][fp] = hlpos + 1;
  533.               fp++;
  534.             }
  535.           }
  536.           else
  537.           {
  538.             // Pulse stopped
  539.             if (fp >= MAX_TABLELEN) goto PULSEDONE;
  540.             ltable[PTBL][fp] = 0xff;
  541.             rtable[PTBL][fp] = 0;
  542.             fp++;
  543.           }
  544.           PULSEDONE: {}
  545.         }
  546.       }
  547.       // Convert patterns
  548.       amount = fread8(handle);
  549.       for (c = 0; c < amount; c++)
  550.       {
  551.         length = fread8(handle);
  552.         for (d = 0; d < length/3; d++)
  553.         {
  554.           unsigned char note, cmd, data, instr;
  555.           note = fread8(handle);
  556.           cmd = fread8(handle);
  557.           data = fread8(handle);
  558.           instr = cmd >> 3;
  559.           cmd &= 7;
  560.  
  561.           switch(note)
  562.           {
  563.             default:
  564.             note += FIRSTNOTE;
  565.             if (note > LASTNOTE) note = REST;
  566.             break;
  567.  
  568.             case OLDKEYOFF:
  569.             note = KEYOFF;
  570.             break;
  571.  
  572.             case OLDREST:
  573.             note = REST;
  574.             break;
  575.  
  576.             case ENDPATT:
  577.             break;
  578.           }
  579.           switch(cmd)
  580.           {
  581.             case 5:
  582.             cmd = CMD_SETFILTERPTR;
  583.             if (data > numfilter) numfilter = data;
  584.             break;
  585.  
  586.             case 7:
  587.             if (data < 0xf0)
  588.               cmd = CMD_SETTEMPO;
  589.             else
  590.             {
  591.               cmd = CMD_SETMASTERVOL;
  592.               data &= 0x0f;
  593.             }
  594.             break;
  595.           }
  596.           pattern[c][d*4] = note;
  597.           pattern[c][d*4+1] = instr;
  598.           pattern[c][d*4+2] = cmd;
  599.           pattern[c][d*4+3] = data;
  600.         }
  601.       }
  602.       countpatternlengths();
  603.       fi = highestusedinstr + 1;
  604.       songchange();
  605.  
  606.       // Read filtertable
  607.       fread(filtertable, 256, 1, handle);
  608.  
  609.       // Convert filtertable
  610.       for (c = 0; c < 64; c++)
  611.       {
  612.         filterjumppos[c] = -1;
  613.         filtermap[c] = 0;
  614.         if (filtertable[c*4+3] > numfilter) numfilter = filtertable[c*4+3];
  615.       }
  616.  
  617.       if (numfilter > 63) numfilter = 63;
  618.  
  619.       for (c = 1; c <= numfilter; c++)
  620.       {
  621.         filtermap[c] = ff+1;
  622.  
  623.         if (filtertable[c*4]|filtertable[c*4+1]|filtertable[c*4+2]|filtertable[c*4+3])
  624.         {
  625.           // Filter set
  626.           if (filtertable[c*4])
  627.           {
  628.             ltable[FTBL][ff] = 0x80 + (filtertable[c*4+1] & 0x70);
  629.             rtable[FTBL][ff] = filtertable[c*4];
  630.             ff++;
  631.             if (filtertable[c*4+2])
  632.             {
  633.               ltable[FTBL][ff] = 0x00;
  634.               rtable[FTBL][ff] = filtertable[c*4+2];
  635.               ff++;
  636.             }
  637.           }
  638.           else
  639.           {
  640.             // Filter modulation
  641.             int time = filtertable[c*4+1];
  642.  
  643.             while (time)
  644.             {
  645.               int acttime = time;
  646.               if (acttime > 127) acttime = 127;
  647.               ltable[FTBL][ff] = acttime;
  648.               rtable[FTBL][ff] = filtertable[c*4+2];
  649.               ff++;
  650.               time -= acttime;
  651.             }
  652.           }
  653.  
  654.           // Jump to next step: unnecessary if follows directly
  655.           if (filtertable[c*4+3] != c+1)
  656.           {
  657.             filterjumppos[c] = ff;
  658.             ltable[FTBL][ff] = 0xff;
  659.             rtable[FTBL][ff] = filtertable[c*4+3]; // Fix the jump later
  660.             ff++;
  661.           }
  662.         }
  663.       }
  664.  
  665.       // Now fix jumps as the filterstep mapping is known
  666.       for (c = 1; c <= numfilter; c++)
  667.       {
  668.         if (filterjumppos[c] != -1)
  669.           rtable[FTBL][filterjumppos[c]] = filtermap[rtable[FTBL][filterjumppos[c]]];
  670.       }
  671.  
  672.       // Fix filterpointers in instruments
  673.       for (c = 1; c < 32; c++)
  674.         instr[c].ptr[FTBL] = filtermap[instr[c].ptr[FTBL]];
  675.  
  676.       // Now fix pattern commands
  677.       memset(arpmap, 0, sizeof arpmap);
  678.       for (c = 0; c < MAX_PATT; c++)
  679.       {
  680.         unsigned char i = 0;
  681.         for (d = 0; d <= MAX_PATTROWS; d++)
  682.         {
  683.           if (pattern[c][d*4+1]) i = pattern[c][d*4+1];
  684.  
  685.           // Convert portamento & vibrato
  686.           if (pattern[c][d*4+2] == CMD_PORTAUP)
  687.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_PORTAMENTO, 0) + 1;
  688.           if (pattern[c][d*4+2] == CMD_PORTADOWN)
  689.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_PORTAMENTO, 0) + 1;
  690.           if (pattern[c][d*4+2] == CMD_TONEPORTA)
  691.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_PORTAMENTO, 0) + 1;
  692.           if (pattern[c][d*4+2] == CMD_VIBRATO)
  693.             pattern[c][d*4+3] = makespeedtable(pattern[c][d*4+3], MST_NOFINEVIB, 0) + 1;
  694.  
  695.           // Convert filterjump
  696.           if (pattern[c][d*4+2] == CMD_SETFILTERPTR)
  697.             pattern[c][d*4+3] = filtermap[pattern[c][d*4+3]];
  698.  
  699.           // Convert funktempo
  700.           if ((pattern[c][d*4+2] == CMD_SETTEMPO) && (!pattern[c][d*4+3]))
  701.           {
  702.             pattern[c][d*4+2] = CMD_FUNKTEMPO;
  703.             pattern[c][d*4+3] = makespeedtable((filtertable[2] << 4) | (filtertable[3] & 0x0f), MST_FUNKTEMPO, 0) + 1;
  704.           }
  705.           // Convert arpeggio
  706.           if ((pattern[c][d*4+2] == CMD_DONOTHING) && (pattern[c][d*4+3]))
  707.           {
  708.             // Must be in conjunction with a note
  709.             if ((pattern[c][d*4] >= FIRSTNOTE) && (pattern[c][d*4] <= LASTNOTE))
  710.             {
  711.               unsigned char param = pattern[c][d*4+3];
  712.               if (i)
  713.               {
  714.                 // Old arpeggio
  715.                 if (arpmap[i][param])
  716.                 {
  717.                   // As command, or as instrument?
  718.                   if (arpmap[i][param] < 256)
  719.                   {
  720.                     pattern[c][d*4+2] = CMD_SETWAVEPTR;
  721.                     pattern[c][d*4+3] = arpmap[i][param];
  722.                   }
  723.                   else
  724.                   {
  725.                     pattern[c][d*4+1] = arpmap[i][param] - 256;
  726.                     pattern[c][d*4+3] = 0;
  727.                   }
  728.                 }
  729.                 else
  730.                 {
  731.                   int e;
  732.                   unsigned char arpstart;
  733.                   unsigned char arploop;
  734.  
  735.                   // New arpeggio
  736.                   // Copy first the instrument's wavetable up to loop/end point
  737.                   arpstart = fw + 1;
  738.                   if (instr[i].ptr[WTBL])
  739.                   {
  740.                     for (e = instr[i].ptr[WTBL]-1;; e++)
  741.                     {
  742.                       if (ltable[WTBL][e] == 0xff) break;
  743.                       if (fw < MAX_TABLELEN)
  744.                       {
  745.                         ltable[WTBL][fw] = ltable[WTBL][e];
  746.                         fw++;
  747.                       }
  748.                     }
  749.                   }
  750.                   // Then make the arpeggio
  751.                   arploop = fw + 1;
  752.                   if (fw < MAX_TABLELEN-3)
  753.                   {
  754.                     ltable[WTBL][fw] = (param & 0x80) >> 7;
  755.                     rtable[WTBL][fw] = (param  & 0x70) >> 4;
  756.                     fw++;
  757.                     ltable[WTBL][fw] = (param & 0x80) >> 7;
  758.                     rtable[WTBL][fw] = (param & 0xf);
  759.                     fw++;
  760.                     ltable[WTBL][fw] = (param & 0x80) >> 7;
  761.                     rtable[WTBL][fw] = 0;
  762.                     fw++;
  763.                     ltable[WTBL][fw] = 0xff;
  764.                     rtable[WTBL][fw] = arploop;
  765.                     fw++;
  766.  
  767.                     // Create new instrument if possible
  768.                     if (fi < MAX_INSTR)
  769.                     {
  770.                       arpmap[i][param] = fi + 256;
  771.                       instr[fi] = instr[i];
  772.                       instr[fi].ptr[WTBL] = arpstart;
  773.                       // Add arpeggio parameter to new instrument name
  774.                       if (strlen(instr[fi].name) < MAX_INSTRNAMELEN-3)
  775.                       {
  776.                         char arpname[8];
  777.                         sprintf(arpname, "0%02X", param&0x7f);
  778.                         strcat(instr[fi].name, arpname);
  779.                       }
  780.                       fi++;
  781.                     }
  782.                     else
  783.                     {
  784.                       arpmap[i][param] = arpstart;
  785.                     }
  786.                   }
  787.  
  788.                   if (arpmap[i][param])
  789.                   {
  790.                     // As command, or as instrument?
  791.                     if (arpmap[i][param] < 256)
  792.                     {
  793.                       pattern[c][d*4+2] = CMD_SETWAVEPTR;
  794.                       pattern[c][d*4+3] = arpmap[i][param];
  795.                     }
  796.                     else
  797.                     {
  798.                       pattern[c][d*4+1] = arpmap[i][param] - 256;
  799.                       pattern[c][d*4+3] = 0;
  800.                     }
  801.                   }
  802.                 }
  803.               }
  804.             }
  805.             // If arpeggio could not be converted, databyte zero
  806.             if (!pattern[c][d*4+2])
  807.               pattern[c][d*4+3] = 0;
  808.           }
  809.         }
  810.       }
  811.     }
  812.     fclose(handle);
  813.   }
  814.   if (ok)
  815.   {
  816.     strcpy(loadedsongfilename, songfilename);
  817.  
  818.     // Reset table views
  819.     for (c = 0; c < MAX_TABLES; c++) settableview(c, 0);
  820.  
  821.     // Convert pulsemodulation speed of < v2.4 songs
  822.     if (ident[3] < '4')
  823.     {
  824.         for (c = 0; c < MAX_TABLELEN; c++)
  825.         {
  826.             if ((ltable[PTBL][c] < 0x80) && (rtable[PTBL][c]))
  827.             {
  828.                 int speed = ((signed char)rtable[PTBL][c]);
  829.                 speed <<= 1;
  830.                 if (speed > 127) speed = 127;
  831.                 if (speed < -128) speed = -128;
  832.                 rtable[PTBL][c] = speed;
  833.             }
  834.         }
  835.     }
  836.  
  837.     // Convert old legato/nohr parameters
  838.     if (ident[3] < '5')
  839.     {
  840.       for (c = 1; c < MAX_INSTR; c++)
  841.       {
  842.         if (instr[c].firstwave >= 0x80)
  843.         {
  844.           instr[c].gatetimer |= 0x80;
  845.           instr[c].firstwave &= 0x7f;
  846.         }
  847.         if (!instr[c].firstwave) instr[c].gatetimer |= 0x40;
  848.       }
  849.     }
  850.  
  851.  
  852.     // If was a mono song, create empty orderlists for channels 4-6
  853.     if (channelstoload < MAX_CHN)
  854.     {
  855.       int emptypatt = MAX_PATT-1;
  856.  
  857.       findusedpatterns();
  858.       for (c = 0; c < MAX_PATT; c++)
  859.       {
  860.         if (!pattused[c])
  861.         {
  862.           int d;
  863.           int ok = 1;
  864.           for (d = 0; d < pattlen[c]; d++)
  865.           {
  866.             if ((pattern[c][d*4] != REST) || (pattern[c][d*4+1] != 0x00) ||
  867.                 (pattern[c][d*4+2] != 0x00) || (pattern[c][d*4+3] != 0x00))
  868.               ok = 0;
  869.           }
  870.  
  871.           if (ok)
  872.           {
  873.             emptypatt = c;
  874.             break;
  875.           }
  876.         }
  877.       }
  878.  
  879.       for (c = 0; c < MAX_SONGS; c++)
  880.       {
  881.         if (songlen[c][0])
  882.         {
  883.           int d;
  884.           for (d = channelstoload; d < MAX_CHN; d++)
  885.           {
  886.             songorder[c][d][0] = emptypatt;
  887.             songorder[c][d][1] = 0xff;
  888.             songorder[c][d][2] = 0x00;
  889.             songlen[c][d] = 1;
  890.           }
  891.         }
  892.       }
  893.       songchange();
  894.     }
  895.   }
  896. }
  897.  
  898. void loadinstrument(void)
  899. {
  900.   char ident[4];
  901.   FILE *handle;
  902.   int c,d;
  903.   int pulsestart = -1;
  904.   int pulseend = -1;
  905.  
  906.   handle = fopen(instrfilename, "rb");
  907.   if (handle)
  908.   {
  909.     stopsong();
  910.     fread(ident, 4, 1, handle);
  911.  
  912.     if ((!memcmp(ident, "GTI3", 4)) || (!memcmp(ident, "GTI4", 4)) || (!memcmp(ident, "GTI5", 4)))
  913.     {
  914.       unsigned char optr[4];
  915.  
  916.       instr[einum].ad = fread8(handle);
  917.       instr[einum].sr = fread8(handle);
  918.       optr[0] = fread8(handle);
  919.       optr[1] = fread8(handle);
  920.       optr[2] = fread8(handle);
  921.       optr[3] = fread8(handle);
  922.       instr[einum].vibdelay = fread8(handle);
  923.       instr[einum].gatetimer = fread8(handle);
  924.       instr[einum].firstwave = fread8(handle);
  925.       fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle);
  926.  
  927.       // Erase old tabledata
  928.       deleteinstrtable(einum);
  929.  
  930.       // Load new tabledata
  931.       for (c = 0; c < MAX_TABLES; c++)
  932.       {
  933.         int start = gettablelen(c);
  934.         int len = fread8(handle);
  935.  
  936.         if (len)
  937.         {
  938.           for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  939.             ltable[c][d] = fread8(handle);
  940.           while (d < start+len)
  941.           {
  942.             fread8(handle);
  943.             d++;
  944.           }
  945.           for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  946.             rtable[c][d] = fread8(handle);
  947.           while (d < start+len)
  948.           {
  949.             fread8(handle);
  950.             d++;
  951.           }
  952.           if (c != STBL)
  953.           {
  954.             for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  955.             {
  956.               if (ltable[c][d] == 0xff)
  957.               {
  958.                 if (rtable[c][d])
  959.                   rtable[c][d] = rtable[c][d] - optr[c] + start + 1;
  960.               }
  961.             }
  962.           }
  963.           if (c == PTBL)
  964.           {
  965.               pulsestart = start;
  966.               pulseend = start + len;
  967.           }
  968.           instr[einum].ptr[c] = start + 1;
  969.         }
  970.         else instr[einum].ptr[c] = 0;
  971.       }
  972.     }
  973.  
  974.     // Goattracker v2.xx (3-table) import
  975.     if (!memcmp(ident, "GTI2", 4))
  976.     {
  977.       unsigned char optr[3];
  978.  
  979.       instr[einum].ad = fread8(handle);
  980.       instr[einum].sr = fread8(handle);
  981.       optr[0] = fread8(handle);
  982.       optr[1] = fread8(handle);
  983.       optr[2] = fread8(handle);
  984.       instr[einum].vibdelay = fread8(handle);
  985.       instr[einum].ptr[STBL] = makespeedtable(fread8(handle), finevibrato, 0) + 1;
  986.       instr[einum].gatetimer = fread8(handle);
  987.       instr[einum].firstwave = fread8(handle);
  988.       fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle);
  989.  
  990.       // Erase old tabledata
  991.       deleteinstrtable(einum);
  992.  
  993.       // Load new tabledata
  994.       for (c = 0; c < MAX_TABLES-1; c++)
  995.       {
  996.         int start = gettablelen(c);
  997.         int len = fread8(handle);
  998.  
  999.         if (len)
  1000.         {
  1001.           for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  1002.             ltable[c][d] = fread8(handle);
  1003.           while (d < start+len)
  1004.           {
  1005.             fread8(handle);
  1006.             d++;
  1007.           }
  1008.           for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  1009.             rtable[c][d] = fread8(handle);
  1010.           while (d < start+len)
  1011.           {
  1012.             fread8(handle);
  1013.             d++;
  1014.           }
  1015.           for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++)
  1016.           {
  1017.             if (ltable[c][d] == 0xff)
  1018.             {
  1019.               if (rtable[c][d])
  1020.                 rtable[c][d] = rtable[c][d] - optr[c] + start + 1;
  1021.             }
  1022.           }
  1023.           if (c == PTBL)
  1024.           {
  1025.               pulsestart = start;
  1026.               pulseend = start + len;
  1027.           }
  1028.           instr[einum].ptr[c] = start + 1;
  1029.         }
  1030.         else instr[einum].ptr[c] = 0;
  1031.       }
  1032.     }
  1033.     // Goattracker 1.xx import
  1034.     if (!memcmp(ident, "GTI!", 4))
  1035.     {
  1036.  
  1037.       unsigned char pulse, pulseadd, pulselimitlow, pulselimithigh, wavelen;
  1038.       unsigned char filtertemp[4];
  1039.       int fw, fp, ff;
  1040.  
  1041.       // Erase old tabledata
  1042.       deleteinstrtable(einum);
  1043.  
  1044.       fw = gettablelen(WTBL);
  1045.       fp = gettablelen(PTBL);
  1046.       ff = gettablelen(FTBL);
  1047.  
  1048.       instr[einum].ad = fread8(handle);
  1049.       instr[einum].sr = fread8(handle);
  1050.       if (multiplier)
  1051.         instr[einum].gatetimer = 2 * multiplier;
  1052.       else
  1053.         instr[einum].gatetimer = 1;
  1054.       instr[einum].firstwave = 0x9;
  1055.       pulse = fread8(handle);
  1056.       pulseadd = fread8(handle);
  1057.       pulselimitlow = fread8(handle);
  1058.       pulselimithigh = fread8(handle);
  1059.       instr[einum].ptr[FTBL] = fread8(handle) ? ff+1 : 0;
  1060.       if (pulse & 1) instr[einum].gatetimer |= 0x80; // "No hardrestart" flag
  1061.         wavelen = fread8(handle)/2;
  1062.       fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle);
  1063.       instr[einum].ptr[WTBL] = fw+1;
  1064.  
  1065.       // Convert wavetable
  1066.       for (d = 0; d < wavelen; d++)
  1067.       {
  1068.         if (fw < MAX_TABLELEN)
  1069.         {
  1070.           ltable[WTBL][fw] = fread8(handle);
  1071.           rtable[WTBL][fw] = fread8(handle);
  1072.           if (ltable[WTBL][fw] == 0xff)
  1073.             if (rtable[WTBL][fw]) rtable[WTBL][fw] += instr[einum].ptr[WTBL]-1;
  1074.           fw++;
  1075.         }
  1076.         else
  1077.         {
  1078.           fread8(handle);
  1079.           fread8(handle);
  1080.         }
  1081.       }
  1082.  
  1083.       // Remove empty wavetable afterwards
  1084.       if ((wavelen == 2) && (!ltable[WTBL][fw-2]) && (!rtable[WTBL][fw-2]))
  1085.       {
  1086.         instr[einum].ptr[WTBL] = 0;
  1087.         fw -= 2;
  1088.         ltable[WTBL][fw] = 0;
  1089.         rtable[WTBL][fw] = 0;
  1090.         ltable[WTBL][fw+1] = 0;
  1091.         rtable[WTBL][fw+1] = 0;
  1092.       }
  1093.  
  1094.       // Convert pulsetable
  1095.       pulse &= 0xfe;
  1096.       if (pulse)
  1097.       {
  1098.         int pulsetime, pulsedist, hlpos;
  1099.  
  1100.         // Initial pulse setting
  1101.         if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1102.         pulsestart = fp;
  1103.         instr[einum].ptr[PTBL] = fp+1;
  1104.         ltable[PTBL][fp] = 0x80 | (pulse >> 4);
  1105.         rtable[PTBL][fp] = pulse << 4;
  1106.         fp++;
  1107.  
  1108.         // Pulse modulation
  1109.         if (pulseadd)
  1110.         {
  1111.           int startpulse = pulse*16;
  1112.           int currentpulse = pulse*16;
  1113.           // Phase 1: From startpos to high limit
  1114.           pulsedist = pulselimithigh*16 - currentpulse;
  1115.           if (pulsedist > 0)
  1116.           {
  1117.             pulsetime = pulsedist/pulseadd;
  1118.             currentpulse += pulsetime*pulseadd;
  1119.             while (pulsetime)
  1120.             {
  1121.               int acttime = pulsetime;
  1122.               if (acttime > 127) acttime = 127;
  1123.               if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1124.               ltable[PTBL][fp] = acttime;
  1125.               rtable[PTBL][fp] = pulseadd / 2;
  1126.               fp++;
  1127.               pulsetime -= acttime;
  1128.             }
  1129.           }
  1130.  
  1131.           hlpos = fp;
  1132.           // Phase 2: from high limit to low limit
  1133.           pulsedist = currentpulse - pulselimitlow*16;
  1134.           if (pulsedist > 0)
  1135.           {
  1136.             pulsetime = pulsedist/pulseadd;
  1137.             currentpulse -= pulsetime*pulseadd;
  1138.             while (pulsetime)
  1139.             {
  1140.               int acttime = pulsetime;
  1141.               if (acttime > 127) acttime = 127;
  1142.               if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1143.               ltable[PTBL][fp] = acttime;
  1144.               rtable[PTBL][fp] = -(pulseadd / 2);
  1145.               fp++;
  1146.               pulsetime -= acttime;
  1147.             }
  1148.           }
  1149.  
  1150.           // Phase 3: from low limit back to startpos/high limit
  1151.           if ((startpulse < pulselimithigh*16) && (startpulse > currentpulse))
  1152.           {
  1153.             pulsedist = startpulse - currentpulse;
  1154.             if (pulsedist > 0)
  1155.             {
  1156.               pulsetime = pulsedist/pulseadd;
  1157.               while (pulsetime)
  1158.               {
  1159.                 int acttime = pulsetime;
  1160.                 if (acttime > 127) acttime = 127;
  1161.                 if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1162.                 ltable[PTBL][fp] = acttime;
  1163.                 rtable[PTBL][fp] = pulseadd / 2;
  1164.                 fp++;
  1165.                 pulsetime -= acttime;
  1166.               }
  1167.             }
  1168.             // Pulse jump back to beginning
  1169.             if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1170.             ltable[PTBL][fp] = 0xff;
  1171.             rtable[PTBL][fp] = instr[einum].ptr[PTBL] + 1;
  1172.             fp++;
  1173.           }
  1174.           else
  1175.           {
  1176.             pulsedist = pulselimithigh*16 - currentpulse;
  1177.             if (pulsedist > 0)
  1178.             {
  1179.               pulsetime = pulsedist/pulseadd;
  1180.               while (pulsetime)
  1181.               {
  1182.                 int acttime = pulsetime;
  1183.                 if (acttime > 127) acttime = 127;
  1184.                 if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1185.                 ltable[PTBL][fp] = acttime;
  1186.                 rtable[PTBL][fp] = pulseadd / 2;
  1187.                 fp++;
  1188.                 pulsetime -= acttime;
  1189.               }
  1190.             }
  1191.             // Pulse jump back to beginning
  1192.             if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1193.             ltable[PTBL][fp] = 0xff;
  1194.             rtable[PTBL][fp] = hlpos + 1;
  1195.             fp++;
  1196.           }
  1197.         }
  1198.         else
  1199.         {
  1200.           // Pulse stopped
  1201.           if (fp >= MAX_TABLELEN) goto PULSEDONE;
  1202.           ltable[PTBL][fp] = 0xff;
  1203.           rtable[PTBL][fp] = 0;
  1204.           fp++;
  1205.         }
  1206.         PULSEDONE:
  1207.         pulseend = fp;
  1208.       }
  1209.  
  1210.       // Convert filter (if any)
  1211.       if ((instr[einum].ptr[FTBL]) && (ff < MAX_TABLELEN-2))
  1212.       {
  1213.         fread(filtertemp, sizeof filtertemp, 1, handle);
  1214.         // Filter set
  1215.         if (filtertemp[0])
  1216.         {
  1217.           ltable[FTBL][ff] = 0x80 + (filtertemp[1] & 0x70);
  1218.           rtable[FTBL][ff] = filtertemp[0];
  1219.           ff++;
  1220.           if (filtertemp[2])
  1221.           {
  1222.             ltable[FTBL][ff] = 0x00;
  1223.             rtable[FTBL][ff] = filtertemp[2];
  1224.             ff++;
  1225.           }
  1226.         }
  1227.         else
  1228.         {
  1229.           // Filter modulation
  1230.           int time = filtertemp[1];
  1231.  
  1232.           while (time)
  1233.           {
  1234.             int acttime = time;
  1235.             if (acttime > 127) acttime = 127;
  1236.             ltable[FTBL][ff] = acttime;
  1237.             rtable[FTBL][ff] = filtertemp[2];
  1238.             ff++;
  1239.             time -= acttime;
  1240.           }
  1241.         }
  1242.  
  1243.         // Jump to next step: always end the filter
  1244.         ltable[FTBL][ff] = 0xff;
  1245.         rtable[FTBL][ff] = 0;
  1246.         ff++;
  1247.       }
  1248.     }
  1249.  
  1250.     fclose(handle);
  1251.  
  1252.     // Convert pulsemodulation speed of < v2.4 instruments
  1253.     if ((ident[3] < '4') && (pulsestart != -1))
  1254.     {
  1255.         for (c = pulsestart; (c < pulseend) && (c < MAX_TABLELEN); c++)
  1256.         {
  1257.             if ((ltable[PTBL][c] < 0x80) && (rtable[PTBL][c]))
  1258.             {
  1259.                 int speed = ((signed char)rtable[PTBL][c]);
  1260.                 speed <<= 1;
  1261.                 if (speed > 127) speed = 127;
  1262.                 if (speed < -128) speed = -128;
  1263.                 rtable[PTBL][c] = speed;
  1264.             }
  1265.         }
  1266.     }
  1267.     // Convert old legato/nohr parameters
  1268.     if (ident[3] < '5')
  1269.     {
  1270.       if (instr[einum].firstwave >= 0x80)
  1271.       {
  1272.         instr[einum].firstwave &= 0x7f;
  1273.         instr[einum].gatetimer |= 0x80;
  1274.       }
  1275.       if (!instr[einum].firstwave) instr[einum].gatetimer |= 0x40;
  1276.     }
  1277.   }
  1278. }
  1279.  
  1280. void clearsong(int cs, int cp, int ci, int ct, int cn)
  1281. {
  1282.   int c;
  1283.  
  1284.   if (!(cs | cp | ci | ct | cn)) return;
  1285.  
  1286.   stopsong();
  1287.  
  1288.   masterfader = 0x0f;
  1289.   epmarkchn = -1;
  1290.   etmarknum = -1;
  1291.   esmarkchn = -1;
  1292.   followplay = 0;
  1293.  
  1294.   for (c = 0; c < MAX_CHN; c++)
  1295.   {
  1296.     int d;
  1297.     chn[c].mute = 0;
  1298.     if (multiplier)
  1299.       chn[c].tempo = multiplier*6-1;
  1300.     else
  1301.       chn[c].tempo = 6-1;
  1302.     chn[c].pattptr = 0;
  1303.     if (cs)
  1304.     {
  1305.       memset(loadedsongfilename, 0, sizeof loadedsongfilename);
  1306.       for (d = 0; d < MAX_SONGS; d++)
  1307.       {
  1308.         memset(&songorder[d][c][0], 0, MAX_SONGLEN+2);
  1309.         if (!d)
  1310.         {
  1311.           songorder[d][c][0] = c;
  1312.           songorder[d][c][1] = LOOPSONG;
  1313.         }
  1314.         else
  1315.         {
  1316.           songorder[d][c][0] = LOOPSONG;
  1317.         }
  1318.       }
  1319.       epnum[c] = songorder[0][c][0];
  1320.       espos[c] = 0;
  1321.       esend[c] = 0;
  1322.     }
  1323.   }
  1324.   if (cs)
  1325.   {
  1326.     esview = 0;
  1327.     eseditpos = 0;
  1328.     escolumn = 0;
  1329.     eschn = 0;
  1330.     esnum = 0;
  1331.     eppos = 0;
  1332.     epview =-VISIBLEPATTROWS/2;
  1333.     epcolumn = 0;
  1334.     epchn = 0;
  1335.   }
  1336.   if (cn)
  1337.   {
  1338.     memset(songname, 0, sizeof songname);
  1339.     memset(authorname, 0, sizeof authorname);
  1340.     memset(copyrightname, 0, sizeof copyrightname);
  1341.     enpos = 0;
  1342.   }
  1343.   if (cp)
  1344.   {
  1345.     memset(loadedsongfilename, 0, sizeof loadedsongfilename);
  1346.     for (c = 0; c < MAX_PATT; c++)
  1347.         clearpattern(c);
  1348.   }
  1349.   if (ci)
  1350.   {
  1351.     for (c = 0; c < MAX_INSTR; c++)
  1352.       clearinstr(c);
  1353.     memset(&instrcopybuffer, 0, sizeof(INSTR));
  1354.     eipos = 0;
  1355.     eicolumn = 0;
  1356.     einum = 1;
  1357.   }
  1358.   if (ct == 1)
  1359.   {
  1360.     for (c = MAX_TABLES-1; c >= 0; c--)
  1361.     {
  1362.       memset(ltable[c], 0, MAX_TABLELEN);
  1363.       memset(rtable[c], 0, MAX_TABLELEN);
  1364.       settableview(c, 0);
  1365.     }
  1366.   }
  1367.   countpatternlengths();
  1368. }
  1369.  
  1370. void countpatternlengths(void)
  1371. {
  1372.   int c, d, e;
  1373.  
  1374.   highestusedpattern = 0;
  1375.   highestusedinstr = 0;
  1376.   for (c = 0; c < MAX_PATT; c++)
  1377.   {
  1378.     for (d = 0; d <= MAX_PATTROWS; d++)
  1379.     {
  1380.       if (pattern[c][d*4] == ENDPATT) break;
  1381.       if ((pattern[c][d*4] != REST) || (pattern[c][d*4+1]) || (pattern[c][d*4+2]) || (pattern[c][d*4+3]))
  1382.         highestusedpattern = c;
  1383.       if (pattern[c][d*4+1] > highestusedinstr) highestusedinstr = pattern[c][d*4+1];
  1384.     }
  1385.     pattlen[c] = d;
  1386.   }
  1387.  
  1388.   for (e = 0; e < MAX_SONGS; e++)
  1389.   {
  1390.     for (c = 0; c < MAX_CHN; c++)
  1391.     {
  1392.       for (d = 0; d < MAX_SONGLEN; d++)
  1393.       {
  1394.         if (songorder[e][c][d] >= LOOPSONG) break;
  1395.         if ((songorder[e][c][d] < REPEAT) && (songorder[e][c][d] > highestusedpattern))
  1396.           highestusedpattern = songorder[e][c][d];
  1397.       }
  1398.       songlen[e][c] = d;
  1399.     }
  1400.   }
  1401. }
  1402.  
  1403. void countthispattern(void)
  1404. {
  1405.   int c, d, e;
  1406.  
  1407.   c = epnum[epchn];
  1408.   for (d = 0; d <= MAX_PATTROWS; d++)
  1409.   {
  1410.     if (pattern[c][d*4] == ENDPATT) break;
  1411.   }
  1412.   pattlen[c] = d;
  1413.  
  1414.   e = esnum;
  1415.   c = eschn;
  1416.   for (d = 0; d < MAX_SONGLEN; d++)
  1417.   {
  1418.     if (songorder[e][c][d] >= LOOPSONG) break;
  1419.     if (songorder[e][c][d] > highestusedpattern)
  1420.       highestusedpattern = songorder[e][c][d];
  1421.   }
  1422.   songlen[e][c] = d;
  1423. }
  1424.  
  1425. int insertpattern(int p)
  1426. {
  1427.   int c, d, e;
  1428.  
  1429.   findusedpatterns();
  1430.   if (p >= MAX_PATT-2) return 0;
  1431.   if (pattused[MAX_PATT-1]) return 0;
  1432.   memmove(pattern[p+2], pattern[p+1], (MAX_PATT-p-2)*(MAX_PATTROWS*4+4));  
  1433.   countpatternlengths();
  1434.  
  1435.   for (c = 0; c < MAX_SONGS; c++)
  1436.   {
  1437.     if ((songlen[c][0]) &&
  1438.         (songlen[c][1]) &&
  1439.         (songlen[c][2]))
  1440.     {
  1441.       for (d = 0; d < MAX_CHN; d++)
  1442.       {
  1443.         for (e = 0; e < songlen[c][d]; e++)
  1444.         {
  1445.           if ((songorder[c][d][e] < REPEAT) && (songorder[c][d][e] > p) && (songorder[c][d][e] != MAX_PATT-1))
  1446.             songorder[c][d][e]++;
  1447.         }
  1448.       }
  1449.     }
  1450.   }
  1451.  
  1452.   for (c = 0; c < MAX_CHN; c++)
  1453.   {
  1454.       if ((epnum[c] > p) && (epnum[c] != MAX_PATT-1)) epnum[c]++;
  1455.   }
  1456.  
  1457.   return 1;
  1458. }
  1459.  
  1460. void deletepattern(int p)
  1461. {
  1462.   int c, d, e;
  1463.  
  1464.   if (p == MAX_PATT-1) return;
  1465.  
  1466.   memmove(pattern[p], pattern[p+1], (MAX_PATT-p-1)*(MAX_PATTROWS*4+4));
  1467.   clearpattern(MAX_PATT-1);
  1468.   countpatternlengths();
  1469.  
  1470.   for (c = 0; c < MAX_SONGS; c++)
  1471.   {
  1472.     if ((songlen[c][0]) &&
  1473.         (songlen[c][1]) &&
  1474.         (songlen[c][2]))
  1475.     {
  1476.       for (d = 0; d < MAX_CHN; d++)
  1477.       {
  1478.         for (e = 0; e < songlen[c][d]; e++)
  1479.         {
  1480.           if ((songorder[c][d][e] < REPEAT) && (songorder[c][d][e] > p))
  1481.             songorder[c][d][e]--;
  1482.         }
  1483.       }
  1484.     }
  1485.   }
  1486.  
  1487.   for (c = 0; c < MAX_CHN; c++)
  1488.   {
  1489.       if (epnum[c] > p) epnum[c]--;
  1490.   }
  1491. }
  1492.  
  1493. void clearpattern(int p)
  1494. {
  1495.   int c;
  1496.  
  1497.   memset(pattern[p], 0, MAX_PATTROWS*4);
  1498.   for (c = 0; c < defaultpatternlength; c++) pattern[p][c*4] = REST;
  1499.   for (c = defaultpatternlength; c <= MAX_PATTROWS; c++) pattern[p][c*4] = ENDPATT;
  1500. }
  1501.  
  1502. void findusedpatterns(void)
  1503. {
  1504.   int c, d, e;
  1505.  
  1506.   countpatternlengths();
  1507.   memset(pattused, 0, sizeof pattused);
  1508.   for (c = 0; c < MAX_SONGS; c++)
  1509.   {
  1510.     if ((songlen[c][0]) &&
  1511.         (songlen[c][1]) &&
  1512.         (songlen[c][2]))
  1513.     {
  1514.       for (d = 0; d < MAX_CHN; d++)
  1515.       {
  1516.         for (e = 0; e < songlen[c][d]; e++)
  1517.         {
  1518.           if (songorder[c][d][e] < REPEAT)
  1519.             pattused[songorder[c][d][e]] = 1;
  1520.         }
  1521.       }
  1522.     }
  1523.   }
  1524. }
  1525.  
  1526. void findduplicatepatterns(void)
  1527. {
  1528.   int c, d;
  1529.  
  1530.   findusedpatterns();
  1531.  
  1532.   for (c = 0; c < MAX_PATT; c++)
  1533.   {
  1534.       if (pattused[c])
  1535.       {
  1536.         for (d = c+1; d < MAX_PATT; d++)
  1537.         {
  1538.             if (pattlen[d] == pattlen[c])
  1539.             {
  1540.           if (!memcmp(pattern[c], pattern[d], pattlen[c]*4))
  1541.           {
  1542.               int f, g, h;
  1543.  
  1544.             for (f = 0; f < MAX_SONGS; f++)
  1545.             {
  1546.               if ((songlen[f][0]) &&
  1547.                   (songlen[f][1]) &&
  1548.                   (songlen[f][2]))
  1549.               {
  1550.                 for (g = 0; g < MAX_CHN; g++)
  1551.                 {
  1552.                   for (h = 0; h < songlen[f][g]; h++)
  1553.                   {
  1554.                     if (songorder[f][g][h] == d)
  1555.                       songorder[f][g][h] = c;
  1556.                   }
  1557.                 }
  1558.               }
  1559.             }
  1560.             for (f = 0; f < MAX_CHN; f++)
  1561.                 if (epnum[f] == d) epnum[f] = c;
  1562.           }
  1563.         }
  1564.       }
  1565.     }
  1566.   }
  1567.  
  1568.   findusedpatterns();
  1569. }
  1570.  
  1571. void optimizeeverything(int oi, int ot)
  1572. {
  1573.   int c, d, e;
  1574.  
  1575.   stopsong();
  1576.  
  1577.   findduplicatepatterns();
  1578.  
  1579.   memset(instrused, 0, sizeof instrused);
  1580.  
  1581.   for (c = MAX_PATT-1; c >= 0; c--)
  1582.   {
  1583.     if (pattused[c])
  1584.     {
  1585.       for (d = 0; d < MAX_PATTROWS; d++)
  1586.       {
  1587.         if (pattern[c][d*4] == ENDPATT) break;
  1588.         if (pattern[c][d*4+1])
  1589.           instrused[pattern[c][d*4+1]] = 1;
  1590.       }
  1591.     }
  1592.     else deletepattern(c);
  1593.   }
  1594.  
  1595.   countpatternlengths();
  1596.  
  1597.   if (oi)
  1598.   {
  1599.     for (c = MAX_INSTR-2; c >= 1; c--)
  1600.     {
  1601.       if (!instrused[c])
  1602.       {
  1603.           clearinstr(c);
  1604.  
  1605.         if (c < MAX_INSTR-2)
  1606.         {
  1607.           memmove(&instr[c], &instr[c+1], (MAX_INSTR-2-c) * sizeof(INSTR));
  1608.           clearinstr(MAX_INSTR-2);
  1609.           for (d = 0; d < MAX_PATT; d++)
  1610.           {
  1611.             for (e = 0; e < pattlen[d]; e++)
  1612.             {
  1613.               if ((pattern[d][e*4+1] > c) && (pattern[d][e*4+1] != MAX_INSTR-1))
  1614.                 pattern[d][e*4+1]--;
  1615.             }
  1616.           }
  1617.         }
  1618.       }
  1619.     }
  1620.   }
  1621.  
  1622.   if (ot)
  1623.   {
  1624.     for (c = 0; c < MAX_TABLES; c++) optimizetable(c);
  1625.   }
  1626. }
  1627.  
  1628. int determinechannels(FILE* handle)
  1629. {
  1630.   int returnpos = ftell(handle);
  1631.   int c, d;
  1632.   int songs = fread8(handle);
  1633.   unsigned char songbuffer[257];
  1634.  
  1635.   for (d = 0; d < songs; d++)
  1636.   {
  1637.     for (c = 0; c < MAX_CHN; c++)
  1638.     {
  1639.       int loadsize = fread8(handle);
  1640.       loadsize++;
  1641.       memset(songbuffer, 0, 257);
  1642.       fread(songbuffer, loadsize, 1, handle);
  1643.  
  1644.       // Check that each track of each song has a valid endmark.
  1645.       // Should fail if it's a mono song (not certain)
  1646.       if ((songbuffer[loadsize - 2] != 0xff) || (songbuffer[loadsize - 1] >= loadsize))
  1647.       {
  1648.         fseek(handle, returnpos, SEEK_SET);
  1649.         return 3;
  1650.       }
  1651.     }
  1652.   }
  1653.  
  1654.   fseek(handle, returnpos, SEEK_SET);
  1655.   return MAX_CHN;
  1656. }
  1657.  
  1658.