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.

212 lines
5.7KB

  1. /*
  2. * DISTRHO StereoEnhancer Plugin, based on StereoEnhancer 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 doc/LGPL.txt file.
  16. */
  17. #include "DistrhoPluginStereoEnhancer.hpp"
  18. #include <cmath>
  19. static const float kDC_ADD = 1e-30f;
  20. static const float kPI = 3.141592654f;
  21. START_NAMESPACE_DISTRHO
  22. // -----------------------------------------------------------------------
  23. DistrhoPluginStereoEnhancer::DistrhoPluginStereoEnhancer()
  24. : Plugin(paramCount, 1, 0) // 1 program, 0 states
  25. {
  26. // set default values
  27. d_setProgram(0);
  28. // reset
  29. d_deactivate();
  30. }
  31. DistrhoPluginStereoEnhancer::~DistrhoPluginStereoEnhancer()
  32. {
  33. }
  34. // -----------------------------------------------------------------------
  35. // Init
  36. void DistrhoPluginStereoEnhancer::d_initParameter(uint32_t index, Parameter& parameter)
  37. {
  38. switch (index)
  39. {
  40. case paramWidthLows:
  41. parameter.hints = PARAMETER_IS_AUTOMABLE;
  42. parameter.name = "Width Lows";
  43. parameter.symbol = "width_lows";
  44. parameter.unit = "%";
  45. parameter.ranges.def = 0.0f;
  46. parameter.ranges.min = 100.0f;
  47. parameter.ranges.max = 200.0f;
  48. break;
  49. case paramWidthHighs:
  50. parameter.hints = PARAMETER_IS_AUTOMABLE;
  51. parameter.name = "WidthHighs";
  52. parameter.symbol = "width_highs";
  53. parameter.unit = "%";
  54. parameter.ranges.def = 0.0f;
  55. parameter.ranges.min = 100.0f;
  56. parameter.ranges.max = 200.0f;
  57. break;
  58. case paramCrossover:
  59. parameter.hints = PARAMETER_IS_AUTOMABLE;
  60. parameter.name = "Crossover";
  61. parameter.symbol = "crossover";
  62. parameter.unit = "%";
  63. parameter.ranges.def = 0.0f;
  64. parameter.ranges.min = 27.51604f;
  65. parameter.ranges.max = 100.0f;
  66. break;
  67. }
  68. }
  69. void DistrhoPluginStereoEnhancer::d_initProgramName(uint32_t index, d_string& programName)
  70. {
  71. if (index != 0)
  72. return;
  73. programName = "Default";
  74. }
  75. // -----------------------------------------------------------------------
  76. // Internal data
  77. float DistrhoPluginStereoEnhancer::d_getParameterValue(uint32_t index) const
  78. {
  79. switch (index)
  80. {
  81. case paramWidthLows:
  82. return widthLP;
  83. case paramWidthHighs:
  84. return widthHP;
  85. case paramCrossover:
  86. return freqHP;
  87. default:
  88. return 0.0f;
  89. }
  90. }
  91. void DistrhoPluginStereoEnhancer::d_setParameterValue(uint32_t index, float value)
  92. {
  93. if (d_getSampleRate() <= 0.0)
  94. return;
  95. switch (index)
  96. {
  97. case paramWidthLows:
  98. widthLP = value;
  99. widthCoeffLP = std::fmax(widthLP, 1.0f);
  100. break;
  101. case paramWidthHighs:
  102. widthHP = value;
  103. widthCoeffHP = std::fmax(widthHP, 1.0f);
  104. break;
  105. case paramCrossover:
  106. freqHPFader = value;
  107. freqHP = freqHPFader*freqHPFader*freqHPFader*24000.0f;
  108. xHP = std::exp(-2.0f * kPI * freqHP / (float)d_getSampleRate());
  109. a0HP = 1.0f-xHP;
  110. b1HP = -xHP;
  111. break;
  112. }
  113. }
  114. void DistrhoPluginStereoEnhancer::d_setProgram(uint32_t index)
  115. {
  116. if (index != 0)
  117. return;
  118. // Default values
  119. widthLP = 100.0f;
  120. widthHP = 100.0f;
  121. freqHPFader = 27.51604f;
  122. // Internal stuff
  123. widthCoeffLP = 1.0f;
  124. widthCoeffHP = 1.0f;
  125. freqHP = 500.0f;
  126. // reset filter values
  127. d_activate();
  128. }
  129. // -----------------------------------------------------------------------
  130. // Process
  131. void DistrhoPluginStereoEnhancer::d_activate()
  132. {
  133. xHP = std::exp(-2.0f * kPI * freqHP/ (float)d_getSampleRate());
  134. a0HP = 1.0f-xHP;
  135. b1HP = -xHP;
  136. }
  137. void DistrhoPluginStereoEnhancer::d_deactivate()
  138. {
  139. out1HP = out2HP = 0.0f;
  140. tmp1HP = tmp2HP = 0.0f;
  141. }
  142. void DistrhoPluginStereoEnhancer::d_run(float** inputs, float** outputs, uint32_t frames)
  143. {
  144. float* in1 = inputs[0];
  145. float* in2 = inputs[1];
  146. float* out1 = outputs[0];
  147. float* out2 = outputs[1];
  148. for (uint32_t i=0; i < frames; ++i)
  149. {
  150. out1HP = in1[i];
  151. out2HP = in2[i];
  152. in1[i] = (tmp1HP = a0HP * in1[i] - b1HP * tmp1HP + kDC_ADD);
  153. in2[i] = (tmp2HP = a0HP * in2[i] - b1HP * tmp2HP + kDC_ADD);
  154. out1HP -= in1[i] - kDC_ADD;
  155. out2HP -= in2[i] - kDC_ADD;
  156. monoLP = (in1[i] + in2[i]) / 2.0f;
  157. stereoLP = in1[i] - in2[i];
  158. (*in1) = (monoLP + stereoLP * widthLP) / widthCoeffLP;
  159. (*in2) = (monoLP - stereoLP * widthLP) / widthCoeffLP;
  160. monoHP = (out1HP + out2HP) / 2.0f;
  161. stereoHP = out1HP - out2HP;
  162. out1HP = (monoHP + stereoHP * widthHP) / widthCoeffHP;
  163. out2HP = (monoHP - stereoHP * widthHP) / widthCoeffHP;
  164. out1[i] = in1[i] + out1HP;
  165. out2[i] = in2[i] + out2HP;
  166. }
  167. }
  168. // -----------------------------------------------------------------------
  169. Plugin* createPlugin()
  170. {
  171. return new DistrhoPluginStereoEnhancer();
  172. }
  173. // -----------------------------------------------------------------------
  174. END_NAMESPACE_DISTRHO