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 / pitch.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  3KB  |  122 lines

  1. /* pitch.c - change the pitch 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 "libfft.h"
  15. #include "libst.h"
  16.  
  17. #define MYBUFSIZ 256
  18.  
  19. #define LOG2_SAMPLES_PER_CHUNK 9
  20. #define SAMPLES_PER_CHUNK ( 1 << LOG2_SAMPLES_PER_CHUNK )
  21. #define HALF_SPC ( 1 << ( LOG2_SAMPLES_PER_CHUNK - 1 ) )
  22.  
  23. static complex czero[SAMPLES_PER_CHUNK];
  24.  
  25. main( argc, argv )
  26. int argc;
  27. char *argv[];
  28.     {
  29.     FILE *f;
  30.     char mybuf[MYBUFSIZ];
  31.     float scale;
  32.     double atof();
  33.     int c, i, j, k;
  34.     int linbuf[SAMPLES_PER_CHUNK];
  35.  
  36.     if ( argc < 2 || argc > 3 )
  37.     {
  38.     fprintf( stderr, "usage:  %s <factor> [<file>]\n", argv[0] );
  39.     exit( 1 );
  40.     }
  41.  
  42.     scale = atof( argv[1] );
  43.     if ( scale <= 0.0 )
  44.     {
  45.     fprintf( stderr, "%s: non-negative scale factor required\n", argv[0] );
  46.     exit( 1 );
  47.     }
  48.  
  49.     if ( argc == 2 )
  50.     f = stdin;
  51.     else if ( argc == 3 )
  52.     {
  53.     f = fopen( argv[2], "r" );
  54.     if ( f == NULL )
  55.         {
  56.         perror( argv[2] );
  57.         exit( 1 );
  58.         }
  59.     }
  60.     initfft( LOG2_SAMPLES_PER_CHUNK );
  61.     for (i = 0; i < SAMPLES_PER_CHUNK; ++i)
  62.     czero[i].r = czero[i].i = 0.0;
  63.  
  64.     i = 0;
  65.     while ( (c = getc( f )) != EOF )
  66.     {
  67.     linbuf[i] = st_ulaw_to_linear( c );
  68.     ++i;
  69.     if ( i == SAMPLES_PER_CHUNK )
  70.         {
  71.         shiftchunk( linbuf, i, scale );
  72.         i = 0;
  73.         }
  74.     }
  75.     if ( i > 0 )
  76.     shiftchunk( linbuf, i, scale );
  77.  
  78.     exit( 0 );
  79.     }
  80.  
  81. shiftchunk( linbuf, i, scale )
  82. int linbuf[SAMPLES_PER_CHUNK];
  83. float scale;
  84. int i;
  85.     {
  86.     register int j, kk;
  87.     register float k;
  88.     complex x[SAMPLES_PER_CHUNK];
  89.     complex sx[SAMPLES_PER_CHUNK];
  90.  
  91.     for ( j = 0; j < i; ++j )
  92.     {
  93.     x[j].r = linbuf[j];
  94.     x[j].i = 0.0;
  95.     }
  96.     if ( i < SAMPLES_PER_CHUNK )
  97.     bcopy( czero, &x[i], ( SAMPLES_PER_CHUNK - i ) * sizeof(*x) );
  98.  
  99.     fft( x, 0 );
  100.  
  101. #ifdef notdef
  102. for ( j = 0; j < SAMPLES_PER_CHUNK; ++j )
  103. fprintf( stderr, "%d %g\n", j, x[j].r );
  104. #endif
  105.  
  106.     /* Zero the destination, then loop through the source copying buckets. */
  107.     bcopy( czero, sx, sizeof(sx) );
  108.     for ( j = 0, k = 0.5; j < HALF_SPC; ++j, k += scale )
  109.     {
  110.     kk = (int) k;
  111.     if ( kk >= HALF_SPC )
  112.         break;
  113.     sx[kk] = x[j];
  114.     sx[SAMPLES_PER_CHUNK - 1 - kk] = x[SAMPLES_PER_CHUNK - 1 - j];
  115.     }
  116.  
  117.     fft_real_inverse( sx );
  118.  
  119.     for ( j = 0; j < i; ++j )
  120.     putchar( st_linear_to_ulaw( (int) sx[j].r ) );
  121.     }
  122.