home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / windows3 / mtlabsnd.zip / SF.C < prev    next >
Text File  |  1993-04-26  |  3KB  |  124 lines

  1. /*
  2.  * July 5, 1991
  3.  * Copyright 1991 Lance Norskog And Sundry Contributors
  4.  * This source code is freely redistributable and may be used for
  5.  * any purpose.  This copyright notice must be maintained. 
  6.  * Lance Norskog And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * Sound Tools IRCAM SoundFile format handler.
  12.  * 
  13.  * Derived from: Sound Tools skeleton handler file.
  14.  */
  15.  
  16. #include "st.h"
  17. #include "sfheader.h"
  18.  
  19. /* Private data for SF file */
  20. typedef struct sfstuff {
  21.     struct sfinfo info;
  22. } *sf_t;
  23.  
  24. extern int summary, verbose;
  25.  
  26. /*
  27.  * Do anything required before you start reading samples.
  28.  * Read file header. 
  29.  *    Find out sampling rate, 
  30.  *    size and style of samples, 
  31.  *    mono/stereo/quad.
  32.  */
  33. sfstartread(ft) 
  34. ft_t ft;
  35. {
  36.     sf_t sf = (sf_t) ft->priv;
  37.     int i;
  38.     
  39.     if (fread(&sf->info, 1, sizeof(sf->info), ft->fp) != sizeof(sf->info))
  40.         fail("unexpected EOF in SF header");
  41.     if (ft->swap) sf->info.sf_magic = swapl(sf->info.sf_magic);
  42.     if (ft->swap) sf->info.sf_srate = swapl(sf->info.sf_srate);
  43.     if (ft->swap) sf->info.sf_packmode = swapl(sf->info.sf_packmode);
  44.     if (ft->swap) sf->info.sf_chans = swapl(sf->info.sf_chans);
  45.     if (ft->swap) sf->info.sf_codes = swapl(sf->info.sf_codes);
  46.     if (sf->info.sf_magic != SF_MAGIC)
  47.         if (sf->info.sf_magic == swapl(SF_MAGIC))
  48.             fail("SF %s file: can't read, it is probably byte-swapped");
  49.         else
  50.             fail("SF %s file: can't read, it is not an IRCAM SoundFile");
  51.  
  52.     /*
  53.      * If your format specifies or your file header contains
  54.      * any of the following information. 
  55.      */
  56.     ft->info.rate = sf->info.sf_srate;
  57.     switch(sf->info.sf_packmode) {
  58.         case SF_SHORT:
  59.             ft->info.size = WORD;
  60.             ft->info.style = SIGN2;
  61.             break;
  62.         case SF_FLOAT:
  63.             ft->info.size = FLOAT;
  64.             ft->info.style = SIGN2;
  65.             break;
  66.         default:
  67.             fail("Soundfile input: unknown format 0x%x\n",
  68.                 sf->info.sf_packmode);
  69.     }
  70.     ft->info.channels = sf->info.sf_chans;
  71.     /* Future: Read codes and print as comments. */
  72.  
  73.     /* Skip all the comments */
  74.     for(i = sizeof(struct sfinfo); i < SIZEOF_BSD_HEADER; i++)
  75.         getc(ft->fp);
  76.  
  77. }
  78.  
  79. sfstartwrite(ft) 
  80. ft_t ft;
  81. {
  82.     sf_t sf = (sf_t) ft->priv;
  83.     int i;
  84.  
  85.     sf->info.sf_magic = SF_MAGIC;
  86.     sf->info.sf_srate = ft->info.rate;
  87. #ifdef    LATER
  88.     /* 
  89.      * CSound sound-files have many formats. 
  90.      * We stick with the IRCAM short-or-float scheme.
  91.      */
  92.     if (ft->info.size == WORD) {
  93.         sf->info.sf_packmode = SF_SHORT;
  94.         ft->info.style = SIGN2;        /* Default to signed words */
  95.     } else if (ft->info.size == FLOAT)
  96.         sf->info.sf_packmode = SF_FLOAT;
  97.     else
  98.         fail("SoundFile %s: must set output as signed shorts or floats",
  99.             ft->filename);
  100. #else
  101.     if (ft->info.size == FLOAT) {
  102.         sf->info.sf_packmode = SF_FLOAT;
  103.         ft->info.size = FLOAT;
  104.     } else {
  105.         sf->info.sf_packmode = SF_SHORT;
  106.         ft->info.size = WORD;
  107.         ft->info.style = SIGN2;        /* Default to signed words */
  108.     }
  109. #endif
  110.     sf->info.sf_chans = ft->info.channels;
  111.     sf->info.sf_codes = SF_END;        /* No comments */
  112.  
  113.     (void) fwrite(&sf->info, 1, sizeof(sf->info), ft->fp);
  114.     /* Skip all the comments */
  115.     for(i = sizeof(struct sfinfo); i < SIZEOF_BSD_HEADER; i++)
  116.         putc(0, ft->fp);
  117.  
  118. }
  119.  
  120. /* Read and write are supplied by raw.c */
  121.  
  122.  
  123.  
  124.