ulaw.txt
上传用户:szhypcb168
上传日期:2007-01-06
资源大小:2187k
文件大小:4k
源码类别:

语音压缩

开发平台:

Unix_Linux

  1. Path: super!cfreese
  2. From: cfreese@super.ORG (Craig F. Reese)
  3. Newsgroups: comp.dsp
  4. Subject: SPARCStation audio conversion (ulaw <-> linear)
  5. Message-ID: <14899@super.ORG>
  6. Date: 29 Sep 89 23:23:49 GMT
  7. Sender: news@super.ORG
  8. Reply-To: cfreese@super.UUCP (Craig F. Reese)
  9. Distribution: na
  10. Organization: Supercomputing Research Center, Bowie, Md.
  11. Lines: 120
  12. TWIMC,
  13. Below are two routines I wrote for converting between ulaw and linear. 
  14. I use them with the SparcStation and they seem to work fine.
  15. I am pretty sure (99.9%) that they implement the standard as specified
  16. in the references. 
  17. Note that the standard deals with converting between 12 bit linear
  18. and 8 bit ulaw.  These routines assume 16 bit linear.  Thus, some
  19. bit shifting may be necessary.
  20. craig
  21. ------------------------------------------------------------------
  22. /**
  23.  ** Signal conversion routines for use with the Sun4/60 audio chip
  24.  **/
  25. /*
  26.  * This routine converts from linear to ulaw
  27.  * 29 September 1989
  28.  *
  29.  * Craig Reese: IDA/Supercomputing Research Center
  30.  * Joe Campbell: Department of Defense
  31.  *
  32.  * References:
  33.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  34.  * 2) "A New Digital Technique for Implementation of Any 
  35.  *     Continuous PCM Companding Law," Villeret, Michel,
  36.  *     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  37.  *     1973, pg. 11.12-11.17
  38.  * 3) MIL-STD-188-113,"Interoperability and Performance Standards
  39.  *     for Analog-to_Digital Conversion Techniques,"
  40.  *     17 February 1987
  41.  *
  42.  * Input: Signed 16 bit linear sample
  43.  * Output: 8 bit ulaw sample
  44.  */
  45. #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  46. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  47. #define CLIP 32635
  48. unsigned char
  49. linear2ulaw(sample)
  50. int sample;
  51. {
  52.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  53.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  54.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  55.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  56.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  57.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  58.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  59.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  60.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  61.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  62.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  63.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  64.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  65.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  66.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  67.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  68.     int sign, exponent, mantissa;
  69.     unsigned char ulawbyte;
  70.     /** get the sample into sign-magnitude **/
  71.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  72.     if (sign != 0) sample = -sample;    /* get magnitude */
  73.     if (sample > CLIP) sample = CLIP;   /* clip the magnitude */
  74.     /** convert from 16 bit linear to ulaw **/
  75.     sample = sample + BIAS;
  76.     exponent = exp_lut[(sample>>7) & 0xFF];
  77.     mantissa = (sample >> (exponent+3)) & 0x0F;
  78.     ulawbyte = ~(sign | (exponent << 4) | mantissa);
  79. #ifdef ZEROTRAP
  80.     if (ulawbyte == 0 ) ulawbyte = 0x02;  /* optional CCITT trap */
  81. #endif
  82.     /** return the result **/
  83.     return(ulawbyte);
  84.     }
  85. /*
  86.  * This routine converts from ulaw to 16 bit linear
  87.  * 29 September 1989
  88.  *
  89.  * Craig Reese: IDA/Supercomputing Research Center
  90.  *
  91.  * References:
  92.  * 1) CCITT Recommendation G.711  (very difficult to follow)
  93.  * 2) MIL-STD-188-113,"Interoperability and Performance Standards
  94.  *     for Analog-to_Digital Conversion Techniques,"
  95.  *     17 February 1987
  96.  *
  97.  * Input: 8 bit ulaw sample
  98.  * Output: signed 16 bit linear sample
  99.  */
  100. int
  101. ulaw2linear(ulawbyte)
  102. unsigned char ulawbyte;
  103. {
  104.     static int exp_lut[8]={0,132,396,924,1980,4092,8316,16764};
  105.     int sign, exponent, mantissa, sample;
  106.     ulawbyte = ~ulawbyte;
  107.     sign = (ulawbyte & 0x80);
  108.     exponent = (ulawbyte >> 4) & 0x07;
  109.     mantissa = ulawbyte & 0x0F;
  110.     sample = exp_lut[exponent] + (mantissa << (exponent+3));
  111.     if (sign != 0) sample = -sample;
  112.     return(sample);
  113.     }
  114. -----------------
  115. Craig F. Reese                           Email: cfreese@super.org
  116. Institute for Defense Analyses/
  117. Supercomputing Research Center
  118. 17100 Science Dr.
  119. Bowie, MD  20715-4300