home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / MM1 / SOUNDUTILS / mm1_tracker.lzh / TRACKER4.6 / st_read.c < prev    next >
Text File  |  1994-11-24  |  10KB  |  442 lines

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