Guest User

Untitled

a guest
Apr 18th, 2012
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2. //
  3. // 3 Band EQ :)
  4. //
  5. // EQ.H - Header file for 3 band EQ
  6. //
  7. // (c) Neil C / Etanza Systems / 2K6
  8. //
  9. // Shouts / Loves / Moans = etanza at lycos dot co dot uk
  10. //
  11. // This work is hereby placed in the public domain for all purposes, including
  12. // use in commercial applications.
  13. //
  14. // The author assumes NO RESPONSIBILITY for any problems caused by the use of
  15. // this software.
  16. //
  17. //----------------------------------------------------------------------------
  18.  
  19. #ifndef __EQ3BAND__
  20. #define __EQ3BAND__
  21.  
  22.  
  23. // ------------
  24. //| Structures |
  25. // ------------
  26.  
  27. typedef struct
  28. {
  29. // Filter #1 (Low band)
  30.  
  31. double lf; // Frequency
  32. double f1p0; // Poles ...
  33. double f1p1;
  34. double f1p2;
  35. double f1p3;
  36.  
  37. // Filter #2 (High band)
  38.  
  39. double hf; // Frequency
  40. double f2p0; // Poles ...
  41. double f2p1;
  42. double f2p2;
  43. double f2p3;
  44.  
  45. // Sample history buffer
  46.  
  47. double sdm1; // Sample data minus 1
  48. double sdm2; // 2
  49. double sdm3; // 3
  50.  
  51. // Gain Controls
  52.  
  53. double lg; // low gain
  54. double mg; // mid gain
  55. double hg; // high gain
  56.  
  57. } EQSTATE;
  58.  
  59.  
  60. // ---------
  61. //| Exports |
  62. // ---------
  63.  
  64. extern void init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq);
  65. extern double do_3band(EQSTATE* es, double sample);
  66.  
  67.  
  68. #endif // #ifndef __EQ3BAND__
  69. //---------------------------------------------------------------------------
  70.  
  71. Now the source ...
  72. //----------------------------------------------------------------------------
  73. //
  74. // 3 Band EQ :)
  75. //
  76. // EQ.C - Main Source file for 3 band EQ
  77. //
  78. // (c) Neil C / Etanza Systems / 2K6
  79. //
  80. // Shouts / Loves / Moans = etanza at lycos dot co dot uk
  81. //
  82. // This work is hereby placed in the public domain for all purposes, including
  83. // use in commercial applications.
  84. //
  85. // The author assumes NO RESPONSIBILITY for any problems caused by the use of
  86. // this software.
  87. //
  88. //----------------------------------------------------------------------------
  89.  
  90. // NOTES :
  91. //
  92. // - Original filter code by Paul Kellet (musicdsp.pdf)
  93. //
  94. // - Uses 4 first order filters in series, should give 24dB per octave
  95. //
  96. // - Now with P4 Denormal fix :)
  97.  
  98.  
  99. //----------------------------------------------------------------------------
  100.  
  101. // ----------
  102. //| Includes |
  103. // ----------
  104.  
  105. #include <math.h>
  106. #include "eq.h"
  107.  
  108.  
  109. // -----------
  110. //| Constants |
  111. // -----------
  112.  
  113. static double vsa = (1.0 / 4294967295.0); // Very small amount (Denormal Fix)
  114.  
  115.  
  116. // ---------------
  117. //| Initialise EQ |
  118. // ---------------
  119.  
  120. // Recommended frequencies are ...
  121. //
  122. // lowfreq = 880 Hz
  123. // highfreq = 5000 Hz
  124. //
  125. // Set mixfreq to whatever rate your system is using (eg 48Khz)
  126.  
  127. void init_3band_state(EQSTATE* es, int lowfreq, int highfreq, int mixfreq)
  128. {
  129. // Clear state
  130.  
  131. memset(es,0,sizeof(EQSTATE));
  132.  
  133. // Set Low/Mid/High gains to unity
  134.  
  135. es->lg = 1.0;
  136. es->mg = 1.0;
  137. es->hg = 1.0;
  138.  
  139. // Calculate filter cutoff frequencies
  140.  
  141. es->lf = 2 * sin(M_PI * ((double)lowfreq / (double)mixfreq));
  142. es->hf = 2 * sin(M_PI * ((double)highfreq / (double)mixfreq));
  143. }
  144.  
  145.  
  146. // ---------------
  147. //| EQ one sample |
  148. // ---------------
  149.  
  150. // - sample can be any range you like :)
  151. //
  152. // Note that the output will depend on the gain settings for each band
  153. // (especially the bass) so may require clipping before output, but you
  154. // knew that anyway :)
  155.  
  156. double do_3band(EQSTATE* es, double sample)
  157. {
  158. // Locals
  159.  
  160. double l,m,h; // Low / Mid / High - Sample Values
  161.  
  162. // Filter #1 (lowpass)
  163.  
  164. es->f1p0 += (es->lf * (sample - es->f1p0)) + vsa;
  165. es->f1p1 += (es->lf * (es->f1p0 - es->f1p1));
  166. es->f1p2 += (es->lf * (es->f1p1 - es->f1p2));
  167. es->f1p3 += (es->lf * (es->f1p2 - es->f1p3));
  168.  
  169. l = es->f1p3;
  170.  
  171. // Filter #2 (highpass)
  172.  
  173. es->f2p0 += (es->hf * (sample - es->f2p0)) + vsa;
  174. es->f2p1 += (es->hf * (es->f2p0 - es->f2p1));
  175. es->f2p2 += (es->hf * (es->f2p1 - es->f2p2));
  176. es->f2p3 += (es->hf * (es->f2p2 - es->f2p3));
  177.  
  178. h = es->sdm3 - es->f2p3;
  179.  
  180. // Calculate midrange (signal - (low + high))
  181.  
  182. m = es->sdm3 - (h + l);
  183.  
  184. // Scale, Combine and store
  185.  
  186. l *= es->lg;
  187. m *= es->mg;
  188. h *= es->hg;
  189.  
  190. // Shuffle history buffer
  191.  
  192. es->sdm3 = es->sdm2;
  193. es->sdm2 = es->sdm1;
  194. es->sdm1 = sample;
  195.  
  196. // Return result
  197.  
  198. return(l + m + h);
  199. }
  200.  
  201.  
  202. //----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment