home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / wve.c < prev   
C/C++ Source or Header  |  1999-07-18  |  4KB  |  195 lines

  1. /*
  2.  * Psion wve format, based on the au format file. Hacked by
  3.  * Richard Caley (R.Caley@ed.ac.uk)
  4.  */
  5.  
  6. #include "st.h"
  7. #include "g72x.h"
  8.  
  9. /* Magic numbers used in Psion audio files */
  10. #define PSION_MAGIC     "ALawSoundFile**"
  11. #define PSION_VERSION   ((short)3856)
  12. #define PSION_INV_VERSION   ((short)4111)
  13. #define PSION_HDRSIZE    32
  14.  
  15. struct wvepriv
  16.     {
  17.     unsigned int length;
  18.     short padding;
  19.     short repeats;
  20.     };
  21.  
  22. void wvewriteheader(P1(ft_t ft));
  23. LONG rawread(P3(ft_t, LONG *, LONG));
  24. void rawwrite(P3(ft_t, LONG *, LONG));
  25.  
  26. void wvestartread(ft) 
  27. ft_t ft;
  28. {
  29.     struct wvepriv *p = (struct wvepriv *) ft->priv;
  30.     char magic[16];
  31.     short version;
  32.  
  33.     int littlendian = 1;
  34.     char *endptr;
  35.  
  36.     /* Needed for rawread() */
  37.     rawstartread(ft);
  38.  
  39.     endptr = (char *) &littlendian;
  40.     /* WVE is in big endian format.  Swap whats read in
  41.      * on little endian machines.
  42.      */
  43.     if (*endptr)
  44.     {
  45.         ft->swap = ft->swap ? 0 : 1;
  46.     }
  47.  
  48.     /* Sanity check */ 
  49.     if (sizeof(struct wvepriv) > PRIVSIZE)
  50.         fail(
  51. "struct wvepriv is too big (%d); change PRIVSIZE in st.h and recompile sox",
  52.              sizeof(struct wvepriv));
  53.  
  54.     /* Check the magic word */
  55.         fread(magic, 16, 1, ft->fp);
  56.     if (strcmp(magic, PSION_MAGIC)==0) {
  57.         report("Found Psion magic word");
  58.     }
  59.     else
  60.         fail("Psion header doesn't start with magic word\nTry the '.al' file type with '-t al -r 8000 filename'");
  61.  
  62.         version=rshort(ft);
  63.  
  64.     /* Check for what type endian machine its read on */
  65.     if (version == PSION_INV_VERSION)
  66.     {
  67.         /* This is probably left over from a time before
  68.          * testing for endianess was standardized.  Leaving since
  69.          * it doesn't hurt.
  70.          */
  71.         ft->swap = ft->swap ? 0 : 1;
  72.         report("Found inverted PSION magic word.  Swapping bytes.");
  73.     }
  74.     else if (version == PSION_VERSION)
  75.     {
  76.         report("Found PSION magic word");
  77.     }
  78.     else
  79.         fail("Wrong version in Psion header");
  80.  
  81.          p->length=rlong(ft);
  82.  
  83.     p->padding=rshort(ft);
  84.  
  85.     p->repeats=rshort(ft);
  86.  
  87.      (void)rshort(ft);
  88.      (void)rshort(ft);
  89.      (void)rshort(ft);
  90.     
  91.     ft->info.style = ALAW;
  92.     ft->info.size = BYTE;
  93.  
  94.     ft->info.rate = 8000;
  95.  
  96.     ft->info.channels = 1;
  97. }
  98.  
  99. /* When writing, the header is supposed to contain the number of
  100.    data bytes written, unless it is written to a pipe.
  101.    Since we don't know how many bytes will follow until we're done,
  102.    we first write the header with an unspecified number of bytes,
  103.    and at the end we rewind the file and write the header again
  104.    with the right size.  This only works if the file is seekable;
  105.    if it is not, the unspecified size remains in the header
  106.    (this is illegal). */
  107.  
  108. void wvestartwrite(ft) 
  109. ft_t ft;
  110. {
  111.     struct wvepriv *p = (struct wvepriv *) ft->priv;
  112.  
  113.     int littlendian = 1;
  114.     char *endptr;
  115.  
  116.     /* Needed for rawwrite() */
  117.     rawstartwrite(ft);
  118.  
  119.     endptr = (char *) &littlendian;
  120.     /* wve is in big endian format.  Swap whats read in
  121.      * on little endian machines.
  122.      */
  123.     if (*endptr)
  124.     {
  125.         ft->swap = ft->swap ? 0 : 1;
  126.     }
  127.  
  128.     p->length = 0;
  129.     if (p->repeats == 0)
  130.         p->repeats = 1;
  131.  
  132.     ft->info.style = ALAW;
  133.     ft->info.size = BYTE;
  134.     ft->info.rate = 8000;
  135.  
  136.     wvewriteheader(ft);
  137. }
  138.  
  139. LONG wveread(ft, buf, samp)
  140. ft_t ft;
  141. LONG *buf, samp;
  142. {
  143.     return rawread(ft, buf, samp);
  144. }
  145.  
  146. void wvewrite(ft, buf, samp)
  147. ft_t ft;
  148. LONG *buf, samp;
  149. {
  150.     struct wvepriv *p = (struct wvepriv *) ft->priv;
  151.     p->length += samp * ft->info.size;
  152.     rawwrite(ft, buf, samp);
  153. }
  154.  
  155. void
  156. wvestopwrite(ft)
  157. ft_t ft;
  158. {
  159.     if (!ft->seekable)
  160.         return;
  161.  
  162.     if (fseek(ft->fp, 0L, 0) != 0)
  163.         fail("Can't rewind output file to rewrite Psion header.");
  164.     wvewriteheader(ft);
  165.  
  166.     /* Needed for rawwrite() */
  167.     rawstopwrite(ft);
  168. }
  169.  
  170. void wvewriteheader(ft)
  171. ft_t ft;
  172. {
  173.  
  174.     char magic[16];
  175.     short version;
  176.     short zero;
  177.     struct wvepriv *p = (struct wvepriv *) ft->priv;
  178.  
  179.     strcpy(magic,PSION_MAGIC);
  180.     version=PSION_VERSION;
  181.     zero=0;
  182.  
  183.     fwrite(magic, sizeof(magic), 1, ft->fp);
  184.  
  185.     wshort(ft, version);
  186.     wlong(ft, p->length);
  187.     wshort(ft, p->padding);
  188.     wshort(ft, p->repeats);
  189.  
  190.     wshort(ft, zero);
  191.     wshort(ft, zero);
  192.     wshort(ft, zero);
  193. }
  194.  
  195.