home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / dcom / modems / 20118 < prev    next >
Encoding:
Text File  |  1993-01-28  |  5.9 KB  |  220 lines

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!emory!swrinde!sdd.hp.com!saimiri.primate.wisc.edu!ames!decwrl!oracle!unrepliable!bounce
  2. Newsgroups: comp.dcom.modems
  3. From: on@us.oracle.com (On G. Paradise)
  4. Subject: Re: zyxel adpcm to binary data?
  5. Message-ID: <on.728121930@base>
  6. Keywords: adpcm-2 adpcm-3 zyxel voice mu-law
  7. Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
  8. Nntp-Posting-Host: base.us.oracle.com
  9. Organization: Oracle Corp., Redwood Shores CA
  10. References: <C1G8o2.s4@wsrcc.com>
  11. Date: Wed, 27 Jan 1993 08:05:30 GMT
  12. X-Disclaimer: This message was written by an unauthenticated user
  13.               at Oracle Corporation.  The opinions expressed are those
  14.               of the user and not necessarily those of Oracle.
  15. Lines: 203
  16.  
  17. Wolfgang S. Rupprecht "wolfgang@wsrcc.com" writes:
  18.  
  19. >Does anyone know how to convert to/from zyxel adpcm-2 or adpcm-3 and
  20. >straight binary data?  I'd like to play/record stuff on the Sun and
  21. >then ship it off to the modem.
  22. ...
  23.  
  24. /*
  25.  * Convert ZyXEL 2-bit, 9600 samples/second ADPCM
  26.  * to 8000 samples/second mu-law suitable for Sun4 /dev/audio.
  27.  */
  28. #include <stdio.h>
  29. #define ETX    03
  30. #define DLE    020
  31. #define I    5    /* 48000 / 9600 */
  32. #define O    6    /* 48000 / 8000 */
  33.  
  34. static int buf[I * O];
  35. static int *ip = buf;
  36.  
  37. /*
  38.  * This is not a textbook approach to interpolation,
  39.  * but multiplication is slow on most SPARCs.
  40.  */
  41. ipol(v)
  42. {
  43.     register i;
  44.     register int *op;
  45.  
  46.     for (i = I; i; i--)
  47.         *ip++ = v;
  48.  
  49.     if (ip < buf + I * O)
  50.         return;
  51.  
  52.     ip = buf;
  53.     for (op = buf; op < buf + I * O; ) {
  54.         v = -*op++;    /* hack -- treble boost */
  55.         for (i = O - 2; i; i--)
  56.             v += *op++;
  57.         v -= *op++;
  58.         (void)putchar(st_linear_to_ulaw(v));
  59.     }
  60. }
  61.  
  62. /*
  63.  * Convert till EOF or ^P^C
  64.  */
  65. conv(fp)
  66. register FILE *fp;
  67. {
  68.     register c, i, val = 0;
  69.     register double delta = 5;
  70.  
  71.     while ((c = getc(fp)) != EOF) {
  72.         if (c == DLE)
  73.             switch (c = getc(fp)) {
  74.                 case DLE:
  75.                     break;;
  76.                 case ETX:
  77.                     return(0);
  78.                 case EOF:
  79.                     return(c);
  80.                 default:
  81.                     continue;
  82.             }
  83.  
  84.         for (i = 0200; i; i >>= 1) {
  85.             register double d;
  86.             register neg = c & i;    /* sign */
  87.  
  88.             if (c & (i >>= 1)) {    /* magnitude */
  89.                 d = delta * 48. / 32 + .5;
  90.                 delta *= 43. / 32;
  91.             } else {
  92.                 d = delta * 16. / 32 + .5;
  93.                 delta *= 28. / 32;
  94.             }
  95.             if (delta < 5)
  96.                 delta = 5;
  97.             ipol(val += neg ? -d : d);
  98.         }
  99.     }
  100.     return(c);
  101. }
  102.  
  103. main(argc, argv)
  104. char **argv;
  105. {
  106.     int i, s = 0;
  107.     FILE *fp;
  108.  
  109.     if (argc == 1)
  110.         exit(conv(stdin));
  111.     for (i = 1; i < argc; i++) {
  112.         if (!(fp = fopen(argv[i], "r"))) {
  113.             fprintf(stderr, *argv);
  114.             perror(argv[i]);
  115.             s--;
  116.             continue;
  117.         }
  118.         s += conv(fp);
  119.         (void)fclose(fp);
  120.     }
  121.     exit(s);
  122. }
  123.  
  124. /*
  125. ** This routine converts from linear to ulaw.
  126. **
  127. ** Craig Reese: IDA/Supercomputing Research Center
  128. ** Joe Campbell: Department of Defense
  129. ** 29 September 1989
  130. **
  131. ** References:
  132. ** 1) CCITT Recommendation G.711  (very difficult to follow)
  133. ** 2) "A New Digital Technique for Implementation of Any
  134. **     Continuous PCM Companding Law," Villeret, Michel,
  135. **     et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  136. **     1973, pg. 11.12-11.17
  137. ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
  138. **     for Analog-to_Digital Conversion Techniques,"
  139. **     17 February 1987
  140. **
  141. ** Input: Signed 16 bit linear sample
  142. ** Output: 8 bit ulaw sample
  143. */
  144.  
  145. #define ZEROTRAP    /* turn on the trap as per the MIL-STD */
  146. #define BIAS 0x84   /* define the add-in bias for 16 bit samples */
  147. #define CLIP 32635
  148.  
  149. st_linear_to_ulaw( sample )
  150. int sample;
  151.     {
  152.     static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  153.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  154.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  155.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  156.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  157.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  158.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  159.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  160.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  161.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  162.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  163.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  164.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  165.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  166.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  167.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  168.     int sign, exponent, mantissa;
  169.     unsigned char ulawbyte;
  170.  
  171.     /* Get the sample into sign-magnitude. */
  172.     sign = (sample >> 8) & 0x80;        /* set aside the sign */
  173.     if ( sign != 0 ) sample = -sample;        /* get magnitude */
  174.     if ( sample > CLIP ) sample = CLIP;        /* clip the magnitude */
  175.  
  176.     /* Convert from 16 bit linear to ulaw. */
  177.     sample = sample + BIAS;
  178.     exponent = exp_lut[( sample >> 7 ) & 0xFF];
  179.     mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F;
  180.     ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa );
  181. #ifdef ZEROTRAP
  182.     if ( ulawbyte == 0 ) ulawbyte = 0x02;    /* optional CCITT trap */
  183. #endif
  184.  
  185.     return ulawbyte;
  186.     }
  187.  
  188. /*
  189. ** This routine converts from ulaw to 16 bit linear.
  190. **
  191. ** Craig Reese: IDA/Supercomputing Research Center
  192. ** 29 September 1989
  193. **
  194. ** References:
  195. ** 1) CCITT Recommendation G.711  (very difficult to follow)
  196. ** 2) MIL-STD-188-113,"Interoperability and Performance Standards
  197. **     for Analog-to_Digital Conversion Techniques,"
  198. **     17 February 1987
  199. **
  200. ** Input: 8 bit ulaw sample
  201. ** Output: signed 16 bit linear sample
  202. */
  203.  
  204. int
  205. st_ulaw_to_linear_slow( ulawbyte )
  206. unsigned char ulawbyte;
  207.     {
  208.     static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
  209.     int sign, exponent, mantissa, sample;
  210.  
  211.     ulawbyte = ~ ulawbyte;
  212.     sign = ( ulawbyte & 0x80 );
  213.     exponent = ( ulawbyte >> 4 ) & 0x07;
  214.     mantissa = ulawbyte & 0x0F;
  215.     sample = exp_lut[exponent] + ( mantissa << ( exponent + 3 ) );
  216.     if ( sign != 0 ) sample = -sample;
  217.  
  218.     return sample;
  219.     }
  220.