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.

290 lines
8.3KB

  1. /*
  2. * DISTRHO AmplitudeImposer, a DPF'ied AmplitudeImposer.
  3. * Copyright (C) 2004 Niall Moody
  4. * Copyright (C) 2015-2022 Filipe Coelho <falktx@falktx.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #include "DistrhoPluginAmplitudeImposer.hpp"
  25. START_NAMESPACE_DISTRHO
  26. // -----------------------------------------------------------------------
  27. struct twofloats {
  28. float left;
  29. float right;
  30. };
  31. // -----------------------------------------------------------------------
  32. DistrhoPluginAmplitudeImposer::DistrhoPluginAmplitudeImposer()
  33. : Plugin(kParameterCount, 1, 0), // 1 program, 0 states
  34. fDepth(1.0f),
  35. fThreshold(0.5f),
  36. ampEnvelope_l(0.0f),
  37. ampEnvelope_r(0.0f),
  38. audioEnvelope_l(0.0f),
  39. audioEnvelope_r(0.0f),
  40. envDecay(0.0001f) {}
  41. // -----------------------------------------------------------------------
  42. // Init
  43. void DistrhoPluginAmplitudeImposer::initAudioPort(bool input, uint32_t index, AudioPort& port)
  44. {
  45. port.hints = 0x0;
  46. if (input)
  47. {
  48. switch (index)
  49. {
  50. case 0:
  51. port.name = "Input Left (Audio)";
  52. port.symbol = "in_left_audio";
  53. port.groupId = kPortGroupStereo;
  54. break;
  55. case 1:
  56. port.name = "Input Right (Audio)";
  57. port.symbol = "in_right_audio";
  58. port.groupId = kPortGroupStereo;
  59. break;
  60. case 2:
  61. port.name = "Input Left (Amp Env)";
  62. port.symbol = "in_left_amp";
  63. port.groupId = kPortGroupAmpEnv;
  64. port.hints = kAudioPortIsSidechain;
  65. break;
  66. case 3:
  67. port.name = "Input Right (Amp Env)";
  68. port.symbol = "in_right_amp";
  69. port.groupId = kPortGroupAmpEnv;
  70. port.hints = kAudioPortIsSidechain;
  71. break;
  72. }
  73. }
  74. else
  75. {
  76. switch (index)
  77. {
  78. case 0:
  79. port.name = "Output Left";
  80. port.symbol = "out_left";
  81. break;
  82. case 1:
  83. port.name = "Output Right";
  84. port.symbol = "out_right";
  85. break;
  86. }
  87. port.groupId = kPortGroupStereo;
  88. }
  89. }
  90. void DistrhoPluginAmplitudeImposer::initParameter(uint32_t index, Parameter& parameter)
  91. {
  92. parameter.hints = kParameterIsAutomatable;
  93. parameter.ranges.min = 0.0f;
  94. parameter.ranges.max = 1.0f;
  95. switch (index)
  96. {
  97. case kParameterDepth:
  98. parameter.name = "Depth";
  99. parameter.symbol = "depth";
  100. parameter.ranges.def = 1.0f;
  101. break;
  102. case kParameterThreshold:
  103. parameter.name = "Thres";
  104. parameter.symbol = "thres";
  105. parameter.ranges.def = 0.5f;
  106. break;
  107. }
  108. }
  109. void DistrhoPluginAmplitudeImposer::initPortGroup(uint32_t groupId, PortGroup& portGroup)
  110. {
  111. switch (groupId)
  112. {
  113. case kPortGroupAmpEnv:
  114. portGroup.name = "Amp Env";
  115. portGroup.symbol = "amp_env";
  116. break;
  117. }
  118. }
  119. void DistrhoPluginAmplitudeImposer::initProgramName(uint32_t index, String& programName)
  120. {
  121. if (index != 0)
  122. return;
  123. programName = "Default";
  124. }
  125. // -----------------------------------------------------------------------
  126. // Internal data
  127. float DistrhoPluginAmplitudeImposer::getParameterValue(uint32_t index) const
  128. {
  129. switch(index)
  130. {
  131. case kParameterDepth:
  132. return fDepth;
  133. case kParameterThreshold:
  134. return fThreshold;
  135. default:
  136. return 0.0f;
  137. }
  138. }
  139. void DistrhoPluginAmplitudeImposer::setParameterValue(uint32_t index, float value)
  140. {
  141. switch(index)
  142. {
  143. case kParameterDepth:
  144. fDepth = value;
  145. break;
  146. case kParameterThreshold:
  147. fThreshold = value;
  148. break;
  149. }
  150. }
  151. void DistrhoPluginAmplitudeImposer::loadProgram(uint32_t index)
  152. {
  153. if (index != 0)
  154. return;
  155. fDepth = 1.0f;
  156. fThreshold = 0.5f;
  157. }
  158. // -----------------------------------------------------------------------
  159. // Process
  160. void DistrhoPluginAmplitudeImposer::activate()
  161. {
  162. ampEnvelope_l = 0.0f;
  163. ampEnvelope_r = 0.0f;
  164. audioEnvelope_l = 0.0f;
  165. audioEnvelope_r = 0.0f;
  166. envDecay = 0.0001f;
  167. }
  168. void DistrhoPluginAmplitudeImposer::run(const float** inputs, float** outputs, uint32_t frames)
  169. {
  170. const float* const in1 = inputs[0];
  171. const float* const in2 = inputs[1];
  172. const float* const env1 = inputs[2];
  173. const float* const env2 = inputs[3];
  174. /* */ float* const out1 = outputs[0];
  175. /* */ float* const out2 = outputs[1];
  176. float tmp;
  177. twofloats tempf;
  178. twofloats tempin;
  179. for (uint32_t i=0; i<frames; ++i)
  180. {
  181. // calculate envelope from 1st two inputs
  182. tmp = std::abs(env1[i]);
  183. /**/ if (tmp > ampEnvelope_l)
  184. ampEnvelope_l = tmp;
  185. else if (tmp < ampEnvelope_l)
  186. ampEnvelope_l -= envDecay;
  187. tmp = std::abs(env2[i]);
  188. /**/ if (tmp > ampEnvelope_r)
  189. ampEnvelope_r = tmp;
  190. else if (tmp < ampEnvelope_r)
  191. ampEnvelope_r -= envDecay;
  192. // calculate envelope from 2nd two inputs
  193. tmp = std::abs(in1[i]);
  194. /**/ if (tmp > audioEnvelope_l)
  195. audioEnvelope_l = tmp;
  196. else if (tmp < audioEnvelope_l)
  197. audioEnvelope_l -= envDecay;
  198. tmp = std::abs(in2[i]);
  199. /**/ if (tmp > audioEnvelope_r)
  200. audioEnvelope_r = tmp;
  201. else if (tmp < audioEnvelope_r)
  202. audioEnvelope_r -= envDecay;
  203. // make sure we're not multiplying by a negative number
  204. if (ampEnvelope_l < 0.0f)
  205. ampEnvelope_l = 0.0f;
  206. if (ampEnvelope_r < 0.0f)
  207. ampEnvelope_r = 0.0f;
  208. if (audioEnvelope_l < 0.0f)
  209. audioEnvelope_l = 0.0f;
  210. if (audioEnvelope_r < 0.0f)
  211. audioEnvelope_r = 0.0f;
  212. // work out whether we need to multiply audio input
  213. if (audioEnvelope_l > fThreshold)
  214. {
  215. tempin.left = in1[i];
  216. }
  217. else
  218. {
  219. if (audioEnvelope_l > 0.001f)
  220. tempin.left = in1[i] * (fThreshold/audioEnvelope_l);
  221. else
  222. tempin.left = in1[i] * (fThreshold/0.001f); //so it'll decay away smoothly
  223. }
  224. if (audioEnvelope_r > fThreshold)
  225. {
  226. tempin.right = in2[i];
  227. }
  228. else
  229. {
  230. if (audioEnvelope_r > 0.001f)
  231. tempin.right = in2[i] * (fThreshold/audioEnvelope_r);
  232. else
  233. tempin.right = in2[i] * (fThreshold/0.001f);
  234. }
  235. // calculate output
  236. tempf.left = tempin.left * ampEnvelope_l;
  237. tempf.left *= fDepth;
  238. tempf.left = tempf.left + ((1.0f-fDepth)*tempin.left);
  239. tempf.right = tempin.right * ampEnvelope_r;
  240. tempf.right *= fDepth;
  241. tempf.right = tempf.right + ((1.0f-fDepth)*tempin.right);
  242. out1[i] = tempf.left;
  243. out2[i] = tempf.right;
  244. }
  245. }
  246. // -----------------------------------------------------------------------
  247. Plugin* createPlugin()
  248. {
  249. return new DistrhoPluginAmplitudeImposer();
  250. }
  251. // -----------------------------------------------------------------------
  252. END_NAMESPACE_DISTRHO