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

  1. /* echoplex.c - echo generator
  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 abs(a) ((a) >= 0 ? (a) : -(a))
  17.  
  18. #define FADE_THRESH 30
  19. #define MYBUFSIZ 256
  20. #define DELAYBUFSIZ ( 50 * SAMPLES_PER_SECOND )
  21. #define MAXDELAYS 1000
  22.  
  23. main( argc, argv )
  24. int argc;
  25. char *argv[];
  26.     {
  27.     FILE *f;
  28.     char mybuf[MYBUFSIZ];
  29.     int argn, numdelays;
  30.     register int i, j, c, lc, plc, pplc, ppplc;
  31.     static int delaybuf[DELAYBUFSIZ];
  32.     float delay[MAXDELAYS], atten[MAXDELAYS];
  33.     int samples[MAXDELAYS], maxsamples;
  34.     double atof();
  35.     char *usage = "usage:  %s <delay> <atten> [<delay> <atten>] ... [<file>]\n";
  36.  
  37.     argn = 1;
  38.     numdelays = 0;
  39.     maxsamples = 0;
  40.  
  41.     while ( argn + 1 < argc )
  42.     {
  43.     delay[numdelays] = atof( argv[argn++] );
  44.     atten[numdelays] = atof( argv[argn++] );
  45.     samples[numdelays] = delay[numdelays] * SAMPLES_PER_SECOND;
  46.     if ( samples[numdelays] < 1 )
  47.         {
  48.         fprintf( stderr, "%s: delay must positive, aye!\n", argv[0] );
  49.         exit( 1 );
  50.         }
  51.     if ( samples[numdelays] > DELAYBUFSIZ )
  52.         {
  53.         fprintf(
  54.         stderr, "%s: delay must be less than %g seconds\n",
  55.         argv[0], DELAYBUFSIZ / (float) SAMPLES_PER_SECOND );
  56.         exit( 1 );
  57.         }
  58.     if ( atten[numdelays] < 0.0 )
  59.         {
  60.         fprintf( stderr, "%s: attenuation must positive, aye!\n", argv[0] );
  61.         exit( 1 );
  62.         }
  63.     if ( samples[numdelays] > maxsamples )
  64.         maxsamples = samples[numdelays];
  65.     ++numdelays;
  66.     }
  67.  
  68.     if ( argn == argc )
  69.     f = stdin;
  70.     else
  71.     {
  72.     f = fopen( argv[argn], "r" );
  73.     if ( f == NULL )
  74.         {
  75.         perror( argv[argn] );
  76.         exit( 1 );
  77.         }
  78.     ++argn;
  79.     }
  80.  
  81.     if ( argn != argc )
  82.     {
  83.     fprintf( stderr, usage, argv[0] );
  84.     exit( 1 );
  85.     }
  86.  
  87.  
  88.     setbuffer( stdout, mybuf, MYBUFSIZ );
  89.  
  90.     for ( i = 0; i < maxsamples; ++i )
  91.     delaybuf[i] = 0;
  92.  
  93.     for ( i = 0; (c = getc( f )) != EOF; i = ( i + 1 ) % maxsamples )
  94.     {
  95.     lc = st_ulaw_to_linear( c );
  96.     for ( j = 0; j < numdelays; ++j )
  97.         lc = lc + delaybuf[( i + maxsamples - samples[j] ) % maxsamples] * atten[j];
  98.     LINCLIP( lc );
  99.     delaybuf[i] = lc;
  100.     putchar( st_linear_to_ulaw( lc ) );
  101.     }
  102.  
  103.     for ( ppplc = pplc = plc = MAXLIN;
  104.       abs(lc) + abs(plc) + abs(pplc) + abs(ppplc) > FADE_THRESH / 4;
  105.       i = ( i + 1 ) % maxsamples )
  106.     {
  107.     lc = 0;
  108.     for ( j = 0; j < numdelays; ++j )
  109.         lc = lc + delaybuf[( i + maxsamples - samples[j] ) % maxsamples] * atten[j];
  110.     LINCLIP( lc );
  111.     delaybuf[i] = lc;
  112.     putchar( st_linear_to_ulaw( lc ) );
  113.     ppplc = pplc;
  114.     pplc = plc;
  115.     plc = lc;
  116.     }
  117.  
  118.     exit( 0 );
  119.     }
  120.