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.

296 lines
8.5KB

  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 (Amp Env)";
  52. port.symbol = "in_left_amp";
  53. port.groupId = kPortGroupAmpEnv;
  54. // FIXME VST3 sidechain handling
  55. // port.hints = kAudioPortIsSidechain;
  56. break;
  57. case 1:
  58. port.name = "Input Right (Amp Env)";
  59. port.symbol = "in_right_amp";
  60. port.groupId = kPortGroupAmpEnv;
  61. // FIXME VST3 sidechain handling
  62. // port.hints = kAudioPortIsSidechain;
  63. break;
  64. case 2:
  65. port.name = "Input Left (Audio)";
  66. port.symbol = "in_left_audio";
  67. port.groupId = kPortGroupAudio;
  68. break;
  69. case 3:
  70. port.name = "Input Right (Audio)";
  71. port.symbol = "in_right_audio";
  72. port.groupId = kPortGroupAudio;
  73. break;
  74. }
  75. }
  76. else
  77. {
  78. switch (index)
  79. {
  80. case 0:
  81. port.name = "Output Left";
  82. port.symbol = "out_left";
  83. break;
  84. case 1:
  85. port.name = "Output Right";
  86. port.symbol = "out_right";
  87. break;
  88. }
  89. port.groupId = kPortGroupStereo;
  90. }
  91. }
  92. void DistrhoPluginAmplitudeImposer::initParameter(uint32_t index, Parameter& parameter)
  93. {
  94. parameter.hints = kParameterIsAutomatable;
  95. parameter.ranges.min = 0.0f;
  96. parameter.ranges.max = 1.0f;
  97. switch (index)
  98. {
  99. case kParameterDepth:
  100. parameter.name = "Depth";
  101. parameter.symbol = "depth";
  102. parameter.ranges.def = 1.0f;
  103. break;
  104. case kParameterThreshold:
  105. parameter.name = "Thres";
  106. parameter.symbol = "thres";
  107. parameter.ranges.def = 0.5f;
  108. break;
  109. }
  110. }
  111. void DistrhoPluginAmplitudeImposer::initPortGroup(uint32_t groupId, PortGroup& portGroup)
  112. {
  113. switch (groupId)
  114. {
  115. case kPortGroupAmpEnv:
  116. portGroup.name = "Amp Env";
  117. portGroup.symbol = "amp_env";
  118. break;
  119. case kPortGroupAudio:
  120. portGroup.name = "Audio";
  121. portGroup.symbol = "audio";
  122. break;
  123. }
  124. }
  125. void DistrhoPluginAmplitudeImposer::initProgramName(uint32_t index, String& programName)
  126. {
  127. if (index != 0)
  128. return;
  129. programName = "Default";
  130. }
  131. // -----------------------------------------------------------------------
  132. // Internal data
  133. float DistrhoPluginAmplitudeImposer::getParameterValue(uint32_t index) const
  134. {
  135. switch(index)
  136. {
  137. case kParameterDepth:
  138. return fDepth;
  139. case kParameterThreshold:
  140. return fThreshold;
  141. default:
  142. return 0.0f;
  143. }
  144. }
  145. void DistrhoPluginAmplitudeImposer::setParameterValue(uint32_t index, float value)
  146. {
  147. switch(index)
  148. {
  149. case kParameterDepth:
  150. fDepth = value;
  151. break;
  152. case kParameterThreshold:
  153. fThreshold = value;
  154. break;
  155. }
  156. }
  157. void DistrhoPluginAmplitudeImposer::loadProgram(uint32_t index)
  158. {
  159. if (index != 0)
  160. return;
  161. fDepth = 1.0f;
  162. fThreshold = 0.5f;
  163. }
  164. // -----------------------------------------------------------------------
  165. // Process
  166. void DistrhoPluginAmplitudeImposer::activate()
  167. {
  168. ampEnvelope_l = 0.0f;
  169. ampEnvelope_r = 0.0f;
  170. audioEnvelope_l = 0.0f;
  171. audioEnvelope_r = 0.0f;
  172. envDecay = 0.0001f;
  173. }
  174. void DistrhoPluginAmplitudeImposer::run(const float** inputs, float** outputs, uint32_t frames)
  175. {
  176. const float* const in1 = inputs[0];
  177. const float* const in2 = inputs[1];
  178. const float* const in3 = inputs[2];
  179. const float* const in4 = inputs[3];
  180. /* */ float* const out1 = outputs[0];
  181. /* */ float* const out2 = outputs[1];
  182. float tmp;
  183. twofloats tempf;
  184. twofloats tempin;
  185. for (uint32_t i=0; i<frames; ++i)
  186. {
  187. // calculate envelope from 1st two inputs
  188. tmp = std::abs(in1[i]);
  189. /**/ if (tmp > ampEnvelope_l)
  190. ampEnvelope_l = tmp;
  191. else if (tmp < ampEnvelope_l)
  192. ampEnvelope_l -= envDecay;
  193. tmp = std::abs(in2[i]);
  194. /**/ if (tmp > ampEnvelope_r)
  195. ampEnvelope_r = tmp;
  196. else if (tmp < ampEnvelope_r)
  197. ampEnvelope_r -= envDecay;
  198. // calculate envelope from 2nd two inputs
  199. tmp = std::abs(in3[i]);
  200. /**/ if (tmp > audioEnvelope_l)
  201. audioEnvelope_l = tmp;
  202. else if (tmp < audioEnvelope_l)
  203. audioEnvelope_l -= envDecay;
  204. tmp = std::abs(in4[i]);
  205. /**/ if (tmp > audioEnvelope_r)
  206. audioEnvelope_r = tmp;
  207. else if (tmp < audioEnvelope_r)
  208. audioEnvelope_r -= envDecay;
  209. // make sure we're not multiplying by a negative number
  210. if (ampEnvelope_l < 0.0f)
  211. ampEnvelope_l = 0.0f;
  212. if (ampEnvelope_r < 0.0f)
  213. ampEnvelope_r = 0.0f;
  214. if (audioEnvelope_l < 0.0f)
  215. audioEnvelope_l = 0.0f;
  216. if (audioEnvelope_r < 0.0f)
  217. audioEnvelope_r = 0.0f;
  218. // work out whether we need to multiply audio input
  219. if (audioEnvelope_l > fThreshold)
  220. {
  221. tempin.left = in3[i];
  222. }
  223. else
  224. {
  225. if (audioEnvelope_l > 0.001f)
  226. tempin.left = in3[i] * (fThreshold/audioEnvelope_l);
  227. else
  228. tempin.left = in3[i] * (fThreshold/0.001f); //so it'll decay away smoothly
  229. }
  230. if (audioEnvelope_r > fThreshold)
  231. {
  232. tempin.right = in4[i];
  233. }
  234. else
  235. {
  236. if (audioEnvelope_r > 0.001f)
  237. tempin.right = in4[i] * (fThreshold/audioEnvelope_r);
  238. else
  239. tempin.right = in4[i] * (fThreshold/0.001f);
  240. }
  241. // calculate output
  242. tempf.left = tempin.left * ampEnvelope_l;
  243. tempf.left *= fDepth;
  244. tempf.left = tempf.left + ((1.0f-fDepth)*tempin.left);
  245. tempf.right = tempin.right * ampEnvelope_r;
  246. tempf.right *= fDepth;
  247. tempf.right = tempf.right + ((1.0f-fDepth)*tempin.right);
  248. out1[i] = tempf.left;
  249. out2[i] = tempf.right;
  250. }
  251. }
  252. // -----------------------------------------------------------------------
  253. Plugin* createPlugin()
  254. {
  255. return new DistrhoPluginAmplitudeImposer();
  256. }
  257. // -----------------------------------------------------------------------
  258. END_NAMESPACE_DISTRHO