home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / audio / tracker / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  2014-05-19  |  12.3 KB  |  536 lines

  1. /* read.c */
  2.  
  3. /* $Author: espie $
  4.  * $Id: read.c,v 2.7 1991/12/03 23:03:39 espie Exp espie $
  5.  * $Revision: 2.7 $
  6.  * $Log: read.c,v $
  7.  * Revision 2.7  1991/12/03  23:03:39  espie
  8.  * Added transpose feature.
  9.  *
  10.  * Revision 2.6  1991/12/03  21:24:53  espie
  11.  * Feature fix: length 1 sample should be empty.
  12.  *
  13.  * Revision 2.5  1991/12/03  17:10:11  espie
  14.  * Corrected repeat length problems concerning badly formed files,
  15.  * added signature checking for new tracker files.
  16.  *
  17.  * Revision 2.4  1991/11/19  16:07:19  espie
  18.  * Added comments, moved minor stuff around.
  19.  *
  20.  * Revision 2.3  1991/11/18  14:10:30  espie
  21.  * Corrected small problem with repeat being too short.
  22.  *
  23.  * Revision 2.2  1991/11/18  01:10:45  espie
  24.  * Minor corrections.
  25.  *
  26.  * Revision 2.1  1991/11/17  23:07:58  espie
  27.  * Coded error types. More amiga specific stuff.
  28.  *
  29.  * Revision 2.0  1991/11/17  21:42:08  espie
  30.  * New version.
  31.  *
  32.  * Revision 1.17  1991/11/17  16:30:48  espie
  33.  * Forgot to return a song_info from new_song_info
  34.  *
  35.  * Revision 1.16  1991/11/16  15:50:34  espie
  36.  * Tabs.
  37.  *
  38.  * Revision 1.15  1991/11/16  15:42:43  espie
  39.  * Rationnalized error recovery.
  40.  * There was a bug: you could try to deallocate
  41.  * stuff in no-noland. Also, strings never got
  42.  * to be freed.
  43.  *
  44.  * Revision 1.14  1991/11/15  20:57:34  espie
  45.  * Centralized error control to error_song.
  46.  *
  47.  * Revision 1.13  1991/11/15  18:22:10  espie
  48.  * Added a new test on length, aborts most modules now.
  49.  * Maybe should say it as well.
  50.  *
  51.  * Revision 1.12  1991/11/10  16:26:14  espie
  52.  * Nasty bug regarding evaluation order in getulong.
  53.  * Bitten on the sparc.
  54.  *
  55.  * Revision 1.11  1991/11/09  17:47:33  espie
  56.  * Added checkpoints for early return if file too short.
  57.  *
  58.  * Revision 1.10  1991/11/08  13:35:57  espie
  59.  * Added memory recovery and error control.
  60.  *
  61.  * Revision 1.9  1991/11/08  12:37:37  espie
  62.  * Bug in checkfp: should return an int
  63.  * because characters are signed on some machines,
  64.  * and we want to use it as un unsigned value
  65.  * for computing short values.
  66.  *
  67.  * Revision 1.8  1991/11/07  23:29:09  espie
  68.  * Suppressed ! warning for bad note.
  69.  *
  70.  * Revision 1.7  1991/11/07  21:40:16  espie
  71.  * Added note support.
  72.  *
  73.  * Revision 1.6  1991/11/07  20:12:34  espie
  74.  * Minor problem with version id.
  75.  *
  76.  * Revision 1.5  1991/11/07  20:11:10  espie
  77.  * Added embedded version id.
  78.  *
  79.  * Revision 1.4  1991/11/07  15:27:02  espie
  80.  * Added some error control, essentially checkgetc.
  81.  *
  82.  * Revision 1.3  1991/11/05  22:49:03  espie
  83.  * Modified the format of the dump slightly for
  84.  * a better readability.
  85.  *
  86.  * Revision 1.2  1991/11/04  20:27:05  espie
  87.  * Corrected length and rep_length/rep_offset
  88.  * which are given in words and should be converted to
  89.  * bytes.
  90.  *
  91.  * Revision 1.1  1991/11/04  13:23:59  espie
  92.  * Initial revision
  93.  *
  94.  *
  95.  */
  96.  
  97. #include <malloc.h>
  98. #include <stdio.h>
  99. #include <string.h>
  100.  
  101. #include "extern.h"
  102. #include "machine.h"
  103. #include "song.h"
  104. #include "channel.h"
  105.  
  106. static char *id = "$Id: read.c,v 2.7 1991/12/03 23:03:39 espie Exp espie $";
  107.  
  108. static int transpose;
  109.  
  110. int find_note(pitch)
  111. int pitch;
  112.     {
  113.     int a, b, i;
  114.     if (pitch == 0)
  115.         return -1;
  116.     a = 0;
  117.     b = NUMBER_NOTES-1;
  118.     while(b-a > 1)
  119.         {
  120.         i = (a+b)/2;
  121.         if (pitch_table[i] == pitch)
  122.             return i + transpose;
  123.         if (pitch_table[i] > pitch)
  124.             a = i;
  125.         else
  126.             b = i;
  127.         }
  128.     if (pitch_table[a] - FUZZ <= pitch)
  129.         return a + transpose;
  130.     if (pitch_table[b] + FUZZ >= pitch)
  131.         return b + transpose;
  132.     return NO_NOTE;
  133.     }
  134.  
  135. /* c = checkgetc(f):
  136.  * gets a character from file f.
  137.  * Aborts program if file f is finished
  138.  */
  139.  
  140. int checkgetc(f)
  141. FILE *f;
  142.     {
  143.     int c;
  144.  
  145.     if ((c = fgetc(f)) == EOF)
  146.         error = FILE_TOO_SHORT;
  147.     return c;
  148.     }
  149.  
  150. /* s = getstring(f, len):
  151.  * gets a soundtracker string from file f.
  152.  * I.e, it is a fixed length string terminated
  153.  * by a 0 if too short
  154.  */
  155.  
  156. #define MAX_LEN 50
  157.  
  158. char *getstring(f, len)
  159. FILE *f;
  160. int len;
  161.     {
  162.     static char s[MAX_LEN];
  163.     char *new;
  164.     int i;
  165.  
  166.         
  167.     for (i = 0; i < len; i++)
  168.         s[MIN(i, MAX_LEN - 1)] = checkgetc(f);
  169.     s[MIN(len, MAX_LEN - 1)] = '\0';
  170.     new = malloc(strlen(s)+1);
  171.  
  172.     return strcpy(new, s);
  173.     }
  174.  
  175. /* byteskip(f, len)
  176.  * same as fseek, xcpt it works on stdin
  177.  */
  178.  
  179. void byteskip(f, len)
  180. FILE *f;
  181. int len;
  182.     {
  183.     int i;
  184.  
  185.     for (i = 0; i < len; i++)
  186.         checkgetc(f);
  187.     }
  188.  
  189. /* v = getushort(f)
  190.  * reads an unsigned short from f
  191.  */
  192.  
  193. int getushort(f)
  194. FILE *f;
  195.     {
  196.     int i;
  197.  
  198.     i = checkgetc(f) << 8;
  199.     return i | checkgetc(f);
  200.     }
  201.  
  202. void fill_sample_info(info, f)
  203. struct sample_info *info;
  204. FILE *f;
  205.     {
  206.     info->name = getstring(f, 22);
  207.     info->length = getushort(f);
  208.     info->finetune = checkgetc(f);
  209.     info->volume = checkgetc(f);
  210.     info->volume = MIN(info->volume, MAX_VOLUME);
  211.     info->rp_offset = getushort(f);
  212.     info->rp_length = getushort(f);
  213.  
  214.     /* the next check is for old modules for which
  215.      * the sample data types are a bit confused, so
  216.      * that what we were expecting to be #words is #bytes.
  217.      */
  218.         /* not sure I understand the -1 myself, though it's
  219.          * necessary to play kawai-k1 correctly 
  220.          */
  221.     if (info->rp_length + info->rp_offset - 1 > info->length)
  222.         info->rp_offset /= 2;
  223.     
  224.     if (info->rp_length + info->rp_offset > info->length)
  225.         info->rp_length = info->length - info->rp_offset;
  226.  
  227.     info->length *= 2;
  228.     info->rp_offset *= 2;
  229.     info->rp_length *= 2;
  230.         /* in all logic, a 2-sized sample could exist,
  231.          * but this is not the case, and even so, some
  232.          * trackers output empty instruments as being 2-sized.
  233.          */
  234.     if (info->length <= 2)
  235.         return;
  236.  
  237.     info->start = (SAMPLE *)calloc(info->length, 1);
  238.  
  239.     if (info->rp_length > 2)
  240.         info->rp_start = info->start + info->rp_offset;
  241.     else
  242.         info->rp_start = NULL;
  243.     
  244.     if (info->length > MAX_SAMPLE_LENGTH)
  245.         error = CORRUPT_FILE;
  246.     }
  247.  
  248. void fill_song_info(info, f)
  249. struct song_info *info;
  250. FILE *f;
  251.     {
  252.     int i;
  253.     int p;
  254.  
  255.     info->length = checkgetc(f);
  256.     checkgetc(f);
  257.     info->maxpat = -1;
  258.     for (i = 0; i < NUMBER_PATTERNS; i++)
  259.         {
  260.         p = getc(f);
  261.         if (p >= NUMBER_PATTERNS)
  262.             p = 0;
  263.         if (p > info->maxpat)
  264.             info->maxpat = p;
  265.         info->patnumber[i] = p;
  266.         }
  267.     info->maxpat++;
  268.     if (info->maxpat == 0 || info->length == 0)
  269.         error = CORRUPT_FILE;
  270.     }
  271.  
  272. void fill_event(e, f)
  273. struct event *e;
  274. FILE *f;
  275.     {
  276.     int a, b, c, d;
  277.  
  278.     a = checkgetc(f);
  279.     b = checkgetc(f);
  280.     c = checkgetc(f);
  281.     d = checkgetc(f);
  282.     e->sample_number = a & 0x10 | (c >> 4);
  283.     e->effect = c & 0xf;
  284.     e->parameters = d;
  285.     e->pitch = ( (a & 15) << 8 ) | b;
  286.     e->note = find_note(e->pitch);
  287.     }
  288.  
  289. void fill_pattern(pattern, f)
  290. struct block *pattern;
  291. FILE *f;
  292.     {
  293.     int i, j;
  294.  
  295.     for (i = 0; i < BLOCK_LENGTH; i++)
  296.         for (j = 0; j < NUMBER_TRACKS; j++)
  297.             fill_event(&(pattern->e[j][i]), f);
  298.     }
  299.  
  300.  
  301. void read_sample(info, f)
  302. struct sample_info *info;
  303. FILE *f;
  304.     {
  305.     int i;
  306.  
  307.     if (info->start)
  308.         {
  309.         fread(info->start, 1, info->length, f);
  310.         }
  311.     }
  312.  
  313.  
  314.  
  315.  
  316. /***
  317.  *
  318.  *  new_XXX: allocates a new structure for a song.
  319.  *  clears each and every field as appropriate.
  320.  *
  321.  ***/
  322.  
  323. struct song *new_song()
  324.     {
  325.     struct song *new;
  326.     int i;
  327.  
  328.     new = (struct song *)malloc(sizeof(struct song));
  329.     new->title = NULL;
  330.     new->info = NULL;
  331.     for (i = 0; i < NUMBER_SAMPLES; i++)
  332.         new->samples[i] = NULL;
  333.     return new;
  334.     }
  335.  
  336. struct sample_info *new_sample_info()
  337.     {
  338.     struct sample_info *new;
  339.  
  340.     new = (struct sample_info *)malloc(sizeof(struct sample_info));
  341.     new->name = NULL;
  342.     new->length = NULL;
  343.     new->start = NULL;
  344.     new->rp_start = NULL;
  345.     return new;
  346.     }
  347.  
  348. struct song_info *new_song_info()
  349.     {
  350.     struct song_info *new;
  351.  
  352.     new = (struct song_info *)malloc(sizeof(struct song_info));
  353.     new->length = 0;
  354.     new->maxpat = -1;
  355.     new->pblocks = NULL;
  356.     return new;
  357.     }
  358.  
  359. /* release_song(song): gives back all memory 
  360.  * occupied by song. Assume that each structure
  361.  * has been correctly allocated by a call to the
  362.  * corresponding new_XXX function.
  363.  */
  364. void release_song(song)
  365. struct song *song;
  366.     {
  367.     int i;
  368.  
  369.     for (i = 0; i < 31; i++)
  370.         {
  371.         if (song->samples[i])
  372.             {
  373.             if (song->samples[i]->start)
  374.                 free(song->samples[i]->start);
  375.             if (song->samples[i]->name)
  376.                 free(song->samples[i]->name);
  377.             free(song->samples[i]);
  378.             }
  379.         }
  380.     if (song->info)
  381.         {
  382.         if (song->info->pblocks)
  383.             free(song->info->pblocks);
  384.         free(song->info);
  385.         }
  386.     if (song->title)
  387.         free(song->title);
  388.     free(song);
  389.     }
  390.  
  391. /* error_song(song): what we should return
  392.  * if there was an error. Actually, is mostly
  393.  * useful for its side effects.
  394.  */
  395. struct song *error_song(song)
  396. struct song *song;
  397.     {
  398.     release_song(song);
  399.     fprintf(stderr, "Error while reading file\n");
  400.     return NULL;
  401.     }
  402.  
  403. /* bad_sig(f): read the signature on file f
  404.  * and returns !0 if it is not a known sig.
  405.  */
  406. int bad_sig(f)
  407. FILE *f;
  408.     {
  409.     char a, b, c, d;
  410.  
  411.     a = fgetc(f);
  412.     b = fgetc(f);
  413.     c = fgetc(f);
  414.     d = fgetc(f);
  415.     if (a == 'M' && b == '.' && c == 'K' && d == '.')
  416.         return 0;
  417.     if (a == 'M' && b == '&' && c == 'K' && d == '!')
  418.         return 0;
  419.     if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  420.         return 0;
  421.     return 1;
  422.     }
  423.  
  424. /* s = read_song(f, type): tries to read a song s
  425.  * of type NEW/OLD in file f. Might fail, i.e.,
  426.  * returns NULL if file is not a mod file of the
  427.  * correct type.
  428.  */
  429. struct song *read_song(f, type, tr)
  430. FILE *f;
  431. int type;
  432. int tr;
  433.     {
  434.     struct song *song;
  435.     int i;
  436.     int ninstr;
  437.  
  438.     error = NONE;
  439.     transpose = tr;
  440.     if (type == NEW || type == NEW_NO_CHECK)
  441.         ninstr = 31;
  442.     else
  443.         ninstr = 15;
  444.  
  445.     song = new_song();
  446.     song->title = getstring(f, 20);
  447.     if (error != NONE)
  448.         return error_song(song);
  449.  
  450.     for (i = 0; i <= 31; i++)
  451.         song->samples[i] = new_sample_info();
  452.  
  453.     for (i = 1; i <= ninstr; i++)
  454.         {
  455.         fill_sample_info(song->samples[i], f);
  456.         if (error != NONE)
  457.             return error_song(song);
  458.         }
  459.     song->info = new_song_info();
  460.  
  461.     fill_song_info(song->info, f);
  462.  
  463.     if (error != NONE)
  464.         return error_song(song);
  465.  
  466.     if (type == NEW  && bad_sig(f))
  467.         return error_song(song);
  468.  
  469.     if (type == NEW_NO_CHECK)
  470.         byteskip(f, 4);
  471.         
  472.  
  473.     song->info->pblocks = (struct block *)
  474.         malloc(sizeof(struct block) * song->info->maxpat);
  475.     for (i = 0; i < song->info->maxpat; i++)
  476.         {
  477.         fill_pattern(song->info->pblocks + i, f);
  478.         if (error != NONE)
  479.             return error_song(song);
  480.         }
  481.  
  482.     for (i = 1; i <= ninstr; i++)
  483.         read_sample(song->samples[i], f);
  484.     
  485.     if (error != NONE)
  486.         return error_song(song);
  487.     return song;
  488.     }
  489.  
  490. /***
  491.  *
  492.  *  dump_block/dump_song:
  493.  *  shows most of the readable info
  494.  *  concerning a module on the screen.
  495.  *
  496.  ***/
  497.  
  498. void dump_block(b)
  499. struct block *b;
  500.     {
  501.     int i, j;
  502.  
  503.     for (i = 0; i < BLOCK_LENGTH; i++)
  504.         {
  505.         for (j = 0; j < NUMBER_TRACKS; j++)
  506.             {
  507.             printf("%8d%5d%2d%4d", b->e[j][i].sample_number,
  508.                 b->e[j][i].pitch, b->e[j][i].effect,
  509.                 b->e[j][i].parameters);
  510.             }
  511.         printf("\n");
  512.         }
  513.     }
  514.  
  515. void dump_song(song)
  516. struct song *song;
  517.     {
  518.     int i;
  519.  
  520.     printf("%s\n", song->title);
  521.  
  522.     for (i = 1; i <= 31; i++)
  523.         {
  524.         if (song->samples[i]->start)
  525.             {
  526.             printf("\t%-30s", song->samples[i]->name);
  527.             printf("%5d", song->samples[i]->length);
  528.             if (song->samples[i]->rp_length > 2)
  529.                 printf("(%5d %5d)\n", song->samples[i]->rp_offset, 
  530.                     song->samples[i]->rp_length);
  531.             else
  532.                 printf("\n");
  533.             }
  534.         }
  535.     }
  536.