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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. EQ::EQ(bool insertion_, float *efxoutl_, float *efxoutr_, unsigned int srate, int bufsize)
  21. :Effect(insertion_, efxoutl_, efxoutr_, NULL, 0, srate, bufsize)
  22. {
  23. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  24. filter[i].Ptype = 0;
  25. filter[i].Pfreq = 64;
  26. filter[i].Pgain = 64;
  27. filter[i].Pq = 64;
  28. filter[i].Pstages = 0;
  29. filter[i].l = new AnalogFilter(6, 1000.0f, 1.0f, 0, srate, bufsize);
  30. filter[i].r = new AnalogFilter(6, 1000.0f, 1.0f, 0, srate, bufsize);
  31. }
  32. //default values
  33. Pvolume = 50;
  34. setpreset(Ppreset);
  35. cleanup();
  36. }
  37. // Cleanup the effect
  38. void EQ::cleanup(void)
  39. {
  40. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  41. filter[i].l->cleanup();
  42. filter[i].r->cleanup();
  43. }
  44. }
  45. //Effect output
  46. void EQ::out(const Stereo<float *> &smp)
  47. {
  48. for(int i = 0; i < buffersize; ++i) {
  49. efxoutl[i] = smp.l[i] * volume;
  50. efxoutr[i] = smp.r[i] * volume;
  51. }
  52. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  53. if(filter[i].Ptype == 0)
  54. continue;
  55. filter[i].l->filterout(efxoutl);
  56. filter[i].r->filterout(efxoutr);
  57. }
  58. }
  59. //Parameter control
  60. void EQ::setvolume(unsigned char _Pvolume)
  61. {
  62. Pvolume = _Pvolume;
  63. outvolume = powf(0.005f, (1.0f - Pvolume / 127.0f)) * 10.0f;
  64. volume = (!insertion) ? 1.0f : outvolume;
  65. }
  66. void EQ::setpreset(unsigned char npreset)
  67. {
  68. const int PRESET_SIZE = 1;
  69. const int NUM_PRESETS = 2;
  70. unsigned char presets[NUM_PRESETS][PRESET_SIZE] = {
  71. {67}, //EQ 1
  72. {67} //EQ 2
  73. };
  74. if(npreset >= NUM_PRESETS)
  75. npreset = NUM_PRESETS - 1;
  76. for(int n = 0; n < PRESET_SIZE; ++n)
  77. changepar(n, presets[npreset][n]);
  78. Ppreset = npreset;
  79. }
  80. void EQ::changepar(int npar, unsigned char value)
  81. {
  82. switch(npar) {
  83. case 0:
  84. setvolume(value);
  85. break;
  86. }
  87. if(npar < 10)
  88. return;
  89. int nb = (npar - 10) / 5; //number of the band (filter)
  90. if(nb >= MAX_EQ_BANDS)
  91. return;
  92. int bp = npar % 5; //band paramenter
  93. float tmp;
  94. switch(bp) {
  95. case 0:
  96. filter[nb].Ptype = value;
  97. if(value > 9)
  98. filter[nb].Ptype = 0; //has to be changed if more filters will be added
  99. if(filter[nb].Ptype != 0) {
  100. filter[nb].l->settype(value - 1);
  101. filter[nb].r->settype(value - 1);
  102. }
  103. break;
  104. case 1:
  105. filter[nb].Pfreq = value;
  106. tmp = 600.0f * powf(30.0f, (value - 64.0f) / 64.0f);
  107. filter[nb].l->setfreq(tmp);
  108. filter[nb].r->setfreq(tmp);
  109. break;
  110. case 2:
  111. filter[nb].Pgain = value;
  112. tmp = 30.0f * (value - 64.0f) / 64.0f;
  113. filter[nb].l->setgain(tmp);
  114. filter[nb].r->setgain(tmp);
  115. break;
  116. case 3:
  117. filter[nb].Pq = value;
  118. tmp = powf(30.0f, (value - 64.0f) / 64.0f);
  119. filter[nb].l->setq(tmp);
  120. filter[nb].r->setq(tmp);
  121. break;
  122. case 4:
  123. filter[nb].Pstages = value;
  124. if(value >= MAX_FILTER_STAGES)
  125. filter[nb].Pstages = MAX_FILTER_STAGES - 1;
  126. filter[nb].l->setstages(value);
  127. filter[nb].r->setstages(value);
  128. break;
  129. }
  130. }
  131. unsigned char EQ::getpar(int npar) const
  132. {
  133. switch(npar) {
  134. case 0:
  135. return Pvolume;
  136. break;
  137. }
  138. if(npar < 10)
  139. return 0;
  140. int nb = (npar - 10) / 5; //number of the band (filter)
  141. if(nb >= MAX_EQ_BANDS)
  142. return 0;
  143. int bp = npar % 5; //band paramenter
  144. switch(bp) {
  145. case 0:
  146. return filter[nb].Ptype;
  147. break;
  148. case 1:
  149. return filter[nb].Pfreq;
  150. break;
  151. case 2:
  152. return filter[nb].Pgain;
  153. break;
  154. case 3:
  155. return filter[nb].Pq;
  156. break;
  157. case 4:
  158. return filter[nb].Pstages;
  159. break;
  160. default: return 0; //in case of bogus parameter number
  161. }
  162. }
  163. float EQ::getfreqresponse(float freq)
  164. {
  165. float resp = 1.0f;
  166. for(int i = 0; i < MAX_EQ_BANDS; ++i) {
  167. if(filter[i].Ptype == 0)
  168. continue;
  169. resp *= filter[i].l->H(freq);
  170. }
  171. return rap2dB(resp * outvolume);
  172. }