home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / windows3 / mtlabsnd.zip / LOWP.C < prev    next >
Text File  |  1993-04-26  |  2KB  |  106 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 Low-Pass effect file.
  13.  *
  14.  * Algorithm:  1nd order filter.
  15.  * From Fugue source code:
  16.  *
  17.  *     output[N] = input[N] * A + input[N-1] * B
  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 Lowpass effect */
  27. typedef struct lowpstuff {
  28.     float    center;
  29.     double    A, B;
  30.     double    in1;
  31. } *lowp_t;
  32.  
  33. /*
  34.  * Process options
  35.  */
  36. lowp_getopts(effp, n, argv) 
  37. eff_t effp;
  38. int n;
  39. char **argv;
  40. {
  41.     lowp_t lowp = (lowp_t) effp->priv;
  42.  
  43.     if ((n < 1) || !sscanf(argv[0], "%f", &lowp->center))
  44.         fail("Usage: lowp center");
  45. }
  46.  
  47. /*
  48.  * Prepare processing.
  49.  */
  50. lowp_start(effp)
  51. eff_t effp;
  52. {
  53.     lowp_t lowp = (lowp_t) effp->priv;
  54.     if (lowp->center > effp->ininfo.rate*2)
  55.         fail("Lowpass: center must be < minimum data rate*2\n");
  56.  
  57.     lowp->A = (M_PI * 2.0 * lowp->center) / effp->ininfo.rate;
  58.     lowp->B = exp(-lowp->A / effp->ininfo.rate);
  59.     lowp->in1 = 0.0;
  60. }
  61.  
  62. /*
  63.  * Processed signed long samples from ibuf to obuf.
  64.  * Return number of samples processed.
  65.  */
  66.  
  67. lowp_flow(effp, ibuf, obuf, isamp, osamp)
  68. eff_t effp;
  69. long *ibuf, *obuf;
  70. int *isamp, *osamp;
  71. {
  72.     lowp_t lowp = (lowp_t) effp->priv;
  73.     register counter, tablen;
  74.     int len, done;
  75.     register long mult;
  76.     register short *sinetab;
  77.     double d, d1;
  78.     long l;
  79.  
  80.     len = ((*isamp > *osamp) ? *osamp : *isamp);
  81.  
  82.     /* yeah yeah yeah registers & integer arithmetic yeah yeah yeah */
  83.     for(done = 0; done < len; done++) {
  84.         l = *ibuf++;
  85.         d = lowp->A * (l / 65536) + lowp->B * (lowp->in1 / 65536);
  86.         d *= 0.8;
  87.         if (d > 32767)
  88.             d = 32767;
  89.         if (d < - 32767)
  90.             d = - 32767;
  91.         lowp->in1 = l;
  92.         *obuf++ = d * 65536;
  93.     }
  94. }
  95.  
  96. /*
  97.  * Do anything required when you stop reading samples.  
  98.  * Don't close input file! 
  99.  */
  100. lowp_stop(effp)
  101. eff_t effp;
  102. {
  103.     /* nothing to do */
  104. }
  105.  
  106.