home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / dsp / 2892 < prev    next >
Encoding:
Text File  |  1993-01-01  |  5.6 KB  |  162 lines

  1. Newsgroups: comp.dsp
  2. Path: sparky!uunet!super!cfreese
  3. From: cfreese@super.org (Craig F. Reese)
  4. Subject: Re: Format of .AU Sound files               
  5. Message-ID: <1992Dec31.185650.13748@super.org>
  6. Sender: news@super.org (USENET News System)
  7. Nntp-Posting-Host: super
  8. Organization: Supercomputing Research Center (Bowie, MD)
  9. References: <memo.834974@cix.compulink.co.uk>
  10. Date: Thu, 31 Dec 1992 18:56:50 GMT
  11. Lines: 149
  12.  
  13. In article <memo.834974@cix.compulink.co.uk> rspence@cix.compulink.co.uk writes:
  14. >christos@kuhub.cc.ukans.edu () writes:
  15. >
  16. >> Hello there,
  17. >
  18. >> I am using the Public Domain program Sound Tools on the Sun station
  19. >> to record speech. The speech samples are saved in a mu-law 8bits/sample
  20. >> encoding rule with a standard ".au" header precceding the data. What I
  21. >> would like to know is what the format of the speech samples is? In other
  22.     .
  23.     .
  24.     .
  25. >
  26. >So much for background. If I remember correctly, the most significant bit is
  27. >a sign bit, indicating positive (0) or negative (1) value. The remaining 7
  28. >bits are the encoded amplitude of the sample. Manipulation will require you
  29. >to decode the sample correctly, and I don't have the details of mu-law
  30. >encoding to hand. But, I know where it is described. Try and get hold of the
  31. >CCITT's G.7xx series recommendations which is one large volume. Try a
  32. >technical library of a university. Alternatively, you could ask very nicely
  33. >and I will try to mail the relevant pages when I'm over in the US in January.
  34. >
  35.  
  36. Here's some code I wrote many moons ago.  It should help.
  37.  
  38. Craig
  39.  
  40. =============================================================
  41.  
  42. TWIMC,
  43.  
  44. Below are two routines I wrote for converting between ulaw and linear. 
  45. I use them with the SparcStation and they seem to work fine.
  46. I am pretty sure (99.9%) that they implement the standard as specified
  47. in the references. 
  48.  
  49. Note that the standard deals with converting between 12 bit linear
  50. and 8 bit ulaw.  These routines assume 16 bit linear.  Thus, some
  51. bit shifting may be necessary.
  52.  
  53. craig
  54.  
  55. ------------------------------------------------------------------
  56. /**
  57.  ** Signal conversion routines for use with the Sun4/60 audio chip
  58.  **/
  59.  
  60. /*
  61.  * This routine converts from linear to ulaw
  62.  * 29 September 1989
  63.  *
  64.  * Craig Reese: IDA/Supercomputing Research Center
  65.  * Joe Campbell: Department of Defense
  66.  *
  67.  * References:
  68.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  69.  * 2) "A New Digital Technique for Implementation of Any 
  70.  *     Continuous PCM Companding Law," Villeret, Michel,
  71.  *     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  72.  *     1973, pg. 11.12-11.17
  73.  * 3) MIL-STD-188-113,"Interoperability and Performance Standards
  74.  *     for Analog-to_Digital Conversion Techniques,"
  75.  *     17 February 1987
  76.  *
  77.  * Input: Signed 16 bit linear sample
  78.  * Output: 8 bit ulaw sample
  79.  */
  80.  
  81. #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  82. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  83. #define CLIP 32635
  84.  
  85. unsigned char
  86. linear2ulaw(sample)
  87. int sample;
  88. {
  89.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  90.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  91.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  92.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  93.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  94.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  95.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  96.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  97.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  98.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  99.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  100.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  101.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  102.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  103.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  104.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  105.     int sign, exponent, mantissa;
  106.     unsigned char ulawbyte;
  107.  
  108.     /** get the sample into sign-magnitude **/
  109.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  110.     if (sign != 0) sample = -sample;    /* get magnitude */
  111.     if (sample > CLIP) sample = CLIP;   /* clip the magnitude */
  112.     /** convert from 16 bit linear to ulaw **/
  113.     sample = sample + BIAS;
  114.     exponent = exp_lut[(sample>>7) & 0xFF];
  115.     mantissa = (sample >> (exponent+3)) & 0x0F;
  116.     ulawbyte = ~(sign | (exponent << 4) | mantissa);
  117. #ifdef ZEROTRAP
  118.     if (ulawbyte == 0 ) ulawbyte = 0x02;  /* optional CCITT trap */
  119. #endif
  120.     /** return the result **/
  121.     return(ulawbyte);
  122.     }
  123.  
  124. /*
  125.  * This routine converts from ulaw to 16 bit linear
  126.  * 29 September 1989
  127.  *
  128.  * Craig Reese: IDA/Supercomputing Research Center
  129.  *
  130.  * References:
  131.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  132.  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
  133.  *     for Analog-to_Digital Conversion Techniques,"
  134.  *     17 February 1987
  135.  *
  136.  * Input: 8 bit ulaw sample
  137.  * Output: signed 16 bit linear sample
  138.  */
  139.  
  140. int
  141. ulaw2linear(ulawbyte)
  142. unsigned char ulawbyte;
  143. {
  144.     static int exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
  145.     int sign, exponent, mantissa, sample;
  146.  
  147.     ulawbyte = ~ulawbyte;
  148.     sign = (ulawbyte & 0x80);
  149.     exponent = (ulawbyte >> 4) & 0x07;
  150.     mantissa = ulawbyte & 0x0F;
  151.     sample = exp_lut[exponent] + (mantissa << (exponent+3));
  152.     if (sign != 0) sample = -sample;
  153.     return(sample);
  154.     }
  155.  
  156. -----------------
  157. Craig F. Reese                           Email: cfreese@super.org
  158. Institute for Defense Analyses/
  159. Supercomputing Research Center
  160. 17100 Science Dr.
  161. Bowie, MD  20715-4300
  162.