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 / speed.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  2KB  |  90 lines

  1. /* speed.c - change the speed of a sound file
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "libst.h"
  15.  
  16. #define MYBUFSIZ 256
  17. #define SCALE 4096
  18. #define HALFSCALE 2048
  19.  
  20. main( argc, argv )
  21. int argc;
  22. char *argv[];
  23.     {
  24.     FILE *f;
  25.     char mybuf[MYBUFSIZ];
  26.     float scale;
  27.     register long sscale, fractofill, fracleft, sc;
  28.     double atof();
  29.     int c, lc;
  30.  
  31.     if ( argc < 2 || argc > 3 )
  32.     {
  33.     fprintf( stderr, "usage:  %s <factor> [<file>]\n", argv[0] );
  34.     exit( 1 );
  35.     }
  36.  
  37.     scale = atof( argv[1] );
  38.     if ( scale == 0.0 )
  39.     {
  40.     fprintf( stderr, "%s: scale factor required\n", argv[0] );
  41.     exit( 1 );
  42.     }
  43.     scale = 1.0 / scale;
  44.     sscale = scale * SCALE;
  45.  
  46.     if ( argc == 2 )
  47.     f = stdin;
  48.     else if ( argc == 3 )
  49.     {
  50.     f = fopen( argv[2], "r" );
  51.     if ( f == NULL )
  52.         {
  53.         perror( argv[2] );
  54.         exit( 1 );
  55.         }
  56.     }
  57.     setbuffer( stdout, mybuf, MYBUFSIZ );
  58.  
  59.     fractofill = SCALE;
  60.     sc = HALFSCALE;
  61.     while ( (c = getc( f )) != EOF )
  62.     {
  63.     lc = st_ulaw_to_linear( c );
  64.     fracleft = sscale;
  65.     while ( fracleft >= fractofill )
  66.         {
  67.         sc += fractofill * lc;
  68.         sc /= SCALE;
  69.         LINCLIP( sc );
  70.         putchar( st_linear_to_ulaw( sc ) );
  71.         fracleft -= fractofill;
  72.         fractofill = SCALE;
  73.         }
  74.     if ( fracleft > 0 )
  75.         {
  76.         sc += fracleft * lc;
  77.         fractofill -= fracleft;
  78.         }
  79.     }
  80.     if ( fractofill > 0 )
  81.     {
  82.     sc += fractofill * lc;
  83.     sc /= SCALE;
  84.     LINCLIP( sc );
  85.     putchar( st_linear_to_ulaw( sc ) );
  86.     }
  87.  
  88.     exit( 0 );
  89.     }
  90.