home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Server⁄Tracker 4.0 / st_read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  11.2 KB  |  483 lines  |  [TEXT/KAHL]

  1. /* st_read.c */
  2.  
  3. /* $Id: st_read.c,v 4.0 1994/01/11 17:56:51 espie Exp espie $
  4.  * $Log: st_read.c,v $
  5.  * Revision 4.0  1994/01/11  17:56:51  espie
  6.  * Use new open.
  7.  *
  8.  * Revision 1.5  1994/01/09  17:36:22  Espie
  9.  * Generalized open.c.
  10.  *
  11.  * Revision 1.4  1994/01/05  16:10:49  Espie
  12.  * *** empty log message ***
  13.  *
  14.  * Revision 1.3  1994/01/05  14:54:09  Espie
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 1.2  1994/01/05  13:50:43  Espie
  18.  * Cosmetic change.
  19.  *
  20.  * Revision 1.1  1993/12/26  00:55:53  Espie
  21.  * Initial revision
  22.  *
  23.  * Revision 3.13  1993/12/04  16:12:50  espie
  24.  * BOOL -> boolean.
  25.  *
  26.  * Revision 3.12  1993/12/02  15:45:33  espie
  27.  * Added lots of checks for malloc, plus count of bytes read.
  28.  *
  29.  * Revision 3.11  1993/11/27  17:29:53  espie
  30.  * Fixed up malloc problems.
  31.  *
  32.  * Revision 3.10  1993/11/17  15:31:16  espie
  33.  * *** empty log message ***
  34.  *
  35.  * Revision 3.9  1993/11/11  20:00:03  espie
  36.  * Amiga support.
  37.  *
  38.  * Revision 3.7  1993/07/17  12:00:30  espie
  39.  * Added other commands (numerous).
  40.  *
  41.  * Revision 3.2  1992/11/22  17:20:01  espie
  42.  * Checks for finetune ?
  43.  *
  44.  * Revision 3.1  1992/11/19  20:44:47  espie
  45.  * Protracker commands.
  46.  *
  47.  * Revision 3.0  1992/11/18  16:08:05  espie
  48.  * New release.
  49.  *
  50.  * Revision 2.16  1992/11/17  17:06:25  espie
  51.  * fix_xxx for better speed.
  52.  * Added some sample information in the dump.
  53.  * Added transpose feature.
  54.  * Feature fix: length 1 sample should be empty.
  55.  * Corrected repeat length problems concerning badly formed files,
  56.  * added signature checking for new tracker files.
  57.  * Corrected small problem with repeat being too short.
  58.  * Coded error types. More amiga specific stuff.
  59.  *
  60.  * Revision 1.17  1991/11/17  16:30:48  espie
  61.  * Rationnalized error recovery.
  62.  * There was a bug: you could try to deallocate
  63.  * stuff in no-noland. Also, strings never got
  64.  * to be freed.
  65.  * Centralized error control to error_song.
  66.  * Added a new test on length, aborts most modules now.
  67.  * Maybe should say it as well.
  68.  * Added checkpoints for early return if file too short.
  69.  * Added memory recovery and error control.
  70.  * Suppressed ! warning for bad note.
  71.  * Added note support.
  72.  * Corrected length and rep_length/rep_offset
  73.  * which are given in words and should be converted to
  74.  * bytes.
  75.  */
  76.  
  77. #include "defs.h"
  78.  
  79. #ifdef MALLOC_NOT_IN_STDLIB
  80. #include <malloc.h>
  81. #else
  82. #include <stdlib.h>
  83. #endif
  84. #include <stdio.h>
  85. #include <string.h>
  86. #include <ctype.h>
  87. #include <assert.h>
  88.  
  89. #include "extern.h"
  90. #include "song.h"
  91. #include "channel.h"
  92.  
  93.  
  94. ID("$Id: st_read.c,v 4.0 1994/01/11 17:56:51 espie Exp espie $")
  95.  
  96. /***
  97.  *
  98.  * Low level st-file access routines 
  99.  *
  100.  ***/
  101.  
  102. #define MAX_LEN 50
  103. /* s = getstring(f, len):
  104.  * gets a soundtracker string from file f.
  105.  * I.e, it is a fixed length string terminated
  106.  * by a 0 if too short. Length should be
  107.  * smaller than MAX_LEN.
  108.  */
  109. LOCAL char *getstring(f, len)
  110. struct exfile *f;
  111. int len;
  112.    {
  113.    static char s[MAX_LEN];
  114.    char *new;
  115.    int i;
  116.         
  117.    assert(len < MAX_LEN);
  118.    for (i = 0; i < len; i++)
  119.       s[MIN(i, MAX_LEN - 1)] = getc_file(f);
  120.    s[MIN(len, MAX_LEN - 1)] = '\0';
  121.    new = malloc(strlen(s)+1);
  122.    if (!new) 
  123.       return NULL;
  124.  
  125.    return strcpy(new, s);
  126.    }
  127.  
  128. /* byteskip(f, len)
  129.  * same as fseek, xcpt it works on stdin
  130.  */
  131. LOCAL void byteskip(f, len)
  132. struct exfile *f;
  133. int len;
  134.    {
  135.    int i;
  136.  
  137.    for (i = 0; i < len; i++)
  138.       getc_file(f);
  139.    }
  140.  
  141. /* v = getushort(f)
  142.  * reads an unsigned short from f
  143.  */
  144. LOCAL int getushort(f)
  145. struct exfile *f;
  146.    {
  147.    int i;
  148.  
  149.       /* order dependent !!! */
  150.    i = getc_file(f) << 8;
  151.    return i | getc_file(f);
  152.    }
  153.  
  154.  
  155. /* fill_sample_info(info, f):
  156.  * fill sample info with the information at current position of
  157.  * file f. Allocate memory for storing the sample, also.
  158.  * fill_sample_info is guaranteed to give you an accurate snapshot
  159.  * of what sample should be like. In particular, length, rp_length,
  160.  * start, rp_start, fix_length, fix_rp_length will have the values
  161.  * you can expect if part of the sample is missing.
  162.  */
  163. LOCAL void fill_sample_info(info, f)
  164. struct sample_info *info;
  165. struct exfile *f;
  166.    {
  167.    info->name = getstring(f, SAMPLENAME_MAXLENGTH);
  168.    if (!info->name)
  169.       {
  170.       error = OUT_OF_MEM;
  171.       return;
  172.       }
  173.    info->length = getushort(f);
  174.    info->finetune = getc_file(f);
  175.    if (info->finetune > 15)
  176.       info->finetune = 0;
  177.    info->volume = getc_file(f);
  178.    info->volume = MIN(info->volume, MAX_VOLUME);
  179.    info->rp_offset = getushort(f);
  180.    info->rp_length = getushort(f);
  181.  
  182.    /* the next check is for old modules for which
  183.     * the sample data types are a bit confused, so
  184.     * that what we were expecting to be #words is #bytes.
  185.     */
  186.       /* not sure I understand the -1 myself, though it's
  187.        * necessary to play kawai-k1 correctly 
  188.        */
  189.    if (info->rp_length + info->rp_offset - 1 > info->length)
  190.       info->rp_offset /= 2;
  191.     
  192.    if (info->rp_length + info->rp_offset > info->length)
  193.       info->rp_length = info->length - info->rp_offset;
  194.  
  195.    info->length *= 2;
  196.    info->rp_offset *= 2;
  197.    info->rp_length *= 2;
  198.       /* in all logic, a 2-sized sample could exist,
  199.        * but this is not the case, and even so, some
  200.        * trackers output empty instruments as being 2-sized.
  201.        */
  202.    if (info->length <= 2)
  203.       {
  204.       info->start = NULL;
  205.       info->length = 0;
  206.       }
  207.    else
  208.       {
  209.       info->start = (SAMPLE *)alloc_sample(info->length);
  210.       if (!info->start)
  211.          {
  212.          error = OUT_OF_MEM;
  213.          return;
  214.          }
  215.  
  216.       if (info->rp_length > 2)
  217.          info->rp_start = info->start + info->rp_offset;
  218.       else
  219.          {
  220.          info->rp_start = NULL;
  221.          info->rp_length = 0;
  222.          }
  223.       }
  224.  
  225.    if (info->length > MAX_SAMPLE_LENGTH)
  226.       error = CORRUPT_FILE;
  227.    info->fix_length = int_to_fix(info->length);
  228.    info->fix_rp_length = int_to_fix(info->rp_length);
  229.    }
  230.  
  231. LOCAL void fill_song_info(info, f)
  232. struct song_info *info;
  233. struct exfile *f;
  234.    {
  235.    int i;
  236.    int p;
  237.  
  238.    info->length = getc_file(f);
  239.    getc_file(f);
  240.    info->maxpat = -1;
  241.    for (i = 0; i < NUMBER_PATTERNS; i++)
  242.       {
  243.       p = getc_file(f);
  244.       if (p >= NUMBER_PATTERNS)
  245.          p = 0;
  246.       if (p > info->maxpat)
  247.          info->maxpat = p;
  248.       info->patnumber[i] = p;
  249.       }
  250.    info->maxpat++;
  251.    if (info->maxpat == 0 || info->length == 0)
  252.       error = CORRUPT_FILE;
  253.    }
  254.  
  255. LOCAL void fill_event(e, f)
  256. struct event *e;
  257. struct exfile *f;
  258.    {
  259.    int a, b, c, d;
  260.  
  261.    a = getc_file(f);
  262.    b = getc_file(f);
  263.    c = getc_file(f);
  264.    d = getc_file(f);
  265.    e->sample_number = (a & 0x10) | (c >> 4);
  266.    e->effect = c & 0xf;
  267.    e->parameters = d;
  268.    if (e->effect == EFF_EXTENDED)
  269.       {
  270.       e->effect = EXT_BASE + HI(e->parameters);
  271.       e->parameters = LOW(e->parameters);
  272.       }
  273.    if (e->effect == 0)
  274.       e->effect = e->parameters ? EFF_ARPEGGIO : EFF_NONE;
  275.    if (e->effect == EFF_SKIP)
  276.       e->parameters = HI(e->parameters) * 10 + LOW(e->parameters);
  277.    e->pitch = ( (a & 15) << 8 ) | b;
  278.    e->note = find_note(e->pitch);
  279.    }
  280.  
  281. LOCAL void fill_pattern(pattern, f)
  282. struct block *pattern;
  283. struct exfile *f;
  284.    {
  285.    int i, j;
  286.  
  287.    for (i = 0; i < BLOCK_LENGTH; i++)
  288.       for (j = 0; j < NUMBER_TRACKS; j++)
  289.          fill_event(&(pattern->e[j][i]), f);
  290.    }
  291.  
  292.  
  293. LOCAL void read_sample(struct sample_info *info, struct exfile *f)
  294.   {
  295.     if (info->start)
  296.       {
  297.         obtain_sample(info->start, info->length, file_handle(f));
  298.         if (info->rp_start)
  299.           {
  300.             /* if sample loops, then append first loop sample to end */
  301.             info->start[info->length] = info->start[info->rp_offset];
  302.           }
  303.          else
  304.           {
  305.             /* otherwise, append last sample */
  306.             info->start[info->length] = info->start[info->length - 1];
  307.           }
  308.       }
  309.   }
  310.  
  311.  
  312.  
  313.  
  314. /***
  315.  *
  316.  *  new_song: allocates a new structure for a song.
  317.  *  clears each and every field as appropriate.
  318.  *
  319.  ***/
  320. LOCAL struct song *new_song()
  321.    {
  322.    struct song *new;
  323.    int i;
  324.  
  325.    new = (struct song *)malloc(sizeof(struct song));
  326.    if (!new) 
  327.       {
  328.       error = OUT_OF_MEM;
  329.       return NULL;
  330.       }
  331.    new->title = NULL;
  332.    new->info.length = 0;
  333.    new->info.maxpat = -1;
  334.    new->info.transpose = 0;
  335.    new->info.pblocks = NULL;
  336.    for (i = 0; i < NUMBER_SAMPLES; i++)
  337.       {
  338.       new->samples[i].finetune = 0;
  339.       new->samples[i].name = NULL;
  340.       new->samples[i].length = 0;
  341.       new->samples[i].start = NULL;
  342.       new->samples[i].rp_start = NULL;
  343.       new->samples[i].fix_length = 0;
  344.       new->samples[i].fix_rp_length = 0;
  345.       }
  346.    return new;
  347.    }
  348.  
  349. /* release_song(song): gives back all memory 
  350.  * occupied by song. Assume that each structure
  351.  * has been correctly allocated by a call to the
  352.  * corresponding new_XXX function.
  353.  */
  354. void release_song(song)
  355. struct song *song;
  356.    {
  357.    int i;
  358.  
  359.    if (!song)
  360.       return;
  361.    for (i = 0; i < NUMBER_SAMPLES; i++)
  362.       {
  363.       if (song->samples[i].start)
  364.          free_sample(song->samples[i].start);
  365.       if (song->samples[i].name)
  366.          free(song->samples[i].name);
  367.       }
  368.    if (song->info.pblocks)
  369.       free(song->info.pblocks);
  370.    if (song->title)
  371.       free(song->title);
  372.    free(song);
  373.    }
  374.  
  375. /* error_song(song): what we should return
  376.  * if there was an error. Actually, is mostly
  377.  * useful for its side effects.
  378.  */
  379. LOCAL struct song *error_song(song)
  380. struct song *song;
  381.    {
  382.    release_song(song);
  383.    return NULL;
  384.    }
  385.  
  386. /* bad_sig(f): read the signature on file f
  387.  * and returns TRUE if it is not a known sig.
  388.  */
  389. LOCAL boolean bad_sig(f)
  390. struct exfile *f;
  391.    {
  392.    char a, b, c, d;
  393.  
  394.    a = getc_file(f);
  395.    b = getc_file(f);
  396.    c = getc_file(f);
  397.    d = getc_file(f);
  398.    if (a == 'M' && b == '.' && c == 'K' && d == '.')
  399.       return FALSE;
  400.    if (a == 'M' && b == '&' && c == 'K' && d == '!')
  401.       return FALSE;
  402.    if (a == 'F' && b == 'L' && c == 'T' && d == '4')
  403.       return FALSE;
  404.    return TRUE;
  405.    }
  406.  
  407. /* s = read_song(f, type): tries to read a song s
  408.  * of type NEW/OLD in file f. Might fail, i.e.,
  409.  * returns NULL if file is not a mod file of the
  410.  * correct type.
  411.  */
  412. struct song *read_song(f, type)
  413. struct exfile *f;
  414. int type;
  415.    {
  416.    struct song *song;
  417.    int i;
  418.    int ninstr;
  419.  
  420.    error = NONE;
  421.  
  422.    if (type == NEW || type == NEW_NO_CHECK)
  423.       ninstr = 31;
  424.    else
  425.       ninstr = 15;
  426.  
  427.    song = new_song();
  428.    if (!song)
  429.       return error_song(song);
  430.    song->title = getstring(f, TITLE_MAXLENGTH);
  431.    if (error != NONE)
  432.       return error_song(song);
  433.  
  434.    for (i = 1; i <= ninstr; i++)
  435.       {
  436.       fill_sample_info(&song->samples[i], f);
  437.       if (error != NONE)
  438.          return error_song(song);
  439.       }
  440.  
  441.    fill_song_info(&song->info, f);
  442.  
  443.    if (error != NONE)
  444.       return error_song(song);
  445.  
  446.    if (type == NEW && bad_sig(f))
  447.       return error_song(song);
  448.  
  449.    if (type == NEW_NO_CHECK)
  450.       byteskip(f, 4);
  451.         
  452.  
  453.    song->info.pblocks = (struct block *)
  454.       malloc(sizeof(struct block) * song->info.maxpat);
  455.    if (!song->info.pblocks)
  456.       {
  457.       error = OUT_OF_MEM;
  458.       return error_song(song);
  459.       }
  460.    for (i = 0; i < song->info.maxpat; i++)
  461.       {
  462.       fill_pattern(song->info.pblocks + i, f);
  463.       if (error != NONE)
  464.          return error_song(song);
  465.       }
  466.          /* future code... */
  467.    song->samples_start = tell_file(f);
  468.  
  469. #if 0
  470.    if (feof(f))
  471.       for (i = 1; i <= ninstr; i++)
  472.          find_sample(&song->samples[i]);
  473.    else
  474. #endif
  475.       for (i = 1; i <= ninstr; i++)
  476.          read_sample(&song->samples[i], f);
  477.     
  478.    if (error != NONE)
  479.       return error_song(song);
  480.    return song;
  481.    }
  482.  
  483.