home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / sst.tar.Z / sst.tar / sst / mactosun.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  5KB  |  163 lines

  1. /************************************************************************/
  2. /*      Copyright 1989 by Rich Gopstein and Harris Corporation          */
  3. /*                                                                      */
  4. /*      Permission to use, copy, modify, and distribute this software   */
  5. /*      and its documentation for any purpose and without fee is        */
  6. /*      hereby granted, provided that the above copyright notice        */
  7. /*      appears in all copies and that both that copyright notice and   */
  8. /*      this permission notice appear in supporting documentation, and  */
  9. /*      that the name of Rich Gopstein and Harris Corporation not be    */
  10. /*      used in advertising or publicity pertaining to distribution     */
  11. /*      of the software without specific, written prior permission.     */
  12. /*      Rich Gopstein and Harris Corporation make no representations    */
  13. /*      about the suitability of this software for any purpose.  It     */
  14. /*      provided "as is" without express or implied warranty.           */
  15. /************************************************************************/
  16.  
  17. /************************************************************************/
  18. /* mactosun.c - Convert sampled audio files into uLAW format for the    */
  19. /*               Sparcstation 1.                                        */
  20. /*               Send comments to ..!rutgers!soleil!gopstein            */
  21. /*               Modified by jef@helios.ee.lbl.gov.                     */
  22. /************************************************************************/
  23.  
  24. #include <stdio.h>
  25.  
  26. #define DEFAULT_FREQUENCY 11000
  27.  
  28. /* convert two's complement ch into uLAW format */
  29.  
  30. unsigned int cvt(ch)
  31. int ch;
  32. {
  33.  
  34.   int mask;
  35.  
  36.   if (ch < 0) {
  37.     ch = -ch;
  38.     mask = 0x7f;
  39.   } else {
  40.     mask = 0xff;
  41.   }
  42.  
  43.   if (ch < 32) {
  44.     ch = 0xF0 | 15 - (ch / 2);
  45.   } else if (ch < 96) {
  46.     ch = 0xE0 | 15 - (ch - 32) / 4;
  47.   } else if (ch < 224) {
  48.     ch = 0xD0 | 15 - (ch - 96) / 8;
  49.   } else if (ch < 480) {
  50.     ch = 0xC0 | 15 - (ch - 224) / 16;
  51.   } else if (ch < 992) {
  52.     ch = 0xB0 | 15 - (ch - 480) / 32;
  53.   } else if (ch < 2016) {
  54.     ch = 0xA0 | 15 - (ch - 992) / 64;
  55.   } else if (ch < 4064) {
  56.     ch = 0x90 | 15 - (ch - 2016) / 128;
  57.   } else if (ch < 8160) {
  58.     ch = 0x80 | 15 - (ch - 4064) /  256;
  59.   } else {
  60.     ch = 0x80;
  61.   }
  62. return (mask & ch);
  63. }
  64.  
  65. /*******************************************************
  66. /*                                                     */
  67. /* Usage is "mactosun [-f frequency] [infile]"         */
  68. /*                                                     */
  69. /* "frequency" is the samples per second of the infile */
  70. /* The output is always 8000 samples per second.       */
  71. /*                                                     */
  72. /*******************************************************/
  73.  
  74. /***********************************************************************/
  75. /*                                                                     */
  76. /* The input file is expected to be a stream of one-byte excess-128    */
  77. /* samples.  Each sample is converted to 2's complement by subtracting */
  78. /* 128, then converted to uLAW and output.  We calculate the proper    */
  79. /* number of input bytes to skip in order to make the sample frequency */
  80. /* convert to 8000/sec properly.  Interpolation could be added, but it */
  81. /* doesn't appear to be necessary.                                     */
  82. /*                                                                     */
  83. /***********************************************************************/
  84.  
  85.  
  86. main(argc, argv)
  87. int argc;
  88. char *argv[];
  89. {
  90.  
  91.   float sum = 0;
  92.   float frequency, increment;
  93.  
  94.   unsigned char ch;
  95.   unsigned char ulaw;
  96.  
  97.   int argn, chr;
  98.   char *usage = "usage:  %s [-f frequency] [infile]\n";
  99.  
  100.   argn = 1;
  101.   frequency = DEFAULT_FREQUENCY;
  102.  
  103.   if (argn + 1 < argc && argv[argn][0] == '-') {
  104.     if (strcmp(argv[argn], "-f") != 0) {
  105.       fprintf(stderr, usage, argv[0]);
  106.       exit(1);
  107.     } else {
  108.       ++argn;
  109.       frequency = atoi(argv[argn]);
  110.       ++argn;
  111.     }
  112.   }
  113.  
  114.   if ( argn < argc ) {
  115.     if (freopen(argv[argn], "r", stdin) == NULL) {
  116.       perror("Error opening infile");
  117.       exit(0);
  118.     }
  119.     ++argn;
  120.   }
  121.  
  122.   if (argn != argc) {
  123.     fprintf(stderr, usage, argv[0]);
  124.     exit(1);
  125.   }
  126.  
  127.  
  128.   /* increment is the number of bytes to read each time */
  129.  
  130.   increment = frequency / 8000;
  131.  
  132.   ch = fgetc(stdin);
  133.  
  134.   while (!feof(stdin)) {
  135.  
  136.     /* convert the excess 128 to two's complement */
  137.  
  138.     chr = 0x80 - ch;
  139.  
  140.     /* increase the volume */
  141.     /* convert to uLAW */
  142.  
  143.     ulaw = cvt(chr * 16);
  144.  
  145.     /* output it */
  146.  
  147.     fputc((char) ulaw, stdout);
  148.  
  149.     /* skip enough input bytes to compensate for sampling frequency diff */
  150.  
  151.     sum += increment;
  152.  
  153.     while(sum > 0) {
  154.       if (!feof(stdin)) ch = fgetc(stdin);
  155.       sum--;
  156.     }
  157.  
  158.   }
  159.  
  160.   fclose(stdin);
  161.   fclose(stdout);
  162. }
  163.