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.

309 lines
7.6KB

  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. // set default values
  26. d_setProgram(0);
  27. // init shm vars
  28. carla_shm_init(shm);
  29. shmData = nullptr;
  30. // reset
  31. d_deactivate();
  32. }
  33. PowerJuicePlugin::~PowerJuicePlugin()
  34. {
  35. closeShm();
  36. }
  37. // -----------------------------------------------------------------------
  38. // Init
  39. void PowerJuicePlugin::d_initParameter(uint32_t index, Parameter& parameter)
  40. {
  41. switch (index)
  42. {
  43. case paramAttack:
  44. parameter.hints = PARAMETER_IS_AUTOMABLE;
  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 = PARAMETER_IS_AUTOMABLE;
  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 = PARAMETER_IS_AUTOMABLE;
  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 = PARAMETER_IS_AUTOMABLE;
  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 = PARAMETER_IS_AUTOMABLE;
  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 = PARAMETER_IS_AUTOMABLE;
  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::d_initProgramName(uint32_t index, d_string& programName)
  100. {
  101. if (index != 0)
  102. return;
  103. programName = "Default";
  104. }
  105. void PowerJuicePlugin::d_initStateKey(uint32_t /*index*/, d_string& /*key*/)
  106. {
  107. /*
  108. if (index != 0)
  109. return;
  110. key = "shmKey";
  111. */
  112. }
  113. // -----------------------------------------------------------------------
  114. // Internal data
  115. float PowerJuicePlugin::d_getParameterValue(uint32_t index) const
  116. {
  117. switch (index)
  118. {
  119. case paramAttack:
  120. return attack;
  121. case paramRelease:
  122. return release;
  123. case paramThreshold:
  124. return threshold;
  125. case paramRatio:
  126. return ratio;
  127. case paramMakeup:
  128. return makeup;
  129. case paramMix:
  130. return mix;
  131. default:
  132. return 0.0f;
  133. }
  134. }
  135. void PowerJuicePlugin::d_setParameterValue(uint32_t index, float value)
  136. {
  137. switch (index)
  138. {
  139. case paramAttack:
  140. attack = value;
  141. break;
  142. case paramRelease:
  143. release = value;
  144. break;
  145. case paramThreshold:
  146. threshold = value;
  147. break;
  148. case paramRatio:
  149. ratio = value;
  150. break;
  151. case paramMakeup:
  152. makeup = value;
  153. break;
  154. case paramMix:
  155. mix = value;
  156. break;
  157. }
  158. }
  159. void PowerJuicePlugin::d_setProgram(uint32_t index)
  160. {
  161. if (index != 0)
  162. return;
  163. /* Default parameter values */
  164. attack = 20.0f;
  165. release = 200.0f;
  166. threshold = 0.0f;
  167. ratio = 1.0f;
  168. makeup = 0.0f;
  169. mix = 1.0f;
  170. /* Default variable values */
  171. averageCounter = 0;
  172. inputMin = 0.0f;
  173. inputMax = 0.0f;
  174. input.start = 0;
  175. output.start = 0;
  176. gainReduction.start = 0;
  177. std::memset(input.data, 0, sizeof(float)*kFloatStackCount);
  178. std::memset(output.data, 0, sizeof(float)*kFloatStackCount);
  179. std::memset(gainReduction.data, 0, sizeof(float)*kFloatStackCount);
  180. d_activate();
  181. }
  182. void PowerJuicePlugin::d_setState(const char* key, const char* value)
  183. {
  184. if (std::strcmp(key, "shmKey") != 0)
  185. return;
  186. if (value[0] == '\0')
  187. {
  188. carla_stdout("Shm closed");
  189. return closeShm();
  190. }
  191. carla_stdout("Got shmKey => %s", value);
  192. initShm(value);
  193. }
  194. // -----------------------------------------------------------------------
  195. // Process
  196. void PowerJuicePlugin::d_activate()
  197. {
  198. }
  199. void PowerJuicePlugin::d_deactivate()
  200. {
  201. // all values to zero
  202. }
  203. void PowerJuicePlugin::d_run(float** inputs, float** /*outputs*/, uint32_t frames)
  204. {
  205. float* in = inputs[0];
  206. //float* out = outputs[0];
  207. for (uint32_t i=0; i < frames; i++) {
  208. //for every sample
  209. //printf("av");
  210. //averageInputs[averageCounter] = in[i];
  211. if (in[i]<inputMin) {
  212. inputMin = in[i];
  213. }
  214. if (in[i]>inputMax) {
  215. inputMax = in[i];
  216. }
  217. if (++averageCounter == 300) {
  218. //output waveform parameter
  219. input.data[input.start++] = inputMin;
  220. input.data[input.start++] = inputMax;
  221. if (input.start == kFloatStackCount)
  222. input.start = 0;
  223. if (shmData != nullptr)
  224. {
  225. for (int j=0; j < kFloatStackCount; ++j)
  226. shmData->input[j] = input.data[(input.start+j) % kFloatStackCount];
  227. }
  228. averageCounter = 0;
  229. inputMin = 0.0f;
  230. inputMax = 0.0f;
  231. }
  232. }
  233. }
  234. void PowerJuicePlugin::initShm(const char* shmKey)
  235. {
  236. shm = carla_shm_attach(shmKey);
  237. if (! carla_is_shm_valid(shm))
  238. {
  239. carla_stderr2("Failed to created shared memory!");
  240. return;
  241. }
  242. if (! carla_shm_map<SharedMemData>(shm, shmData))
  243. {
  244. carla_stderr2("Failed to map shared memory!");
  245. return;
  246. }
  247. }
  248. void PowerJuicePlugin::closeShm()
  249. {
  250. if (! carla_is_shm_valid(shm))
  251. return;
  252. if (shmData != nullptr)
  253. {
  254. carla_shm_unmap<SharedMemData>(shm, shmData);
  255. shmData = nullptr;
  256. }
  257. carla_shm_close(shm);
  258. }
  259. // -----------------------------------------------------------------------
  260. Plugin* createPlugin()
  261. {
  262. return new PowerJuicePlugin();
  263. }
  264. // -----------------------------------------------------------------------
  265. END_NAMESPACE_DISTRHO