DISTRHO Juice Plugins
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.

385 lines
11KB

  1. /*
  2. * Power Juice Plugin
  3. * Copyright (C) 2014 Andre Sklenar <andre.sklenar@gmail.com>, www.juicelab.cz
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  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 General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "PowerJuicePlugin.hpp"
  18. //#include <cstring>
  19. #include <cstdlib>
  20. START_NAMESPACE_DISTRHO
  21. // -----------------------------------------------------------------------
  22. PowerJuicePlugin::PowerJuicePlugin()
  23. : Plugin(paramCount, 1, 0) // 1 program, 0 states
  24. {
  25. std::memset(&RMSStack, 0, sizeof(RMSStack));
  26. std::memset(&lookaheadStack, 0, sizeof(lookaheadStack));
  27. // set default values
  28. loadProgram(0);
  29. // reset
  30. deactivate();
  31. }
  32. PowerJuicePlugin::~PowerJuicePlugin()
  33. {
  34. std::free(RMSStack.data);
  35. std::free(lookaheadStack.data);
  36. }
  37. // -----------------------------------------------------------------------
  38. // Init
  39. void PowerJuicePlugin::initParameter(uint32_t index, Parameter& parameter)
  40. {
  41. switch (index)
  42. {
  43. case paramAttack:
  44. parameter.hints = kParameterIsAutomatable;
  45. parameter.name = "Attack";
  46. parameter.symbol = "att";
  47. parameter.unit = "ms";
  48. parameter.ranges.def = 20.0f;
  49. parameter.ranges.min = 0.1f;
  50. parameter.ranges.max = 1000.0f;
  51. break;
  52. case paramRelease:
  53. parameter.hints = kParameterIsAutomatable;
  54. parameter.name = "Release";
  55. parameter.symbol = "rel";
  56. parameter.unit = "ms";
  57. parameter.ranges.def = 200.0f;
  58. parameter.ranges.min = 0.1f;
  59. parameter.ranges.max = 1000.0f;
  60. break;
  61. case paramThreshold:
  62. parameter.hints = kParameterIsAutomatable;
  63. parameter.name = "Threshold";
  64. parameter.symbol = "thr";
  65. parameter.unit = "dB";
  66. parameter.ranges.def = 0.0f;
  67. parameter.ranges.min = -60.0f;
  68. parameter.ranges.max = 0.0f;
  69. break;
  70. case paramRatio:
  71. parameter.hints = kParameterIsAutomatable;
  72. parameter.name = "Ratio";
  73. parameter.symbol = "rat";
  74. parameter.unit = "";
  75. parameter.ranges.def = 1.0f;
  76. parameter.ranges.min = 1.0f;
  77. parameter.ranges.max = 10.0f;
  78. break;
  79. case paramMakeup:
  80. parameter.hints = kParameterIsAutomatable;
  81. parameter.name = "Make-Up";
  82. parameter.symbol = "mak";
  83. parameter.unit = "";
  84. parameter.ranges.def = 0.0f;
  85. parameter.ranges.min = 0.0f;
  86. parameter.ranges.max = 20.0f;
  87. break;
  88. case paramMix:
  89. parameter.hints = kParameterIsAutomatable;
  90. parameter.name = "Mix";
  91. parameter.symbol = "Mix";
  92. parameter.unit = "";
  93. parameter.ranges.def = 1.0f;
  94. parameter.ranges.min = 0.0f;
  95. parameter.ranges.max = 1.0f;
  96. break;
  97. }
  98. }
  99. void PowerJuicePlugin::initProgramName(uint32_t index, String& programName)
  100. {
  101. if (index != 0)
  102. return;
  103. programName = "Default";
  104. }
  105. // -----------------------------------------------------------------------
  106. // Internal data
  107. float PowerJuicePlugin::getParameterValue(uint32_t index) const
  108. {
  109. switch (index)
  110. {
  111. case paramAttack:
  112. return attack;
  113. case paramRelease:
  114. return release;
  115. case paramThreshold:
  116. return threshold;
  117. case paramRatio:
  118. return ratio;
  119. case paramMakeup:
  120. return makeup;
  121. case paramMix:
  122. return mix;
  123. default:
  124. return 0.0f;
  125. }
  126. }
  127. void PowerJuicePlugin::setParameterValue(uint32_t index, float value)
  128. {
  129. switch (index)
  130. {
  131. case paramAttack:
  132. attack = value;
  133. attackSamples = getSampleRate()*(attack/1000.0f);
  134. break;
  135. case paramRelease:
  136. release = value;
  137. releaseSamples = getSampleRate()*(release/1000.0f);
  138. break;
  139. case paramThreshold:
  140. threshold = value;
  141. break;
  142. case paramRatio:
  143. ratio = value;
  144. break;
  145. case paramMakeup:
  146. makeup = value;
  147. makeupFloat = fromDB(makeup);
  148. break;
  149. case paramMix:
  150. mix = value;
  151. break;
  152. }
  153. }
  154. void PowerJuicePlugin::loadProgram(uint32_t index)
  155. {
  156. if (index != 0)
  157. return;
  158. /* Default parameter values */
  159. attack = 20.0f;
  160. release = 200.0f;
  161. threshold = 0.0f;
  162. ratio = 1.0f;
  163. makeup = 0.0f;
  164. mix = 1.0f;
  165. makeupFloat = fromDB(makeup);
  166. attackSamples = getSampleRate()*(attack/1000.0f);
  167. releaseSamples = getSampleRate()*(release/1000.0f);
  168. w = 563; //waveform plane size, size of the plane in pixels;
  169. w2 = 1126; //wavefowm array
  170. h = 121; //waveform plane height
  171. x = 27; //waveform plane positions
  172. y = 53;
  173. dc = 113; //0DC line y position
  174. /* Default variable values */
  175. averageCounter = 0;
  176. inputMax = 0.0f;
  177. balancer = 1.0f;
  178. GR = 1.0f;
  179. newRepaint = false;
  180. input.start = 0;
  181. rms.start = 0;
  182. gainReduction.start = 0;
  183. RMSStack.start = 0;
  184. lookaheadStack.start = 0;
  185. repaintSkip = 0;
  186. kFloatRMSStackCount = 400.0f/44100.0f*getSampleRate();
  187. std::free(RMSStack.data);
  188. RMSStack.data = (float*) calloc(kFloatRMSStackCount, sizeof(float));
  189. kFloatLookaheadStackCount = 800.0f/44100.0f*getSampleRate();
  190. std::free(lookaheadStack.data);
  191. lookaheadStack.data = (float*) calloc(kFloatLookaheadStackCount, sizeof(float));
  192. refreshSkip= 300.0f/44100.0f*getSampleRate();
  193. std::memset(rms.data, 0, sizeof(float)*kFloatStackCount);
  194. std::memset(gainReduction.data, 0, sizeof(float)*kFloatStackCount);
  195. std::memset(RMSStack.data, 0, sizeof(float)*kFloatRMSStackCount);
  196. std::memset(lookaheadStack.data, 0, sizeof(float)*kFloatLookaheadStackCount);
  197. for (int j=0; j < kFloatStackCount; ++j)
  198. history.rms[j] = h +y;
  199. for (int j=0; j < kFloatStackCount; ++j)
  200. history.gainReduction[j] = h +y;
  201. activate();
  202. }
  203. float PowerJuicePlugin::getRMSHistory(int n) {
  204. return history.rms[n];
  205. }
  206. bool PowerJuicePlugin::repaintNeeded() {
  207. return newRepaint;
  208. }
  209. float PowerJuicePlugin::getGainReductionHistory(int n) {
  210. if (n == kFloatStackCount-1) {
  211. newRepaint = false;
  212. //printf("falsing!\n");
  213. }
  214. return history.gainReduction[n];
  215. }
  216. // -----------------------------------------------------------------------
  217. // Process
  218. void PowerJuicePlugin::activate()
  219. {
  220. }
  221. void PowerJuicePlugin::deactivate()
  222. {
  223. // all values to zero
  224. }
  225. void PowerJuicePlugin::run(const float** inputs, float** outputs, uint32_t frames)
  226. {
  227. const float* in = inputs[0];
  228. float* out = outputs[0];
  229. float sum;
  230. float data;
  231. float difference;
  232. for (uint32_t i=0; i < frames; i++) {
  233. sum = 0.0f;
  234. data = 0.0f;
  235. difference = 0;
  236. //sanitizeDenormal(in[i]); // FIXME - you cannot modify inputs
  237. /* compute last RMS */
  238. //store audio samples in an RMS buffer line
  239. RMSStack.data[RMSStack.start++] = in[i];
  240. if (RMSStack.start == kFloatRMSStackCount)
  241. RMSStack.start = 0;
  242. //compute RMS over last kFloatRMSStackCount samples
  243. for (int j=0; j < kFloatRMSStackCount; ++j) {
  244. data = RMSStack.data[(RMSStack.start+j) % kFloatRMSStackCount];
  245. sum += data * data;
  246. }
  247. //root mean SQUARE
  248. float RMS = sqrt(sum / kFloatRMSStackCount);
  249. sanitizeDenormal(RMS);
  250. /* compute gain reduction if needed */
  251. float RMSDB = toDB(RMS);
  252. if (RMSDB>threshold) {
  253. //attack stage
  254. float difference = (RMSDB-threshold);
  255. //sanitizeDenormal(difference);
  256. targetGR = difference - difference/ratio;
  257. if (targetGR>difference/(ratio/4.0f)) {
  258. targetGR = difference - difference/(ratio*1.5f);
  259. //more power!
  260. }
  261. //
  262. if (GR<targetGR) {
  263. //approach targetGR at attackSamples rate
  264. GR -= (GR-targetGR)/(attackSamples);
  265. } else {
  266. //approach targetGR at releaseSamples rate
  267. GR -= (GR-targetGR)/releaseSamples;
  268. }
  269. sanitizeDenormal(GR);
  270. } else {
  271. //release stage
  272. //approach targetGR at releaseSamples rate, targetGR = 0.0f
  273. GR -= GR/releaseSamples;
  274. }
  275. //store audio in lookahead buffer
  276. lookaheadStack.data[lookaheadStack.start++] = in[i];
  277. //printf("rms\n");
  278. if (lookaheadStack.start == kFloatLookaheadStackCount)
  279. lookaheadStack.start = 0;
  280. if (++averageCounter >= refreshSkip) {
  281. //add relevant values to the shared memory
  282. rms.data[rms.start++] = RMSDB;
  283. gainReduction.data[gainReduction.start++] = GR;
  284. //rewind stack reading heads if needed
  285. if (rms.start == kFloatStackCount)
  286. rms.start = 0;
  287. if (gainReduction.start == kFloatStackCount)
  288. gainReduction.start = 0;
  289. //saving in gfx format, for speed
  290. //share memory
  291. for (int j=0; j < kFloatStackCount; ++j)
  292. history.rms[j] = -toIEC(rms.data[(rms.start+j) % kFloatStackCount])/200*h +h +y;
  293. for (int j=0; j < kFloatStackCount; ++j) {
  294. history.gainReduction[j] = -toIEC(-gainReduction.data[(gainReduction.start+j) % kFloatStackCount])/200*h +h +y;
  295. }
  296. repaintSkip++;
  297. if (repaintSkip>5) {
  298. repaintSkip = 0;
  299. newRepaint = true;
  300. }
  301. averageCounter = 0;
  302. inputMax = 0.0f;
  303. }
  304. /* compress, mix, done. */
  305. float compressedSignal = in[i]*fromDB(-GR);
  306. out[i] = (compressedSignal*makeupFloat*mix)+in[i]*(1-mix);
  307. }
  308. }
  309. // -----------------------------------------------------------------------
  310. Plugin* createPlugin()
  311. {
  312. return new PowerJuicePlugin();
  313. }
  314. // -----------------------------------------------------------------------
  315. END_NAMESPACE_DISTRHO