home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / windows3 / mtlabsnd.zip / VIBRO.C < prev    next >
Text File  |  1993-04-26  |  3KB  |  136 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 Vibro effect file.
  13.  *
  14.  * Modeled on world-famous Fender(TM) Amp Vibro knobs.
  15.  * 
  16.  * Algorithm: generate a sine wave ranging from
  17.  * 0 + depth to 1.0, where signal goes from -1.0 to 1.0.
  18.  * Multiply signal with sine wave.  I think.
  19.  */
  20.  
  21. #include <math.h>
  22. #include "st.h"
  23.  
  24. /* Private data for Vibro effect */
  25. typedef struct vibrostuff {
  26.     float         speed;
  27.     float         depth;
  28.     short        *sinetab;        /* sine wave to apply */
  29.     int        mult;            /* multiplier */
  30.     unsigned    length;            /* length of table */
  31.     int        counter;        /* current counter */
  32. } *vibro_t;
  33.  
  34. /*
  35.  * Process options
  36.  */
  37. vibro_getopts(effp, n, argv) 
  38. eff_t effp;
  39. int n;
  40. char **argv;
  41. {
  42.     vibro_t vibro = (vibro_t) effp->priv;
  43.  
  44.     vibro->depth = 0.5;
  45.     if ((n == 0) || !sscanf(argv[0], "%f", &vibro->speed) ||
  46.         ((n == 2) && !sscanf(argv[1], "%f", &vibro->depth)))
  47.         fail("Usage: vibro speed [ depth ]");
  48.     if ((vibro->speed <= 0.001) || (vibro->speed > 30.0) || 
  49.             (vibro->depth < 0.0) || (vibro->depth > 1.0))
  50.         fail("Vibro: speed must be < 30.0, 0.0 < depth < 1.0");
  51. }
  52.  
  53. /*
  54.  * Prepare processing.
  55.  */
  56. vibro_start(effp)
  57. eff_t effp;
  58. {
  59.     vibro_t vibro = (vibro_t) effp->priv;
  60.  
  61.     vibro->length = effp->ininfo.rate / vibro->speed;
  62.     if (! (vibro->sinetab = (short*) malloc(vibro->length * sizeof(short))))
  63.         fail("Vibro: Cannot malloc %d bytes",
  64.             vibro->length * sizeof(short));
  65.  
  66.     sine(vibro->sinetab, vibro->length, vibro->depth);
  67.     vibro->counter = 0;
  68. }
  69.  
  70. /*
  71.  * Processed signed long samples from ibuf to obuf.
  72.  * Return number of samples processed.
  73.  */
  74.  
  75. vibro_flow(effp, ibuf, obuf, isamp, osamp)
  76. eff_t effp;
  77. long *ibuf, *obuf;
  78. int *isamp, *osamp;
  79. {
  80.     vibro_t vibro = (vibro_t) effp->priv;
  81.     register counter, tablen;
  82.     int len, done;
  83.     register long mult;
  84.     register short *sinetab;
  85.     long l;
  86.  
  87.     len = ((*isamp > *osamp) ? *osamp : *isamp);
  88.  
  89.     sinetab = vibro->sinetab;
  90.     counter = vibro->counter;
  91.     tablen = vibro->length;
  92.     for(done = 0; done < len; done++) {
  93.         l = *ibuf++;
  94.         /* 24x8 gives 32-bit result */
  95.         *obuf++ = ((l / 256) * sinetab[counter++ % tablen]);
  96.     }
  97.     vibro->counter = counter;
  98.     /* processed all samples */
  99. }
  100.  
  101. /*
  102.  * Do anything required when you stop reading samples.  
  103.  * Don't close input file! 
  104.  */
  105. vibro_stop(effp)
  106. eff_t effp;
  107. {
  108.     /* nothing to do */
  109. }
  110.  
  111. /* This was very painful.  We need a sine library. */
  112.  
  113. sine(buf, len, depth)
  114. short *buf;
  115. int len;
  116. float depth;
  117. {
  118.     int i;
  119.     int scale = depth * 128;
  120.     int base = (1.0 - depth) * 128;
  121.     double val;
  122.  
  123.     for (i = 0; i < len; i++) {
  124.         val = sin((float)i/(float)len * 2.0 * M_PI);
  125.         buf[i] = (val + 1.0) * scale + base * 2;
  126.     }
  127. /*
  128.     for (i = 0; i < len; i++)
  129.         fprintf(stderr, "%d\n", buf[i]);
  130. */
  131. }
  132.  
  133.  
  134.  
  135.  
  136.