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.

269 lines
7.6KB

  1. /*
  2. * DISTRHO 3BandSplitter Plugin, based on 3BandSplitter 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 "DistrhoPlugin3BandSplitter.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. DistrhoPlugin3BandSplitter::DistrhoPlugin3BandSplitter()
  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. DistrhoPlugin3BandSplitter::~DistrhoPlugin3BandSplitter()
  33. {
  34. }
  35. // -------------------------------------------------
  36. // Init
  37. void DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::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 DistrhoPlugin3BandSplitter::d_deactivate()
  193. {
  194. out1LP = out2LP = out1HP = out2HP = 0.0f;
  195. tmp1LP = tmp2LP = tmp1HP = tmp2HP = 0.0f;
  196. }
  197. void DistrhoPlugin3BandSplitter::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. float* out3 = outputs[2];
  204. float* out4 = outputs[3];
  205. float* out5 = outputs[4];
  206. float* out6 = outputs[5];
  207. for (uint32_t i=0; i < frames; ++i)
  208. {
  209. tmp1LP = a0LP * in1[i] - b1LP * tmp1LP + cfDC_ADD;
  210. tmp2LP = a0LP * in2[i] - b1LP * tmp2LP + cfDC_ADD;
  211. out1LP = tmp1LP - cfDC_ADD;
  212. out2LP = tmp2LP - cfDC_ADD;
  213. tmp1HP = a0HP * in1[i] - b1HP * tmp1HP + cfDC_ADD;
  214. tmp2HP = a0HP * in2[i] - b1HP * tmp2HP + cfDC_ADD;
  215. out1HP = in1[i] - tmp1HP - cfDC_ADD;
  216. out2HP = in2[i] - tmp2HP - cfDC_ADD;
  217. out1[i] = out1LP*lowVol * outVol;
  218. out2[i] = out2LP*lowVol * outVol;
  219. out3[i] = (in1[i] - out1LP - out1HP)*midVol * outVol;
  220. out4[i] = (in2[i] - out2LP - out2HP)*midVol * outVol;
  221. out5[i] = out1HP*highVol * outVol;
  222. out6[i] = out2HP*highVol * outVol;
  223. }
  224. }
  225. // -------------------------------------------------
  226. Plugin* createPlugin()
  227. {
  228. return new DistrhoPlugin3BandSplitter();
  229. }
  230. // -------------------------------------------------
  231. END_NAMESPACE_DISTRHO