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

  1. /*
  2.  * September 25, 1991
  3.  * Copyright 1991 Guido van Rossum 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.  * Guido van Rossum And Sundry Contributors are not responsible for 
  7.  * the consequences of using this software.
  8.  */
  9.  
  10. /*
  11.  * Sound Tools SGI/Amiga AIFF format.
  12.  * Used by SGI on 4D/35 and Indigo.
  13.  * This is also part of the IFF format used by the Amiga.
  14.  */
  15.  
  16. #include <math.h>
  17. #include "st.h"
  18.  
  19. /* Private data used by writer */
  20. struct aiffpriv {
  21.     unsigned long nsamples;
  22. };
  23.  
  24. double read_ieee_extended();
  25.  
  26. aiffstartread(ft) 
  27. ft_t ft;
  28. {
  29.     char buf[4];
  30.     unsigned long totalsize;
  31.     unsigned long chunksize;
  32.     int channels;
  33.     unsigned long frames;
  34.     int bits;
  35.     double rate;
  36.     unsigned long offset;
  37.     unsigned long blocksize;
  38.     int littlendian = 0;
  39.     char *endptr;
  40.  
  41.     /* FORM chunk */
  42.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "FORM", 4) != 0)
  43.         fail("AIFF header does not begin with magic word 'FORM'");
  44.     totalsize = rblong(ft);
  45.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "AIFF", 4) != 0)
  46.         fail("AIFF 'FORM' chunk does not specify 'AIFF' as type");
  47.  
  48.     /* This is not completely general; other chunk types may be present
  49.        in AIFF files; but this is all I care about for now. */
  50.  
  51.     /* COMM chunk */
  52.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "COMM", 4) != 0)
  53.         fail("AIFF header doesn't have 'COMM' chunk");
  54.     chunksize = rblong(ft);
  55.     if (chunksize != 18)
  56.         fail("AIFF COMM chunk has bad size");
  57.     channels = rbshort(ft);
  58.     frames = rblong(ft);
  59.     bits = rbshort(ft);
  60.     rate = read_ieee_extended(ft);
  61.  
  62.     /* SSND chunk */
  63.     if (fread(buf, 1, 4, ft->fp) != 4 || strncmp(buf, "SSND", 4) != 0)
  64.         fail("AIFF header doesn't have 'SSND' chunk");
  65.     chunksize = rblong(ft);
  66.     offset = rblong(ft);
  67.     blocksize = rblong(ft);
  68.     if (blocksize != 0)
  69.         fail("AIFF header specifies nonzero blocksize?!?!");
  70.     while (offset > 0) {
  71.         if (getc(ft->fp) == EOF)
  72.             fail("EOF in AIFF file while skipping header");
  73.     }
  74.  
  75.     ft->info.channels = channels;
  76.     ft->info.rate = rate;
  77.     ft->info.style = SIGN2;
  78.     switch (bits) {
  79.     case 8:
  80.         ft->info.size = BYTE;
  81.         break;
  82.     case 16:
  83.         ft->info.size = WORD;
  84.         break;
  85.     default:
  86.         fail("unsupported sample size in AIFF header");
  87.         /*NOTREACHED*/
  88.     }
  89.     endptr = (char *) &littlendian;
  90.     *endptr = 1;
  91.     if (littlendian == 1)
  92.         ft->swap = 1;
  93. }
  94.  
  95. /* When writing, the header is supposed to contain the number of
  96.    samples and data bytes written.
  97.    Since we don't know how many samples there are until we're done,
  98.    we first write the header with an very large number,
  99.    and at the end we rewind the file and write the header again
  100.    with the right number.  This only works if the file is seekable;
  101.    if it is not, the very large size remains in the header.
  102.    Strictly spoken this is not legal, but the playaiff utility
  103.    will still be able to play the resulting file. */
  104.  
  105. aiffstartwrite(ft)
  106. ft_t ft;
  107. {
  108.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  109.     int littlendian = 0;
  110.     char *endptr;
  111.  
  112.     p->nsamples = 0;
  113.     if (ft->info.style == ULAW && ft->info.size == BYTE) {
  114.         report("expanding 8-bit u-law to 16 bits");
  115.         ft->info.size = WORD;
  116.     }
  117.     ft->info.style = SIGN2; /* We have a fixed style */
  118.     /* Compute the "very large number" so that a maximum number
  119.        of samples can be transmitted through a pipe without the
  120.        risk of causing overflow when calculating the number of bytes.
  121.        At 48 kHz, 16 bits stereo, this gives ~3 hours of music.
  122.        Sorry, the AIFF format does not provide for an "infinite"
  123.        number of samples. */
  124.     aiffwriteheader(ft, 0x7f000000 / (ft->info.size*ft->info.channels));
  125.  
  126.     endptr = (char *) &littlendian;
  127.     *endptr = 1;
  128.     if (littlendian == 1)
  129.         ft->swap = 1;
  130. }
  131.  
  132. aiffwrite(ft, buf, len)
  133. ft_t ft;
  134. long *buf, len;
  135. {
  136.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  137.     p->nsamples += len;
  138.     rawwrite(ft, buf, len);
  139. }
  140.  
  141. void
  142. aiffstopwrite(ft)
  143. ft_t ft;
  144. {
  145.     struct aiffpriv *p = (struct aiffpriv *) ft->priv;
  146.     if (!ft->seekable)
  147.         return;
  148.     if (fseek(ft->fp, 0L, 0) != 0)
  149.         fail("can't rewind output file to rewrite AIFF header");
  150.     aiffwriteheader(ft, p->nsamples / ft->info.channels);
  151. }
  152.  
  153. aiffwriteheader(ft, nframes)
  154. ft_t ft;
  155. long nframes;
  156. {
  157.     int hsize =
  158.         8 /*COMM hdr*/ + 18 /*COMM chunk*/ +
  159.         8 /*SSND hdr*/ + 12 /*SSND chunk*/;
  160.     int bits;
  161.  
  162.     if (ft->info.style == SIGN2 && ft->info.size == BYTE)
  163.         bits = 8;
  164.     else if (ft->info.style == SIGN2 && ft->info.size == WORD)
  165.         bits = 16;
  166.     else
  167.         fail("unsupported output style/size for AIFF header");
  168.  
  169.     fputs("FORM", ft->fp); /* IFF header */
  170.     wblong(ft, hsize + nframes * ft->info.size * ft->info.channels); /* file size */
  171.     fputs("AIFF", ft->fp); /* File type */
  172.  
  173.     /* COMM chunk -- describes encoding (and #frames) */
  174.     fputs("COMM", ft->fp);
  175.     wblong(ft, (long) 18); /* COMM chunk size */
  176.     wbshort(ft, ft->info.channels); /* nchannels */
  177.     wblong(ft, nframes); /* number of frames */
  178.     wbshort(ft, bits); /* sample width, in bits */
  179.     write_ieee_extended(ft, (double)ft->info.rate);
  180.  
  181.     /* SSND chunk -- describes data */
  182.     fputs("SSND", ft->fp);
  183.     wblong(ft, 8 + nframes * ft->info.channels * ft->info.size); /* chunk size */
  184.     wblong(ft, (long) 0); /* offset */
  185.     wblong(ft, (long) 0); /* block size */
  186. }
  187.  
  188. double ConvertFromIeeeExtended();
  189.  
  190. double read_ieee_extended(ft)
  191. ft_t ft;
  192. {
  193.     char buf[10];
  194.     if (fread(buf, 1, 10, ft->fp) != 10)
  195.         fail("EOF while reading IEEE extended number");
  196.     return ConvertFromIeeeExtended(buf);
  197. }
  198.  
  199. write_ieee_extended(ft, x)
  200. ft_t ft;
  201. double x;
  202. {
  203.     char buf[10];
  204.     ConvertToIeeeExtended(x, buf);
  205.     /*
  206.     report("converted %g to %o %o %o %o %o %o %o %o %o %o",
  207.         x,
  208.         buf[0], buf[1], buf[2], buf[3], buf[4],
  209.         buf[5], buf[6], buf[7], buf[8], buf[9]);
  210.     */
  211.     (void) fwrite(buf, 1, 10, ft->fp);
  212. }
  213.  
  214.  
  215. /*
  216.  * C O N V E R T   T O   I E E E   E X T E N D E D
  217.  */
  218.  
  219. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  220.  * All rights reserved.
  221.  *
  222.  * Machine-independent I/O routines for IEEE floating-point numbers.
  223.  *
  224.  * NaN's and infinities are converted to HUGE_VAL or HUGE, which
  225.  * happens to be infinity on IEEE machines.  Unfortunately, it is
  226.  * impossible to preserve NaN's in a machine-independent way.
  227.  * Infinities are, however, preserved on IEEE machines.
  228.  *
  229.  * These routines have been tested on the following machines:
  230.  *    Apple Macintosh, MPW 3.1 C compiler
  231.  *    Apple Macintosh, THINK C compiler
  232.  *    Silicon Graphics IRIS, MIPS compiler
  233.  *    Cray X/MP and Y/MP
  234.  *    Digital Equipment VAX
  235.  *
  236.  *
  237.  * Implemented by Malcolm Slaney and Ken Turkowski.
  238.  *
  239.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  240.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  241.  * floating-point format, and conversions to and from IEEE single-
  242.  * precision floating-point format.
  243.  *
  244.  * In 1991, Ken Turkowski implemented the conversions to and from
  245.  * IEEE double-precision format, added more precision to the extended
  246.  * conversions, and accommodated conversions involving +/- infinity,
  247.  * NaN's, and denormalized numbers.
  248.  */
  249.  
  250. #ifndef HUGE_VAL
  251. # define HUGE_VAL HUGE
  252. #endif /*HUGE_VAL*/
  253.  
  254. # define FloatToUnsigned(f)      ((unsigned long)(((long)(f - 2147483648.0)) + 2147483647L + 1))
  255.  
  256. ConvertToIeeeExtended(num, bytes)
  257. double num;
  258. char *bytes;
  259. {
  260.     int    sign;
  261.     int expon;
  262.     double fMant, fsMant;
  263.     unsigned long hiMant, loMant;
  264.  
  265.     if (num < 0) {
  266.         sign = 0x8000;
  267.         num *= -1;
  268.     } else {
  269.         sign = 0;
  270.     }
  271.  
  272.     if (num == 0) {
  273.         expon = 0; hiMant = 0; loMant = 0;
  274.     }
  275.     else {
  276.         fMant = frexp(num, &expon);
  277.         if ((expon > 16384) || !(fMant < 1)) {    /* Infinity or NaN */
  278.             expon = sign|0x7FFF; hiMant = 0; loMant = 0; /* infinity */
  279.         }
  280.         else {    /* Finite */
  281.             expon += 16382;
  282.             if (expon < 0) {    /* denormalized */
  283.                 fMant = ldexp(fMant, expon);
  284.                 expon = 0;
  285.             }
  286.             expon |= sign;
  287.             fMant = ldexp(fMant, 32);          
  288.             fsMant = floor(fMant); 
  289.             hiMant = FloatToUnsigned(fsMant);
  290.             fMant = ldexp(fMant - fsMant, 32); 
  291.             fsMant = floor(fMant); 
  292.             loMant = FloatToUnsigned(fsMant);
  293.         }
  294.     }
  295.     
  296.     bytes[0] = expon >> 8;
  297.     bytes[1] = expon;
  298.     bytes[2] = hiMant >> 24;
  299.     bytes[3] = hiMant >> 16;
  300.     bytes[4] = hiMant >> 8;
  301.     bytes[5] = hiMant;
  302.     bytes[6] = loMant >> 24;
  303.     bytes[7] = loMant >> 16;
  304.     bytes[8] = loMant >> 8;
  305.     bytes[9] = loMant;
  306. }
  307.  
  308.  
  309. /*
  310.  * C O N V E R T   F R O M   I E E E   E X T E N D E D  
  311.  */
  312.  
  313. /* 
  314.  * Copyright (C) 1988-1991 Apple Computer, Inc.
  315.  * All rights reserved.
  316.  *
  317.  * Machine-independent I/O routines for IEEE floating-point numbers.
  318.  *
  319.  * NaN's and infinities are converted to HUGE_VAL or HUGE, which
  320.  * happens to be infinity on IEEE machines.  Unfortunately, it is
  321.  * impossible to preserve NaN's in a machine-independent way.
  322.  * Infinities are, however, preserved on IEEE machines.
  323.  *
  324.  * These routines have been tested on the following machines:
  325.  *    Apple Macintosh, MPW 3.1 C compiler
  326.  *    Apple Macintosh, THINK C compiler
  327.  *    Silicon Graphics IRIS, MIPS compiler
  328.  *    Cray X/MP and Y/MP
  329.  *    Digital Equipment VAX
  330.  *
  331.  *
  332.  * Implemented by Malcolm Slaney and Ken Turkowski.
  333.  *
  334.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  335.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  336.  * floating-point format, and conversions to and from IEEE single-
  337.  * precision floating-point format.
  338.  *
  339.  * In 1991, Ken Turkowski implemented the conversions to and from
  340.  * IEEE double-precision format, added more precision to the extended
  341.  * conversions, and accommodated conversions involving +/- infinity,
  342.  * NaN's, and denormalized numbers.
  343.  */
  344.  
  345. #ifndef HUGE_VAL
  346. # define HUGE_VAL HUGE
  347. #endif /*HUGE_VAL*/
  348.  
  349. # define UnsignedToFloat(u)         (((double)((long)(u - 2147483647L - 1))) + 2147483648.0)
  350.  
  351. /****************************************************************
  352.  * Extended precision IEEE floating-point conversion routine.
  353.  ****************************************************************/
  354.  
  355. double ConvertFromIeeeExtended(bytes)
  356. unsigned char *bytes;    /* LCN */
  357. {
  358.     double    f;
  359.     int    expon;
  360.     unsigned long hiMant, loMant;
  361.     
  362.     expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
  363.     hiMant    =    ((unsigned long)(bytes[2] & 0xFF) << 24)
  364.             |    ((unsigned long)(bytes[3] & 0xFF) << 16)
  365.             |    ((unsigned long)(bytes[4] & 0xFF) << 8)
  366.             |    ((unsigned long)(bytes[5] & 0xFF));
  367.     loMant    =    ((unsigned long)(bytes[6] & 0xFF) << 24)
  368.             |    ((unsigned long)(bytes[7] & 0xFF) << 16)
  369.             |    ((unsigned long)(bytes[8] & 0xFF) << 8)
  370.             |    ((unsigned long)(bytes[9] & 0xFF));
  371.  
  372.     if (expon == 0 && hiMant == 0 && loMant == 0) {
  373.         f = 0;
  374.     }
  375.     else {
  376.         if (expon == 0x7FFF) {    /* Infinity or NaN */
  377.             f = HUGE_VAL;
  378.         }
  379.         else {
  380.             expon -= 16383;
  381.             f  = ldexp(UnsignedToFloat(hiMant), expon-=31);
  382.             f += ldexp(UnsignedToFloat(loMant), expon-=32);
  383.         }
  384.     }
  385.  
  386.     if (bytes[0] & 0x80)
  387.         return -f;
  388.     else
  389.         return f;
  390. }
  391.