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

  1. /* libst.c - portable sound tools library
  2. */
  3.  
  4. /*
  5. ** This routine converts from linear to ulaw.
  6. **
  7. ** Craig Reese: IDA/Supercomputing Research Center
  8. ** Joe Campbell: Department of Defense
  9. ** 29 September 1989
  10. **
  11. ** References:
  12. ** 1) CCITT Recommendation G.711  (very difficult to follow)
  13. ** 2) "A New Digital Technique for Implementation of Any
  14. **     Continuous PCM Companding Law," Villeret, Michel,
  15. **     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  16. **     1973, pg. 11.12-11.17
  17. ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
  18. **     for Analog-to_Digital Conversion Techniques,"
  19. **     17 February 1987
  20. **
  21. ** Input: Signed 16 bit linear sample
  22. ** Output: 8 bit ulaw sample
  23. */
  24.  
  25. #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  26. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  27. #define CLIP 32635
  28.  
  29. unsigned char
  30. st_linear_to_ulaw( sample )
  31. int sample;
  32.     {
  33.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  34.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  35.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  36.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  37.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  38.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  39.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  40.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  41.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  42.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  43.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  44.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  45.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  46.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  47.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  48.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  49.     int sign, exponent, mantissa;
  50.     unsigned char ulawbyte;
  51.  
  52.     /* Get the sample into sign-magnitude. */
  53.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  54.     if ( sign != 0 ) sample = -sample;        /* get magnitude */
  55.     if ( sample > CLIP ) sample = CLIP;        /* clip the magnitude */
  56.  
  57.     /* Convert from 16 bit linear to ulaw. */
  58.     sample = sample + BIAS;
  59.     exponent = exp_lut[( sample >> 7 ) & 0xFF];
  60.     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
  61.     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
  62. #ifdef ZEROTRAP
  63.     if ( ulawbyte == 0 ) ulawbyte = 0x02;    /* optional CCITT trap */
  64. #endif
  65.  
  66.     return ulawbyte;
  67.     }
  68.  
  69. /*
  70. ** This routine converts from ulaw to 16 bit linear.
  71. **
  72. ** Craig Reese: IDA/Supercomputing Research Center
  73. ** 29 September 1989
  74. **
  75. ** References:
  76. ** 1) CCITT Recommendation G.711  (very difficult to follow)
  77. ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
  78. **     for Analog-to_Digital Conversion Techniques,"
  79. **     17 February 1987
  80. **
  81. ** Input: 8 bit ulaw sample
  82. ** Output: signed 16 bit linear sample
  83. */
  84.  
  85. int
  86. st_ulaw_to_linear_slow( ulawbyte )
  87. unsigned char ulawbyte;
  88.     {
  89.     static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
  90.     int sign, exponent, mantissa, sample;
  91.  
  92.     ulawbyte = ~ ulawbyte;
  93.     sign = ( ulawbyte & 0x80 );
  94.     exponent = ( ulawbyte >> 4 ) & 0x07;
  95.     mantissa = ulawbyte & 0x0F;
  96.     sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
  97.     if ( sign != 0 ) sample = -sample;
  98.  
  99.     return sample;
  100.     }
  101.