Collection of DPF-based plugins for packaging
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.

267 lines
7.4KB

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