home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Sound / SoX / Source / highp.c < prev    next >
C/C++ Source or Header  |  1999-07-18  |  2KB  |  108 lines

  1.  
  2. /*
  3.  * July 5, 1991
  4.  * Copyright 1991 Lance Norskog And Sundry Contributors
  5.  * This source code is freely redistributable and may be used for
  6.  * any purpose.  This copyright notice must be maintained. 
  7.  * Lance Norskog And Sundry Contributors are not responsible for 
  8.  * the consequences of using this software.
  9.  */
  10.  
  11. /*
  12.  * Sound Tools High-Pass effect file.
  13.  *
  14.  * Algorithm:  1nd order filter.
  15.  * From Fugue source code:
  16.  *
  17.  *     output[N] = B * (output[N-1] - input[N-1] + input[N])
  18.  *
  19.  *     A = 2.0 * pi * center
  20.  *     B = exp(-A / frequency)
  21.  */
  22.  
  23. #include <math.h>
  24. #include "st.h"
  25.  
  26. /* Private data for Highpass effect */
  27. typedef struct highpstuff {
  28.     float    center;
  29.     double    A, B;
  30.     double    in1, out1;
  31. } *highp_t;
  32.  
  33. /*
  34.  * Process options
  35.  */
  36. void highp_getopts(effp, n, argv) 
  37. eff_t effp;
  38. int n;
  39. char **argv;
  40. {
  41.     highp_t highp = (highp_t) effp->priv;
  42.  
  43.     if ((n < 1) || !sscanf(argv[0], "%f", &highp->center))
  44.         fail("Usage: highp center");
  45. }
  46.  
  47. /*
  48.  * Prepare processing.
  49.  */
  50. void highp_start(effp)
  51. eff_t effp;
  52. {
  53.     highp_t highp = (highp_t) effp->priv;
  54.     if (highp->center > effp->ininfo.rate*2)
  55.         fail("Highpass: center must be < minimum data rate*2\n");
  56.     
  57.     highp->A = (M_PI * 2.0 * highp->center) / effp->ininfo.rate;
  58.     highp->B = exp(-highp->A / effp->ininfo.rate);
  59.     highp->in1 = 0.0;
  60.     highp->out1 = 0.0;
  61. }
  62.  
  63. /*
  64.  * Processed signed long samples from ibuf to obuf.
  65.  * Return number of samples processed.
  66.  */
  67.  
  68. void highp_flow(effp, ibuf, obuf, isamp, osamp)
  69. eff_t effp;
  70. LONG *ibuf, *obuf;
  71. int *isamp, *osamp;
  72. {
  73.     highp_t highp = (highp_t) effp->priv;
  74.     int len, done;
  75.     double d;
  76.     LONG l;
  77.  
  78.     len = ((*isamp > *osamp) ? *osamp : *isamp);
  79.     d = highp->out1;
  80.  
  81.     /* yeah yeah yeah registers & integer arithmetic yeah yeah yeah */
  82.     for(done = 0; done < len; done++) {
  83.         l = *ibuf++;
  84.         d = (highp->B * ((d - highp->in1) + (double) l)) / 65536.0;
  85.         d *= 0.8;
  86.         if (d > 32767)
  87.             d = 32767;
  88.         if (d < - 32767)
  89.             d = - 32767;
  90.         highp->in1 = l;
  91.         *obuf++ = d * 65536L;
  92.     }
  93.     highp->out1 = d;
  94.     *isamp = len;
  95.     *osamp = len;
  96. }
  97.  
  98. /*
  99.  * Do anything required when you stop reading samples.  
  100.  * Don't close input file! 
  101.  */
  102. void highp_stop(effp)
  103. eff_t effp;
  104. {
  105.     /* nothing to do */
  106. }
  107.  
  108.