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

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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-2013 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 LGPL.txt file
  16. */
  17. #include "DistrhoPlugin3BandEQ.hpp"
  18. #include <cmath>
  19. static const float cfAMP_DB = 8.656170245f;
  20. static const float cfDC_ADD = 1e-30f;
  21. static const float cfPI = 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. DistrhoPlugin3BandEQ::~DistrhoPlugin3BandEQ()
  33. {
  34. }
  35. // -------------------------------------------------
  36. // Init
  37. void DistrhoPlugin3BandEQ::d_initParameter(uint32_t index, Parameter& parameter)
  38. {
  39. switch (index)
  40. {
  41. case paramLow:
  42. parameter.hints = PARAMETER_IS_AUTOMABLE;
  43. parameter.name = "Low";
  44. parameter.symbol = "low";
  45. parameter.unit = "dB";
  46. parameter.ranges.def = 0.0f;
  47. parameter.ranges.min = -24.0f;
  48. parameter.ranges.max = 24.0f;
  49. break;
  50. case paramMid:
  51. parameter.hints = PARAMETER_IS_AUTOMABLE;
  52. parameter.name = "Mid";
  53. parameter.symbol = "mid";
  54. parameter.unit = "dB";
  55. parameter.ranges.def = 0.0f;
  56. parameter.ranges.min = -24.0f;
  57. parameter.ranges.max = 24.0f;
  58. break;
  59. case paramHigh:
  60. parameter.hints = PARAMETER_IS_AUTOMABLE;
  61. parameter.name = "High";
  62. parameter.symbol = "high";
  63. parameter.unit = "dB";
  64. parameter.ranges.def = 0.0f;
  65. parameter.ranges.min = -24.0f;
  66. parameter.ranges.max = 24.0f;
  67. break;
  68. case paramMaster:
  69. parameter.hints = PARAMETER_IS_AUTOMABLE;
  70. parameter.name = "Master";
  71. parameter.symbol = "master";
  72. parameter.unit = "dB";
  73. parameter.ranges.def = 0.0f;
  74. parameter.ranges.min = -24.0f;
  75. parameter.ranges.max = 24.0f;
  76. break;
  77. case paramLowMidFreq:
  78. parameter.hints = PARAMETER_IS_AUTOMABLE;
  79. parameter.name = "Low-Mid Freq";
  80. parameter.symbol = "low_mid";
  81. parameter.unit = "Hz";
  82. parameter.ranges.def = 440.0f;
  83. parameter.ranges.min = 0.0f;
  84. parameter.ranges.max = 1000.0f;
  85. break;
  86. case paramMidHighFreq:
  87. parameter.hints = PARAMETER_IS_AUTOMABLE;
  88. parameter.name = "Mid-High Freq";
  89. parameter.symbol = "mid_high";
  90. parameter.unit = "Hz";
  91. parameter.ranges.def = 1000.0f;
  92. parameter.ranges.min = 1000.0f;
  93. parameter.ranges.max = 20000.0f;
  94. break;
  95. }
  96. }
  97. void DistrhoPlugin3BandEQ::d_initProgramName(uint32_t index, d_string& programName)
  98. {
  99. if (index != 0)
  100. return;
  101. programName = "Default";
  102. }
  103. // -------------------------------------------------
  104. // Internal data
  105. float DistrhoPlugin3BandEQ::d_parameterValue(uint32_t index)
  106. {
  107. switch (index)
  108. {
  109. case paramLow:
  110. return fLow;
  111. case paramMid:
  112. return fMid;
  113. case paramHigh:
  114. return fHigh;
  115. case paramMaster:
  116. return fMaster;
  117. case paramLowMidFreq:
  118. return fLowMidFreq;
  119. case paramMidHighFreq:
  120. return fMidHighFreq;
  121. default:
  122. return 0.0f;
  123. }
  124. }
  125. void DistrhoPlugin3BandEQ::d_setParameterValue(uint32_t index, float value)
  126. {
  127. if (d_sampleRate() <= 0.0)
  128. return;
  129. switch (index)
  130. {
  131. case paramLow:
  132. fLow = value;
  133. lowVol = std::exp( (fLow/48.0f) * 48 / cfAMP_DB);
  134. break;
  135. case paramMid:
  136. fMid = value;
  137. midVol = std::exp( (fMid/48.0f) * 48 / cfAMP_DB);
  138. break;
  139. case paramHigh:
  140. fHigh = value;
  141. highVol = std::exp( (fHigh/48.0f) * 48 / cfAMP_DB);
  142. break;
  143. case paramMaster:
  144. fMaster = value;
  145. outVol = std::exp( (fMaster/48.0f) * 48 / cfAMP_DB);
  146. break;
  147. case paramLowMidFreq:
  148. fLowMidFreq = std::fmin(value, fMidHighFreq);
  149. freqLP = fLowMidFreq; //fLowMidFreq * (fLowMidFreq / 24000.0f) * (fLowMidFreq / 24000.0f);
  150. xLP = std::exp(-2.0f * cfPI * freqLP / (float)d_sampleRate());
  151. a0LP = 1.0f - xLP;
  152. b1LP = -xLP;
  153. break;
  154. case paramMidHighFreq:
  155. fMidHighFreq = std::fmax(value, fLowMidFreq);
  156. freqHP = fMidHighFreq; //fMidHighFreq * (fMidHighFreq / 24000.0f) * (fMidHighFreq / 24000.0f);
  157. xHP = std::exp(-2.0f * cfPI * freqHP / (float)d_sampleRate());
  158. a0HP = 1.0f - xHP;
  159. b1HP = -xHP;
  160. break;
  161. }
  162. }
  163. void DistrhoPlugin3BandEQ::d_setProgram(uint32_t index)
  164. {
  165. if (index != 0)
  166. return;
  167. // Default values
  168. fLow = 0.0f;
  169. fMid = 0.0f;
  170. fHigh = 0.0f;
  171. fMaster = 0.0f;
  172. fLowMidFreq = 220.0f;
  173. fMidHighFreq = 2000.0f;
  174. // Internal stuff
  175. lowVol = midVol = highVol = outVol = 1.0f;
  176. freqLP = 200.0f;
  177. freqHP = 2000.0f;
  178. // reset filter values
  179. d_activate();
  180. }
  181. // -------------------------------------------------
  182. // Process
  183. void DistrhoPlugin3BandEQ::d_activate()
  184. {
  185. xLP = std::exp(-2.0f * cfPI * freqLP / (float)d_sampleRate());
  186. a0LP = 1.0f - xLP;
  187. b1LP = -xLP;
  188. xHP = std::exp(-2.0f * cfPI * freqHP / (float)d_sampleRate());
  189. a0HP = 1.0f - xHP;
  190. b1HP = -xHP;
  191. }
  192. void DistrhoPlugin3BandEQ::d_deactivate()
  193. {
  194. out1LP = out2LP = out1HP = out2HP = 0.0f;
  195. tmp1LP = tmp2LP = tmp1HP = tmp2HP = 0.0f;
  196. }
  197. void DistrhoPlugin3BandEQ::d_run(float** inputs, float** outputs, uint32_t frames, uint32_t, const MidiEvent*)
  198. {
  199. float* in1 = inputs[0];
  200. float* in2 = inputs[1];
  201. float* out1 = outputs[0];
  202. float* out2 = outputs[1];
  203. for (uint32_t i=0; i < frames; ++i)
  204. {
  205. tmp1LP = a0LP * in1[i] - b1LP * tmp1LP + cfDC_ADD;
  206. tmp2LP = a0LP * in2[i] - b1LP * tmp2LP + cfDC_ADD;
  207. out1LP = tmp1LP - cfDC_ADD;
  208. out2LP = tmp2LP - cfDC_ADD;
  209. tmp1HP = a0HP * in1[i] - b1HP * tmp1HP + cfDC_ADD;
  210. tmp2HP = a0HP * in2[i] - b1HP * tmp2HP + cfDC_ADD;
  211. out1HP = in1[i] - tmp1HP - cfDC_ADD;
  212. out2HP = in2[i] - tmp2HP - cfDC_ADD;
  213. out1[i] = (out1LP*lowVol + (in1[i] - out1LP - out1HP)*midVol + out1HP*highVol) * outVol;
  214. out2[i] = (out2LP*lowVol + (in2[i] - out2LP - out2HP)*midVol + out2HP*highVol) * outVol;
  215. }
  216. }
  217. // -------------------------------------------------
  218. Plugin* createPlugin()
  219. {
  220. return new DistrhoPlugin3BandEQ();
  221. }
  222. // -------------------------------------------------
  223. END_NAMESPACE_DISTRHO