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.

DistrhoPlugin3BandEQ.cpp 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * DISTRHO 3BandEQ Plugin, based on 3BandEQ by Michael Gruhn
  3. * Copyright (C) 2007 Michael Gruhn <michael-gruhn@web.de>
  4. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * For a full copy of the license see the LICENSE file.
  16. */
  17. #include "DistrhoPlugin3BandEQ.hpp"
  18. #include <cmath>
  19. static const float kAMP_DB = 8.656170245f;
  20. static const float kDC_ADD = 1e-30f;
  21. static const float kPI = 3.141592654f;
  22. START_NAMESPACE_DISTRHO
  23. // -----------------------------------------------------------------------
  24. DistrhoPlugin3BandEQ::DistrhoPlugin3BandEQ()
  25. : Plugin(paramCount, 1, 0) // 1 program, 0 states
  26. {
  27. // set default values
  28. d_setProgram(0);
  29. // reset
  30. d_deactivate();
  31. }
  32. // -----------------------------------------------------------------------
  33. // Init
  34. void DistrhoPlugin3BandEQ::d_initParameter(uint32_t index, Parameter& parameter)
  35. {
  36. switch (index)
  37. {
  38. case paramLow:
  39. parameter.hints = kParameterIsAutomable;
  40. parameter.name = "Low";
  41. parameter.symbol = "low";
  42. parameter.unit = "dB";
  43. parameter.ranges.def = 0.0f;
  44. parameter.ranges.min = -24.0f;
  45. parameter.ranges.max = 24.0f;
  46. break;
  47. case paramMid:
  48. parameter.hints = kParameterIsAutomable;
  49. parameter.name = "Mid";
  50. parameter.symbol = "mid";
  51. parameter.unit = "dB";
  52. parameter.ranges.def = 0.0f;
  53. parameter.ranges.min = -24.0f;
  54. parameter.ranges.max = 24.0f;
  55. break;
  56. case paramHigh:
  57. parameter.hints = kParameterIsAutomable;
  58. parameter.name = "High";
  59. parameter.symbol = "high";
  60. parameter.unit = "dB";
  61. parameter.ranges.def = 0.0f;
  62. parameter.ranges.min = -24.0f;
  63. parameter.ranges.max = 24.0f;
  64. break;
  65. case paramMaster:
  66. parameter.hints = kParameterIsAutomable;
  67. parameter.name = "Master";
  68. parameter.symbol = "master";
  69. parameter.unit = "dB";
  70. parameter.ranges.def = 0.0f;
  71. parameter.ranges.min = -24.0f;
  72. parameter.ranges.max = 24.0f;
  73. break;
  74. case paramLowMidFreq:
  75. parameter.hints = kParameterIsAutomable;
  76. parameter.name = "Low-Mid Freq";
  77. parameter.symbol = "low_mid";
  78. parameter.unit = "Hz";
  79. parameter.ranges.def = 440.0f;
  80. parameter.ranges.min = 0.0f;
  81. parameter.ranges.max = 1000.0f;
  82. break;
  83. case paramMidHighFreq:
  84. parameter.hints = kParameterIsAutomable;
  85. parameter.name = "Mid-High Freq";
  86. parameter.symbol = "mid_high";
  87. parameter.unit = "Hz";
  88. parameter.ranges.def = 1000.0f;
  89. parameter.ranges.min = 1000.0f;
  90. parameter.ranges.max = 20000.0f;
  91. break;
  92. }
  93. }
  94. void DistrhoPlugin3BandEQ::d_initProgramName(uint32_t index, d_string& programName)
  95. {
  96. if (index != 0)
  97. return;
  98. programName = "Default";
  99. }
  100. // -----------------------------------------------------------------------
  101. // Internal data
  102. float DistrhoPlugin3BandEQ::d_getParameterValue(uint32_t index) const
  103. {
  104. switch (index)
  105. {
  106. case paramLow:
  107. return fLow;
  108. case paramMid:
  109. return fMid;
  110. case paramHigh:
  111. return fHigh;
  112. case paramMaster:
  113. return fMaster;
  114. case paramLowMidFreq:
  115. return fLowMidFreq;
  116. case paramMidHighFreq:
  117. return fMidHighFreq;
  118. default:
  119. return 0.0f;
  120. }
  121. }
  122. void DistrhoPlugin3BandEQ::d_setParameterValue(uint32_t index, float value)
  123. {
  124. if (d_getSampleRate() <= 0.0)
  125. return;
  126. switch (index)
  127. {
  128. case paramLow:
  129. fLow = value;
  130. lowVol = std::exp( (fLow/48.0f) * 48.0f / kAMP_DB);
  131. break;
  132. case paramMid:
  133. fMid = value;
  134. midVol = std::exp( (fMid/48.0f) * 48.0f / kAMP_DB);
  135. break;
  136. case paramHigh:
  137. fHigh = value;
  138. highVol = std::exp( (fHigh/48.0f) * 48.0f / kAMP_DB);
  139. break;
  140. case paramMaster:
  141. fMaster = value;
  142. outVol = std::exp( (fMaster/48.0f) * 48.0f / kAMP_DB);
  143. break;
  144. case paramLowMidFreq:
  145. fLowMidFreq = std::fmin(value, fMidHighFreq);
  146. freqLP = fLowMidFreq;
  147. xLP = std::exp(-2.0f * kPI * freqLP / (float)d_getSampleRate());
  148. a0LP = 1.0f - xLP;
  149. b1LP = -xLP;
  150. break;
  151. case paramMidHighFreq:
  152. fMidHighFreq = std::fmax(value, fLowMidFreq);
  153. freqHP = fMidHighFreq;
  154. xHP = std::exp(-2.0f * kPI * freqHP / (float)d_getSampleRate());
  155. a0HP = 1.0f - xHP;
  156. b1HP = -xHP;
  157. break;
  158. }
  159. }
  160. void DistrhoPlugin3BandEQ::d_setProgram(uint32_t index)
  161. {
  162. if (index != 0)
  163. return;
  164. // Default values
  165. fLow = 0.0f;
  166. fMid = 0.0f;
  167. fHigh = 0.0f;
  168. fMaster = 0.0f;
  169. fLowMidFreq = 220.0f;
  170. fMidHighFreq = 2000.0f;
  171. // Internal stuff
  172. lowVol = midVol = highVol = outVol = 1.0f;
  173. freqLP = 200.0f;
  174. freqHP = 2000.0f;
  175. // reset filter values
  176. d_activate();
  177. }
  178. // -----------------------------------------------------------------------
  179. // Process
  180. void DistrhoPlugin3BandEQ::d_activate()
  181. {
  182. const float sr = (float)d_getSampleRate();
  183. xLP = std::exp(-2.0f * kPI * freqLP / sr);
  184. #ifdef DISTRHO_OS_WINDOWS
  185. // don't ask me why, but this fixes a crash/exception below on windows...
  186. printf("%f\n", -xLP);
  187. #endif
  188. a0LP = 1.0f - xLP;
  189. b1LP = -xLP;
  190. xHP = std::exp(-2.0f * kPI * freqHP / sr);
  191. a0HP = 1.0f - xHP;
  192. b1HP = -xHP;
  193. }
  194. void DistrhoPlugin3BandEQ::d_deactivate()
  195. {
  196. out1LP = out2LP = out1HP = out2HP = 0.0f;
  197. tmp1LP = tmp2LP = tmp1HP = tmp2HP = 0.0f;
  198. }
  199. void DistrhoPlugin3BandEQ::d_run(const float** inputs, float** outputs, uint32_t frames)
  200. {
  201. const float* in1 = inputs[0];
  202. const float* in2 = inputs[1];
  203. float* out1 = outputs[0];
  204. float* out2 = outputs[1];
  205. for (uint32_t i=0; i < frames; ++i)
  206. {
  207. tmp1LP = a0LP * in1[i] - b1LP * tmp1LP + kDC_ADD;
  208. tmp2LP = a0LP * in2[i] - b1LP * tmp2LP + kDC_ADD;
  209. out1LP = tmp1LP - kDC_ADD;
  210. out2LP = tmp2LP - kDC_ADD;
  211. tmp1HP = a0HP * in1[i] - b1HP * tmp1HP + kDC_ADD;
  212. tmp2HP = a0HP * in2[i] - b1HP * tmp2HP + kDC_ADD;
  213. out1HP = in1[i] - tmp1HP - kDC_ADD;
  214. out2HP = in2[i] - tmp2HP - kDC_ADD;
  215. out1[i] = (out1LP*lowVol + (in1[i] - out1LP - out1HP)*midVol + out1HP*highVol) * outVol;
  216. out2[i] = (out2LP*lowVol + (in2[i] - out2LP - out2HP)*midVol + out2HP*highVol) * outVol;
  217. }
  218. }
  219. // -----------------------------------------------------------------------
  220. Plugin* createPlugin()
  221. {
  222. return new DistrhoPlugin3BandEQ();
  223. }
  224. // -----------------------------------------------------------------------
  225. END_NAMESPACE_DISTRHO