Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EQ.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. EQ.cpp - EQ effect
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License
  8. as published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License (version 2 or later) for more details.
  13. You should have received a copy of the GNU General Public License (version 2)
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <cmath>
  18. #include "EQ.h"
  19. #include "../DSP/AnalogFilter.h"
  20. #include "../Misc/Allocator.h"
  21. EQ::EQ(EffectParams pars)
  22. :Effect(pars)
  23. {
  24. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  25. filter[i].Ptype = 0;
  26. filter[i].Pfreq = 64;
  27. filter[i].Pgain = 64;
  28. filter[i].Pq = 64;
  29. filter[i].Pstages = 0;
  30. filter[i].l = memory.alloc<AnalogFilter>(6, 1000.0f, 1.0f, 0, pars.srate, pars.bufsize);
  31. filter[i].r = memory.alloc<AnalogFilter>(6, 1000.0f, 1.0f, 0, pars.srate, pars.bufsize);
  32. }
  33. //default values
  34. Pvolume = 50;
  35. setpreset(Ppreset);
  36. cleanup();
  37. }
  38. EQ::~EQ()
  39. {
  40. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  41. memory.dealloc(filter[i].l);
  42. memory.dealloc(filter[i].r);
  43. }
  44. }
  45. // Cleanup the effect
  46. void EQ::cleanup(void)
  47. {
  48. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  49. filter[i].l->cleanup();
  50. filter[i].r->cleanup();
  51. }
  52. }
  53. //Effect output
  54. void EQ::out(const Stereo<float *> &smp)
  55. {
  56. for(int i = 0; i < buffersize; ++i) {
  57. efxoutl[i] = smp.l[i] * volume;
  58. efxoutr[i] = smp.r[i] * volume;
  59. }
  60. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  61. if(filter[i].Ptype == 0)
  62. continue;
  63. filter[i].l->filterout(efxoutl);
  64. filter[i].r->filterout(efxoutr);
  65. }
  66. }
  67. //Parameter control
  68. void EQ::setvolume(unsigned char _Pvolume)
  69. {
  70. Pvolume = _Pvolume;
  71. outvolume = powf(0.005f, (1.0f - Pvolume / 127.0f)) * 10.0f;
  72. volume = (!insertion) ? 1.0f : outvolume;
  73. }
  74. void EQ::setpreset(unsigned char npreset)
  75. {
  76. const int PRESET_SIZE = 1;
  77. const int NUM_PRESETS = 2;
  78. unsigned char presets[NUM_PRESETS][PRESET_SIZE] = {
  79. {67}, //EQ 1
  80. {67} //EQ 2
  81. };
  82. if(npreset >= NUM_PRESETS)
  83. npreset = NUM_PRESETS - 1;
  84. for(int n = 0; n < PRESET_SIZE; ++n)
  85. changepar(n, presets[npreset][n]);
  86. Ppreset = npreset;
  87. }
  88. void EQ::changepar(int npar, unsigned char value)
  89. {
  90. switch(npar) {
  91. case 0:
  92. setvolume(value);
  93. break;
  94. }
  95. if(npar < 10)
  96. return;
  97. int nb = (npar - 10) / 5; //number of the band (filter)
  98. if(nb >= MAX_EQ_BANDS)
  99. return;
  100. int bp = npar % 5; //band paramenter
  101. float tmp;
  102. switch(bp) {
  103. case 0:
  104. filter[nb].Ptype = value;
  105. if(value > 9)
  106. filter[nb].Ptype = 0; //has to be changed if more filters will be added
  107. if(filter[nb].Ptype != 0) {
  108. filter[nb].l->settype(value - 1);
  109. filter[nb].r->settype(value - 1);
  110. }
  111. break;
  112. case 1:
  113. filter[nb].Pfreq = value;
  114. tmp = 600.0f * powf(30.0f, (value - 64.0f) / 64.0f);
  115. filter[nb].l->setfreq(tmp);
  116. filter[nb].r->setfreq(tmp);
  117. break;
  118. case 2:
  119. filter[nb].Pgain = value;
  120. tmp = 30.0f * (value - 64.0f) / 64.0f;
  121. filter[nb].l->setgain(tmp);
  122. filter[nb].r->setgain(tmp);
  123. break;
  124. case 3:
  125. filter[nb].Pq = value;
  126. tmp = powf(30.0f, (value - 64.0f) / 64.0f);
  127. filter[nb].l->setq(tmp);
  128. filter[nb].r->setq(tmp);
  129. break;
  130. case 4:
  131. filter[nb].Pstages = value;
  132. if(value >= MAX_FILTER_STAGES)
  133. filter[nb].Pstages = MAX_FILTER_STAGES - 1;
  134. filter[nb].l->setstages(value);
  135. filter[nb].r->setstages(value);
  136. break;
  137. }
  138. }
  139. unsigned char EQ::getpar(int npar) const
  140. {
  141. switch(npar) {
  142. case 0:
  143. return Pvolume;
  144. break;
  145. }
  146. if(npar < 10)
  147. return 0;
  148. int nb = (npar - 10) / 5; //number of the band (filter)
  149. if(nb >= MAX_EQ_BANDS)
  150. return 0;
  151. int bp = npar % 5; //band paramenter
  152. switch(bp) {
  153. case 0:
  154. return filter[nb].Ptype;
  155. break;
  156. case 1:
  157. return filter[nb].Pfreq;
  158. break;
  159. case 2:
  160. return filter[nb].Pgain;
  161. break;
  162. case 3:
  163. return filter[nb].Pq;
  164. break;
  165. case 4:
  166. return filter[nb].Pstages;
  167. break;
  168. default: return 0; //in case of bogus parameter number
  169. }
  170. }
  171. float EQ::getfreqresponse(float freq)
  172. {
  173. float resp = 1.0f;
  174. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  175. if(filter[i].Ptype == 0)
  176. continue;
  177. resp *= filter[i].l->H(freq);
  178. }
  179. return rap2dB(resp * outvolume);
  180. }
  181. //Not exactly the most efficient manner to derive the total taps, but it should
  182. //be fast enough in practice
  183. void EQ::getFilter(float *a, float *b) const
  184. {
  185. a[0] = 1;
  186. b[0] = 1;
  187. off_t off=0;
  188. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  189. auto &F = filter[i];
  190. if(F.Ptype == 0)
  191. continue;
  192. const double Fb[3] = {F.l->coeff.c[0], F.l->coeff.c[1], F.l->coeff.c[2]};
  193. const double Fa[3] = {1.0f, -F.l->coeff.d[1], -F.l->coeff.d[2]};
  194. for(int j=0; j<F.Pstages+1; ++j) {
  195. for(int k=0; k<3; ++k) {
  196. a[off] = Fa[k];
  197. b[off] = Fb[k];
  198. off++;
  199. }
  200. }
  201. }
  202. }