home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3593 / sf.c < prev    next >
C/C++ Source or Header  |  1991-07-07  |  3KB  |  108 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.  * AUX IRCAM SoundFile format handler.
  12.  * 
  13.  * Derived from: AUX skeleton handler file.
  14.  */
  15.  
  16. #include "aux.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.     fread(&sf->info, 1, sizeof(sf->info), ft->fp);
  40.     if (ft->swap) sf->info.sf_magic = swapl(sf->info.sf_magic);
  41.     if (ft->swap) sf->info.sf_srate = swapl(sf->info.sf_srate);
  42.     if (ft->swap) sf->info.sf_packmode = swapl(sf->info.sf_packmode);
  43.     if (ft->swap) sf->info.sf_chans = swapl(sf->info.sf_chans);
  44.     if (ft->swap) sf->info.sf_codes = swapl(sf->info.sf_codes);
  45.     if (sf->info.sf_magic != SF_MAGIC)
  46.         if (sf->info.sf_magic == swapl(SF_MAGIC))
  47.             fail("SF %s file: can't read, it is probably byte-swapped");
  48.         else
  49.             fail("SF %s file: can't read, it is not an IRCAM SoundFile");
  50.  
  51.     /*
  52.      * If your format specifies or your file header contains
  53.      * any of the following information. 
  54.      */
  55.     ft->rate = sf->info.sf_srate;
  56.     switch(sf->info.sf_packmode) {
  57.         case SF_SHORT:
  58.             ft->size = WORD;
  59.             ft->style = SIGN2;
  60.             break;
  61.         case SF_FLOAT:
  62.             ft->size = FLOAT;
  63.             ft->style = SIGN2;
  64.             break;
  65.     }
  66.     ft->channels = sf->info.sf_chans;
  67.     /* Future: Read codes and print as comments. */
  68.  
  69.     /* Skip all the comments */
  70.     for(i = sizeof(struct sfinfo); i < SIZEOF_BSD_HEADER; i++)
  71.         getc(ft->fp);
  72.  
  73.     /* It's raw from here. */
  74.     ft->h = &handlers[RAWTYPE];
  75. }
  76.  
  77. sfstartwrite(ft) 
  78. ft_t ft;
  79. {
  80.     sf_t sf = (sf_t) ft->priv;
  81.     int i;
  82.  
  83.     sf->info.sf_magic = SF_MAGIC;
  84.     sf->info.sf_srate = ft->rate;
  85.     if (ft->size == WORD) {
  86.         sf->info.sf_packmode = SF_SHORT;
  87.         ft->style = SIGN2;        /* Default to signed words */
  88.     } else if (ft->size == FLOAT)
  89.         sf->info.sf_packmode = SF_FLOAT;
  90.     else
  91.         fail("SoundFile %s: must set output as signed shorts or floats",
  92.             ft->which);
  93.     sf->info.sf_chans = ft->channels;
  94.     sf->info.sf_codes = SF_END;        /* No comments */
  95.  
  96.     fwrite(&sf->info, 1, sizeof(sf->info), ft->fp);
  97.     /* Skip all the comments */
  98.     for(i = sizeof(struct sfinfo); i < SIZEOF_BSD_HEADER; i++)
  99.         putc(0, ft->fp);
  100.  
  101.     /* It's raw from here. */
  102.     ft->h = &handlers[RAWTYPE];
  103. }
  104.  
  105.  
  106.  
  107.  
  108.