Guest User

Untitled

a guest
Oct 16th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1. int loadPatterns(IT_MAIN *newModule, FILE *modHandle)
  2. {
  3.     int i, j;
  4.  
  5.     for (i = 0; i < newModule->PatNum; i++)
  6.     {
  7.         if (newModule->patOffsets[i] == 0)
  8.         {
  9.             newModule->patLengths[i] = 64;
  10.  
  11.             newModule->pat[i] = (IT_NOTE *)malloc(sizeof (IT_NOTE) * 64 * 64);
  12.  
  13.             for (j = 0; j < 64 * 64; j++)
  14.             {
  15.                 newModule->pat[i]->note = 0;
  16.                 newModule->pat[i]->inst = 255;
  17.                 newModule->pat[i]->volCol = 255;
  18.                 newModule->pat[i]->cmd = 0;
  19.                 newModule->pat[i]->param = 0;
  20.  
  21.             }
  22.  
  23.             continue;
  24.         }
  25.  
  26.         fseek(modHandle, newModule->patOffsets[i], SEEK_SET);
  27.  
  28.         {
  29.             unsigned char lastMaskVariable[64];
  30.             IT_NOTE lastValue[64];
  31.  
  32.             IT_NOTE *p;
  33.  
  34.             int packLength = 0, row = 0, bytesRead = 0;
  35.             unsigned char channel = 0, maskVariable, channelVariable;
  36.  
  37.             for (j = 0; j < 64; j++)
  38.             {
  39.                 lastValue[j].note = 0;
  40.                 lastValue[j].inst = 255;
  41.                 lastValue[j].volCol = 255;
  42.                 lastValue[j].cmd = 0;
  43.                 lastValue[j].param = 0;
  44.  
  45.                 lastMaskVariable[j] = 0;
  46.             }
  47.  
  48.             fread(&packLength, 2, 1, modHandle);
  49.             fread(&newModule->patLengths[i], 2, 1, modHandle);
  50.             fseek(modHandle, 4, SEEK_CUR);
  51.        
  52.             p = newModule->pat[i] = (IT_NOTE *)malloc(sizeof (IT_NOTE) * newModule->patLengths[i] * 64);
  53.             if (p == NULL)
  54.             {
  55.                 for (i = 0; i < 199; i++)
  56.                 {
  57.                     if (newModule->pat[i] != NULL)
  58.                     {
  59.                         free(newModule->pat[i]);
  60.                     }
  61.                 }
  62.  
  63.                 return 0;
  64.             }
  65.  
  66.             for (j = 0; j < (64 * 64); j++)
  67.             {
  68.                 p[i].note = 0;
  69.                 p[i].inst = 255;
  70.                 p[i].volCol = 255;
  71.                 p[i].cmd = 0;
  72.                 p[i].param = 0;
  73.             }
  74.  
  75.             while (row < newModule->patLengths[i])
  76.             {
  77.                 IT_NOTE n;
  78.  
  79.                 fread(&channelVariable, 1, 1, modHandle);
  80.                 if (channelVariable == 0)
  81.                 {
  82.                     row++;
  83.                     continue;
  84.                 }
  85.            
  86.                 channel = (channelVariable - 1) & 63;
  87.  
  88.                 if (channelVariable & 128)
  89.                 {
  90.                     fread(&maskVariable, 1, 1, modHandle);
  91.  
  92.                     lastMaskVariable[channel] = maskVariable;
  93.                 }
  94.  
  95.                 if (lastMaskVariable[channel] & 1)
  96.                 {
  97.                     unsigned char note;
  98.                     fread(&note, 1, 1, modHandle);
  99.  
  100.                     if (note == 255)
  101.                     {
  102.                         lastValue[channel].note = n.note = 255;
  103.                         p[(row * 64) + channel].note = n.note;
  104.                     }
  105.                     else if (note == 254)
  106.                     {
  107.                         lastValue[channel].note = n.note = 254;
  108.                         p[(row * 64) + channel].note = n.note;
  109.                     }
  110.                     else if (note <= 120)
  111.                     {
  112.                         lastValue[channel].note = n.note = note;
  113.                         p[(row * 64) + channel].note = n.note;
  114.                     }
  115.                 }
  116.  
  117.                 if (lastMaskVariable[channel] & 2)
  118.                 {
  119.                     unsigned char instrument;
  120.                     fread(&instrument, 1, 1, modHandle);
  121.  
  122.                     if (instrument <= 99)
  123.                     {
  124.                         lastValue[channel].inst = n.inst = instrument - 1;
  125.                         p[(row * 64) + channel].inst = n.inst;
  126.                     }
  127.                 }
  128.  
  129.                 if (lastMaskVariable[channel] & 4)
  130.                 {
  131.                     unsigned char volCol;
  132.                     fread(&volCol, 1, 1, modHandle);
  133.  
  134.                     if (volCol <= 212)
  135.                     {
  136.                         lastValue[channel].volCol = n.volCol = volCol;
  137.                         p[(row * 64) + channel].volCol = n.volCol;
  138.                     }
  139.                 }
  140.  
  141.                 if (lastMaskVariable[channel] & 8)
  142.                 {
  143.                     unsigned char cmd, param;
  144.  
  145.                     fread(&cmd, 1, 1, modHandle);
  146.                     fread(&param, 1, 1, modHandle);
  147.  
  148.                     if (cmd > 0)
  149.                     {
  150.                         lastValue[channel].cmd = n.cmd = cmd;
  151.                         p[(row * 64) + channel].cmd = n.cmd;
  152.                     }
  153.  
  154.                     lastValue[channel].param = n.param = param;
  155.                     p[(row * 64) + channel].param = n.param;
  156.                 }
  157.  
  158.                 if (lastMaskVariable[channel] & 16)
  159.                 {
  160.                     n.note = lastValue[channel].note;
  161.                     p[(row * 64) + channel].note = n.note;
  162.                 }
  163.  
  164.                 if (lastMaskVariable[channel] & 32)
  165.                 {
  166.                     n.inst = lastValue[channel].inst;
  167.                     p[(row * 64) + channel].inst = n.inst;
  168.                 }
  169.  
  170.                 if (lastMaskVariable[channel] & 64)
  171.                 {
  172.                     n.volCol = lastValue[channel].volCol;
  173.                     p[(row * 64) + channel].volCol = n.volCol;
  174.                 }
  175.  
  176.                 if (lastMaskVariable[channel] & 128)
  177.                 {
  178.                     n.cmd = lastValue[channel].cmd;
  179.                     n.param = lastValue[channel].param;
  180.                     p[(row * 64) + channel].cmd = n.cmd;
  181.                     p[(row * 64) + channel].param = n.param;
  182.                 }
  183.             }
  184.         }
  185.     }
  186.  
  187.     return 1;
  188. }
Add Comment
Please, Sign In to add comment