home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / sgi / 11291 < prev    next >
Encoding:
Text File  |  1992-07-23  |  4.1 KB  |  123 lines

  1. Newsgroups: comp.sys.sgi
  2. Path: sparky!uunet!darwin.sura.net!mips!odin!fido!bonk.esd.sgi.com!cpirazzi
  3. From: cpirazzi@bonk.esd.sgi.com (Chris Pirazzi)
  4. Subject: Re: ulaw format wanted
  5. Message-ID: <nm4rfes@fido.asd.sgi.com>
  6. Keywords: mulaw sound file format
  7. Sender: news@fido.asd.sgi.com (Usenet News Admin)
  8. Organization: Silicon Graphics, Inc., Mountain View, CA
  9. References: <23286@alice.att.com>
  10. Date: Fri, 24 Jul 1992 01:43:57 GMT
  11. Lines: 110
  12.  
  13. In article <23286@alice.att.com> jsh@alice.att.com (Juergen Schroeter) writes:
  14. >Hi,
  15. >
  16. >I'd like to get hold of the exact file format of the SGI utilities'
  17. >(e.g., sfconvert, sfplay) mulaw option.  Any soul out there who could
  18. >give me a pointer/share it with me?
  19. >
  20. >Juergen Schroeter, jsh@alice.att.com
  21. >(908) 582-7059
  22. >AT&T Bell Labs, Murray Hill, NJ 07974-0636
  23.  
  24. Hi,
  25.  
  26. Here are the formulas used:
  27.  
  28. /*
  29.  * This routine converts from ulaw to 16 bit linear
  30.  * 29 September 1989
  31.  *
  32.  * Craig Reese: IDA/Supercomputing Research Center
  33.  *
  34.  * References:
  35.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  36.  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
  37.  *     for Analog-to_Digital Conversion Techniques,"
  38.  *     17 February 1987
  39.  *
  40.  * Input: 8 bit ulaw sample
  41.  * Output: signed 16 bit linear sample
  42.  */
  43.  
  44. short 
  45. ulaw2linear(unsigned char ulawbyte) {
  46.     static int exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
  47.     int sign, exponent, mantissa, sample;
  48.  
  49.     ulawbyte = ~ulawbyte;
  50.     sign = (ulawbyte & 0x80);
  51.     exponent = (ulawbyte >> 4) & 0x07;
  52.     mantissa = ulawbyte & 0x0F;
  53.     sample = exp_lut[exponent] + (mantissa << (exponent+3));
  54.     if (sign != 0) sample = -sample;
  55.     return(sample);
  56. }
  57.  
  58. /*
  59.  * This routine converts from linear to ulaw
  60.  * 29 September 1989
  61.  *
  62.  * Craig Reese: IDA/Supercomputing Research Center
  63.  * Joe Campbell: Department of Defense
  64.  *
  65.  * References:
  66.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  67.  * 2) "A New Digital Technique for Implementation of Any 
  68.  *     Continuous PCM Companding Law," Villeret, Michel,
  69.  *     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  70.  *     1973, pg. 11.12-11.17
  71.  * 3) MIL-STD-188-113,"Interoperability and Performance Standards
  72.  *     for Analog-to_Digital Conversion Techniques,"
  73.  *     17 February 1987
  74.  *
  75.  * Input: Signed 16 bit linear sample
  76.  * Output: 8 bit ulaw sample
  77.  */
  78.  
  79. #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  80. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  81. #define CLIP 32635
  82.  
  83. unsigned char 
  84. linear2ulaw(short sample)
  85. {
  86.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  87.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  88.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  89.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  90.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  91.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  92.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  93.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  94.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  95.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  96.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  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.     int sign, exponent, mantissa;
  103.     unsigned char ulawbyte;
  104.  
  105.     /** get the sample into sign-magnitude **/
  106.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  107.     if (sign != 0) sample = -sample;    /* get magnitude */
  108.     if (sample > CLIP) sample = CLIP;   /* clip the magnitude */
  109.     /** convert from 16 bit linear to ulaw **/
  110.     sample = sample + BIAS;
  111.     exponent = exp_lut[(sample>>7) & 0xFF];
  112.     mantissa = (sample >> (exponent+3)) & 0x0F;
  113.     ulawbyte = ~(sign | (exponent << 4) | mantissa);
  114. #ifdef ZEROTRAP
  115.     if (ulawbyte == 0 ) ulawbyte = 0x02;  /* optional CCITT trap */
  116. #endif
  117.     /** return the result **/
  118.     return(ulawbyte);
  119. }
  120.  
  121.  
  122.     - Chris Pirazzi
  123.