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.

294 lines
8.4KB

  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. port.hints = kAudioPortIsSidechain;
  55. break;
  56. case 1:
  57. port.name = "Input Right (Amp Env)";
  58. port.symbol = "in_right_amp";
  59. port.groupId = kPortGroupAmpEnv;
  60. port.hints = kAudioPortIsSidechain;
  61. break;
  62. case 2:
  63. port.name = "Input Left (Audio)";
  64. port.symbol = "in_left_audio";
  65. port.groupId = kPortGroupAudio;
  66. break;
  67. case 3:
  68. port.name = "Input Right (Audio)";
  69. port.symbol = "in_right_audio";
  70. port.groupId = kPortGroupAudio;
  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. case kPortGroupAudio:
  118. portGroup.name = "Audio";
  119. portGroup.symbol = "audio";
  120. break;
  121. }
  122. }
  123. void DistrhoPluginAmplitudeImposer::initProgramName(uint32_t index, String& programName)
  124. {
  125. if (index != 0)
  126. return;
  127. programName = "Default";
  128. }
  129. // -----------------------------------------------------------------------
  130. // Internal data
  131. float DistrhoPluginAmplitudeImposer::getParameterValue(uint32_t index) const
  132. {
  133. switch(index)
  134. {
  135. case kParameterDepth:
  136. return fDepth;
  137. case kParameterThreshold:
  138. return fThreshold;
  139. default:
  140. return 0.0f;
  141. }
  142. }
  143. void DistrhoPluginAmplitudeImposer::setParameterValue(uint32_t index, float value)
  144. {
  145. switch(index)
  146. {
  147. case kParameterDepth:
  148. fDepth = value;
  149. break;
  150. case kParameterThreshold:
  151. fThreshold = value;
  152. break;
  153. }
  154. }
  155. void DistrhoPluginAmplitudeImposer::loadProgram(uint32_t index)
  156. {
  157. if (index != 0)
  158. return;
  159. fDepth = 1.0f;
  160. fThreshold = 0.5f;
  161. }
  162. // -----------------------------------------------------------------------
  163. // Process
  164. void DistrhoPluginAmplitudeImposer::activate()
  165. {
  166. ampEnvelope_l = 0.0f;
  167. ampEnvelope_r = 0.0f;
  168. audioEnvelope_l = 0.0f;
  169. audioEnvelope_r = 0.0f;
  170. envDecay = 0.0001f;
  171. }
  172. void DistrhoPluginAmplitudeImposer::run(const float** inputs, float** outputs, uint32_t frames)
  173. {
  174. const float* const in1 = inputs[0];
  175. const float* const in2 = inputs[1];
  176. const float* const in3 = inputs[2];
  177. const float* const in4 = inputs[3];
  178. /* */ float* const out1 = outputs[0];
  179. /* */ float* const out2 = outputs[1];
  180. float tmp;
  181. twofloats tempf;
  182. twofloats tempin;
  183. for (uint32_t i=0; i<frames; ++i)
  184. {
  185. // calculate envelope from 1st two inputs
  186. tmp = std::abs(in1[i]);
  187. /**/ if (tmp > ampEnvelope_l)
  188. ampEnvelope_l = tmp;
  189. else if (tmp < ampEnvelope_l)
  190. ampEnvelope_l -= envDecay;
  191. tmp = std::abs(in2[i]);
  192. /**/ if (tmp > ampEnvelope_r)
  193. ampEnvelope_r = tmp;
  194. else if (tmp < ampEnvelope_r)
  195. ampEnvelope_r -= envDecay;
  196. // calculate envelope from 2nd two inputs
  197. tmp = std::abs(in3[i]);
  198. /**/ if (tmp > audioEnvelope_l)
  199. audioEnvelope_l = tmp;
  200. else if (tmp < audioEnvelope_l)
  201. audioEnvelope_l -= envDecay;
  202. tmp = std::abs(in4[i]);
  203. /**/ if (tmp > audioEnvelope_r)
  204. audioEnvelope_r = tmp;
  205. else if (tmp < audioEnvelope_r)
  206. audioEnvelope_r -= envDecay;
  207. // make sure we're not multiplying by a negative number
  208. if (ampEnvelope_l < 0.0f)
  209. ampEnvelope_l = 0.0f;
  210. if (ampEnvelope_r < 0.0f)
  211. ampEnvelope_r = 0.0f;
  212. if (audioEnvelope_l < 0.0f)
  213. audioEnvelope_l = 0.0f;
  214. if (audioEnvelope_r < 0.0f)
  215. audioEnvelope_r = 0.0f;
  216. // work out whether we need to multiply audio input
  217. if (audioEnvelope_l > fThreshold)
  218. {
  219. tempin.left = in3[i];
  220. }
  221. else
  222. {
  223. if (audioEnvelope_l > 0.001f)
  224. tempin.left = in3[i] * (fThreshold/audioEnvelope_l);
  225. else
  226. tempin.left = in3[i] * (fThreshold/0.001f); //so it'll decay away smoothly
  227. }
  228. if (audioEnvelope_r > fThreshold)
  229. {
  230. tempin.right = in4[i];
  231. }
  232. else
  233. {
  234. if (audioEnvelope_r > 0.001f)
  235. tempin.right = in4[i] * (fThreshold/audioEnvelope_r);
  236. else
  237. tempin.right = in4[i] * (fThreshold/0.001f);
  238. }
  239. // calculate output
  240. tempf.left = tempin.left * ampEnvelope_l;
  241. tempf.left *= fDepth;
  242. tempf.left = tempf.left + ((1.0f-fDepth)*tempin.left);
  243. tempf.right = tempin.right * ampEnvelope_r;
  244. tempf.right *= fDepth;
  245. tempf.right = tempf.right + ((1.0f-fDepth)*tempin.right);
  246. out1[i] = tempf.left;
  247. out2[i] = tempf.right;
  248. }
  249. }
  250. // -----------------------------------------------------------------------
  251. Plugin* createPlugin()
  252. {
  253. return new DistrhoPluginAmplitudeImposer();
  254. }
  255. // -----------------------------------------------------------------------
  256. END_NAMESPACE_DISTRHO