home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / sfircam.h < prev    next >
C/C++ Source or Header  |  1999-07-18  |  7KB  |  219 lines

  1. /*                SFHEADER.H                */
  2.  
  3. /* definitions and structures needed for manipulating soundfiles.
  4.  */
  5.  
  6. #define SIZEOF_HEADER 1024
  7. #define SF_BUFSIZE    (16*1024) /* used only in play */
  8. #define SF_MAXCHAN    4
  9. #define MAXCOMM 512
  10. #define MINCOMM 256
  11.  
  12. #define SF_MAGIC1 0144
  13. #define SF_MAGIC2 0243
  14.  
  15. /* Definition of SF_MACHINE and SF_MAGIC
  16.  *
  17.  * Note that SF_MAGIC always has SF_MAGIC1 as its first byte, SF_MAGIC2 as its
  18.  * second, SF_MACHINE as its third, and zero as its fourth.  Separate define's
  19.  * are needed because byte order is different on different machines.
  20.  */
  21. #define SF_VAX 1
  22. #define SF_SUN 2
  23. #define SF_MIPS 3
  24. #define SF_NEXT 4
  25. #ifdef vax
  26. #define SF_MACHINE SF_VAX
  27. #define SF_MAGIC ((LONG)(SF_MAGIC1 | SF_MAGIC2 << 8 | SF_MACHINE << 16))
  28. #endif
  29. #ifdef sun
  30. #define SF_MACHINE SF_SUN
  31. #define SF_MAGIC ((LONG)(SF_MAGIC1 << 24 | SF_MAGIC2 << 16 | SF_MACHINE << 8))
  32. #endif
  33. #ifdef mips
  34. #define SF_MACHINE SF_MIPS
  35. #define SF_MAGIC ((LONG)(SF_MAGIC1 | SF_MAGIC2 << 8 | SF_MACHINE << 16))
  36. #endif
  37. #ifdef NeXT
  38. #define SF_MACHINE SF_NEXT
  39. #define SF_MAGIC ((LONG)(SF_MAGIC1 << 24 | SF_MAGIC2 << 16 | SF_MACHINE << 8))
  40. #endif
  41.  
  42.  
  43. /* Packing modes, as stored in the SFHEADER.sf_packmode field
  44.  *
  45.  * For each packing mode, the lower-order short is the number of bytes per
  46.  * sample, and for backward compatibility, SF_SHORT and SF_FLOAT have
  47.  * high-order short = 0 so overall they're the bytes per sample, but that's not
  48.  * true for all SF_'s.  Thus while the "sfclass" macro still returns a unique
  49.  * ID for each packing mode, the new "sfsamplesize" macro should be used to get
  50.  * the bytes per sample.
  51.  *
  52.  * Note that SF_X == SFMT_X in most, but not all, cases, because MIT changed
  53.  * SFMT_FLOAT and we kept SF_FLOAT for compatibility with existing sound files.
  54.  *
  55.  * Possible values of sf_packmode:
  56.  */
  57. #define SF_CHAR  ((LONG) sizeof(char))
  58. #define SF_ALAW  ((LONG) sizeof(char) | 0x10000)
  59. #define SF_ULAW  ((LONG) sizeof(char) | 0x20000)
  60. #define SF_SHORT ((LONG) sizeof(short))
  61. #define SF_LONG  ((LONG) sizeof(LONG) | 0x40000)
  62. #define SF_FLOAT ((LONG) sizeof(float))
  63.  
  64. /* For marking data after fixed section of soundfile header -- see man (3carl)
  65.  * sfcodes and defintions of SFCODE and related structures, below.
  66.  */
  67. #define SF_END 0            /* Meaning no more information */
  68. #define SF_MAXAMP 1         /* Meaning maxamp follows */
  69. #define SF_COMMENT 2        /* code for "comment line" */
  70. #define SF_PVDATA      3
  71. #define SF_AUDIOENCOD  4
  72. #define SF_CODMAX      4
  73.  
  74. /*
  75.  * DEFINITION OF SFHEADER FORMAT
  76.  *
  77.  * The first four bytes are the magic information for the sound file.  They
  78.  * can be accessed, via a union, either as a structure of four unsigned bytes
  79.  * sf_magic1, sf_magic2, sf_machine, sf_param, or as the single long sf_magic.
  80.  * sf_magic is for backward compatibility; it should be SF_MAGIC as defined
  81.  * above.
  82.  */
  83. typedef union sfheader {
  84.     struct sfinfo {
  85.         union magic_union {
  86.             struct {
  87.                 unsigned char sf_magic1;  /* byte 1 of magic */
  88.                 unsigned char sf_magic2;  /* 2 */
  89.                 unsigned char sf_machine; /* 3 */
  90.                 unsigned char sf_param;      /* 4 */
  91.                 } _magic_bytes;
  92.             LONG sf_magic;              /* magic as a 4-byte long */
  93.             } magic_union;
  94.         float      sf_srate;
  95.         LONG      sf_chans;
  96.         LONG      sf_packmode;
  97.         char      sf_codes;
  98.     } sfinfo;
  99.     char    filler[SIZEOF_HEADER];
  100. } SFHEADER;
  101.  
  102. /*
  103.  * Definition of SFCODE and related data structs
  104.  *
  105.  * Two routines in libbicsf/sfcodes.c, getsfcode() and putsfcode()
  106.  * are used to insert additionnal information into a header
  107.  * or to retreive such information. See man sfcodes.
  108.  *
  109.  * 10/90 pw
  110.  *    These routines are now part of libcarl/sfcodes.c
  111.  */
  112.  
  113. typedef struct sfcode {
  114.     short    code;
  115.     short    bsize;
  116. } SFCODE;
  117.  
  118. typedef struct Sfmaxamp {
  119.     float    value[SF_MAXCHAN];
  120.     LONG    samploc[SF_MAXCHAN];
  121.     LONG    timetag;
  122. } SFMAXAMP;
  123.  
  124. typedef struct sfcomment {
  125.         char    comment[MAXCOMM];
  126. } SFCOMMENT;
  127.  
  128. typedef struct {                  /* this code written by pvanal */
  129.         short   frameSize;
  130.     short   frameIncr;
  131. } SFPVDATA;
  132.  
  133. typedef struct {                  /*     ditto                    */
  134.         short   encoding;
  135.     short   grouping;
  136. } SFAUDIOENCOD;
  137.  
  138. /*
  139.  * DEFINITION OF MACROS TO GET HEADER INFO
  140.  *     x is a pointer to SFHEADER
  141.  *
  142.  * For backward compatibility in MIT Csound code, sfmagic(x) still provides
  143.  * access to the first long of SFHEADER x.  It can be compared to SF_MAGIC,
  144.  * which is defined machine-dependently (above) to always be the right four
  145.  * bytes in the right order.
  146.  *
  147.  * sfclass(x) returns one of SF_SHORT, SF_FLOAT etc. defined above, while
  148.  * sfsamplesize(x) returns just the bytes per object, the lower-order short of
  149.  * sf_packmode.
  150.  */
  151. #define sfmagic(x) ((x)->sfinfo.magic_union.sf_magic)
  152. #define sfmagic1(x) ((x)->sfinfo.magic_union._magic_bytes.sf_magic1)
  153. #define sfmagic2(x) ((x)->sfinfo.magic_union._magic_bytes.sf_magic2)
  154. #define sfmachine(x) ((x)->sfinfo.magic_union._magic_bytes.sf_machine)
  155. #define sfparam(x) ((x)->sfinfo.magic_union._magic_bytes.sf_param)
  156. #define sfsrate(x) ((x)->sfinfo.sf_srate)
  157. #define sfchans(x) ((x)->sfinfo.sf_chans)
  158. #define sfclass(x) ((x)->sfinfo.sf_packmode)
  159. #define sfsamplesize(x) ((size_t) ((x)->sfinfo.sf_packmode & 0xFFFF))
  160. #define sfbsize(x) ((x)->st_size - sizeof(SFHEADER))
  161. #define sfcodes(x) ((x)->sfinfo.sf_codes)
  162.  
  163. /*
  164.  * Macros for testing soundfiles
  165.  */
  166. /* True if soundfile and good arch */
  167. #define ismagic(x) ((sfmagic1(x) == SF_MAGIC1) && \
  168.     (sfmagic2(x) == SF_MAGIC2) && \
  169.     (sfmachine(x) == SF_MACHINE))
  170.  
  171. /* True if soundfile */
  172. #define isforeignmagic(x) ((sfmagic1(x) == SF_MAGIC1) && \
  173.     (sfmagic2(x) == SF_MAGIC2))
  174.  
  175. /* True if soundfile */
  176. #define issoundfile(x)  ((sfmagic1(x) == SF_MAGIC1) && \
  177.     (sfmagic2(x) == SF_MAGIC2))
  178.  
  179. /* True if soundfile and foreign arch */
  180. #define isforeignsoundfile(x) ((sfmagic1(x) == SF_MAGIC1) && \
  181.     (sfmagic2(x) == SF_MAGIC2) && \
  182.     (sfmachine(x) != SF_MACHINE))
  183.  
  184. /* True if foreign arch */
  185. #define isforeign(x) (sfmachine(x) != SF_MACHINE)
  186.  
  187.  
  188. /*
  189.  * The macros for opening soundfiles have been rewritten as C routines.
  190.  * In order to preserve compatibility, we supply the following new macros
  191.  */
  192.  
  193. #define readopensf(name,fd,sfh,sfst,prog,result) \
  194.     result = (fd = openrosf(name, &sfh, &sfst, prog)) < 0 ? fd : 0;
  195.  
  196. #define freadopensf(name,fp,sfh,sfst,prog,result) \
  197.     result = fopenrosf(name, &fp, &sfh, &sfst, prog);
  198.  
  199. #define wropensf(name,fd,sfh,prog,result) \
  200.     result = (fd = openwosf(name, &sfh, prog)) < 0 ? fd : 0;
  201.  
  202. #define rdwropensf(name,fd,sfh,sfst,prog,result) \
  203.     result = (fd = openrwsf(name, &sfh, &sfst, prog)) < 0 ? fd : 0;
  204.  
  205.  
  206. /*
  207.  * Definition of macro to get MAXAMP and COMMENT info
  208.  *
  209.  * sfm is ptr to SFMAXAMP
  210.  * sfst is the address of a stat struct
  211.  */
  212.  
  213. #define sfmaxamp(mptr,chan) (mptr)->value[chan]
  214. #define sfmaxamploc(mptr,chan) (mptr)->samploc[chan]
  215. #define sfmaxamptime(x) (x)->timetag
  216. #define ismaxampgood(x,s) (sfmaxamptime(x) >= (s)->st_mtime)
  217. #define sfcomm(x,n) (x)->comment[n]
  218.  
  219.