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.

339 lines
9.4KB

  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 "PowerJuiceUI.hpp"
  18. #include <cstdlib>
  19. #include <ctime>
  20. using DGL::Point;
  21. START_NAMESPACE_DISTRHO
  22. // -----------------------------------------------------------------------
  23. PowerJuiceUI::PowerJuiceUI()
  24. : UI(),
  25. fAboutWindow(this),
  26. shmData(nullptr)
  27. {
  28. // background
  29. fImgBackground = Image(PowerJuiceArtwork::backgroundData, PowerJuiceArtwork::backgroundWidth, PowerJuiceArtwork::backgroundHeight, GL_BGR);
  30. // about
  31. Image imageAbout(PowerJuiceArtwork::aboutData, PowerJuiceArtwork::aboutWidth, PowerJuiceArtwork::aboutHeight, GL_BGR);
  32. fAboutWindow.setImage(imageAbout);
  33. // knobs
  34. Image knobImage(PowerJuiceArtwork::knobData, PowerJuiceArtwork::knobWidth, PowerJuiceArtwork::knobHeight);
  35. // knob Attack
  36. fKnobAttack = new ImageKnob(this, knobImage);
  37. fKnobAttack->setPos(37, 213);
  38. fKnobAttack->setRange(0.1f, 1000.0f);
  39. fKnobAttack->setStep(0.1f);
  40. fKnobAttack->setValue(20.0f);
  41. fKnobAttack->setRotationAngle(270);
  42. fKnobAttack->setCallback(this);
  43. // knob Release
  44. fKnobRelease = new ImageKnob(this, knobImage);
  45. fKnobRelease->setPos(136, 213);
  46. fKnobRelease->setRange(0.1f, 1000.0f);
  47. fKnobRelease->setValue(0.1f);
  48. fKnobRelease->setRotationAngle(270);
  49. fKnobRelease->setCallback(this);
  50. // knob Threshold
  51. fKnobThreshold = new ImageKnob(this, knobImage);
  52. fKnobThreshold->setPos(235, 213);
  53. fKnobThreshold->setRange(-60.0f, 0.0f);
  54. fKnobThreshold->setValue(0.0f);
  55. fKnobThreshold->setRotationAngle(270);
  56. fKnobThreshold->setCallback(this);
  57. // knob Ratio
  58. fKnobRatio = new ImageKnob(this, knobImage);
  59. fKnobRatio->setPos(334, 213);
  60. fKnobRatio->setRange(1.0f, 10.0f);
  61. fKnobRatio->setValue(1.0f);
  62. fKnobRatio->setRotationAngle(270);
  63. fKnobRatio->setCallback(this);
  64. // knob Make-Up
  65. fKnobMakeup = new ImageKnob(this, knobImage);
  66. fKnobMakeup->setPos(433, 213);
  67. fKnobMakeup->setRange(0.0f, 20.0f);
  68. fKnobMakeup->setValue(0.0f);
  69. fKnobMakeup->setRotationAngle(270);
  70. fKnobMakeup->setCallback(this);
  71. // knob Mix
  72. fKnobMix = new ImageKnob(this, knobImage);
  73. fKnobMix->setPos(532, 213);
  74. fKnobMix->setRange(0.0f, 1.0f);
  75. fKnobMix->setValue(1.0f);
  76. fKnobMix->setRotationAngle(270);
  77. fKnobMix->setCallback(this);
  78. // about button
  79. Image aboutImageNormal(PowerJuiceArtwork::aboutButtonNormalData, PowerJuiceArtwork::aboutButtonNormalWidth, PowerJuiceArtwork::aboutButtonNormalHeight);
  80. Image aboutImageHover(PowerJuiceArtwork::aboutButtonHoverData, PowerJuiceArtwork::aboutButtonHoverWidth, PowerJuiceArtwork::aboutButtonHoverHeight);
  81. fButtonAbout = new ImageButton(this, aboutImageNormal, aboutImageHover, aboutImageHover);
  82. fButtonAbout->setPos(502, 17);
  83. fButtonAbout->setCallback(this);
  84. // init shm vars
  85. carla_shm_init(shm);
  86. shmData = nullptr;
  87. fFirstDisplay = true;
  88. }
  89. PowerJuiceUI::~PowerJuiceUI()
  90. {
  91. delete fKnobAttack;
  92. delete fKnobRelease;
  93. delete fKnobThreshold;
  94. delete fKnobRatio;
  95. delete fKnobMakeup;
  96. delete fKnobMix;
  97. delete fButtonAbout;
  98. closeShm();
  99. }
  100. // -----------------------------------------------------------------------
  101. // DSP Callbacks
  102. void PowerJuiceUI::d_parameterChanged(uint32_t index, float value)
  103. {
  104. switch (index)
  105. {
  106. case PowerJuicePlugin::paramAttack:
  107. fKnobAttack->setValue(value);
  108. break;
  109. case PowerJuicePlugin::paramRelease:
  110. fKnobRelease->setValue(value);
  111. break;
  112. case PowerJuicePlugin::paramThreshold:
  113. fKnobThreshold->setValue(value);
  114. break;
  115. case PowerJuicePlugin::paramRatio:
  116. fKnobRatio->setValue(value);
  117. break;
  118. case PowerJuicePlugin::paramMakeup:
  119. fKnobMakeup->setValue(value);
  120. break;
  121. case PowerJuicePlugin::paramMix:
  122. fKnobMix->setValue(value);
  123. break;
  124. }
  125. }
  126. void PowerJuiceUI::d_programChanged(uint32_t index)
  127. {
  128. if (index != 0)
  129. return;
  130. // Default values
  131. fKnobAttack->setValue(20.0f);
  132. fKnobRelease->setValue(200.0f);
  133. fKnobThreshold->setValue(0.0f);
  134. fKnobRatio->setValue(1.0f);
  135. fKnobMakeup->setValue(0.0f);
  136. fKnobMix->setValue(1.0f);
  137. }
  138. void PowerJuiceUI::d_stateChanged(const char*, const char*)
  139. {
  140. }
  141. // -----------------------------------------------------------------------
  142. // Widget Callbacks
  143. void PowerJuiceUI::imageButtonClicked(ImageButton* button, int)
  144. {
  145. if (button != fButtonAbout)
  146. return;
  147. fAboutWindow.exec();
  148. }
  149. void PowerJuiceUI::imageKnobDragStarted(ImageKnob* knob)
  150. {
  151. if (knob == fKnobAttack)
  152. d_editParameter(PowerJuicePlugin::paramAttack, true);
  153. else if (knob == fKnobRelease)
  154. d_editParameter(PowerJuicePlugin::paramRelease, true);
  155. else if (knob == fKnobThreshold)
  156. d_editParameter(PowerJuicePlugin::paramThreshold, true);
  157. else if (knob == fKnobRatio)
  158. d_editParameter(PowerJuicePlugin::paramRatio, true);
  159. else if (knob == fKnobMakeup)
  160. d_editParameter(PowerJuicePlugin::paramMakeup, true);
  161. else if (knob == fKnobMix)
  162. d_editParameter(PowerJuicePlugin::paramMix, true);
  163. }
  164. void PowerJuiceUI::imageKnobDragFinished(ImageKnob* knob)
  165. {
  166. if (knob == fKnobAttack)
  167. d_editParameter(PowerJuicePlugin::paramAttack, false);
  168. else if (knob == fKnobRelease)
  169. d_editParameter(PowerJuicePlugin::paramRelease, false);
  170. else if (knob == fKnobThreshold)
  171. d_editParameter(PowerJuicePlugin::paramThreshold, false);
  172. else if (knob == fKnobRatio)
  173. d_editParameter(PowerJuicePlugin::paramRatio, false);
  174. else if (knob == fKnobMakeup)
  175. d_editParameter(PowerJuicePlugin::paramMakeup, false);
  176. else if (knob == fKnobMix)
  177. d_editParameter(PowerJuicePlugin::paramMix, false);
  178. }
  179. void PowerJuiceUI::imageKnobValueChanged(ImageKnob* knob, float value)
  180. {
  181. if (knob == fKnobAttack)
  182. d_setParameterValue(PowerJuicePlugin::paramAttack, value);
  183. else if (knob == fKnobRelease)
  184. d_setParameterValue(PowerJuicePlugin::paramRelease, value);
  185. else if (knob == fKnobThreshold)
  186. d_setParameterValue(PowerJuicePlugin::paramThreshold, value);
  187. else if (knob == fKnobRatio)
  188. d_setParameterValue(PowerJuicePlugin::paramRatio, value);
  189. else if (knob == fKnobMakeup)
  190. d_setParameterValue(PowerJuicePlugin::paramMakeup, value);
  191. else if (knob == fKnobMix)
  192. d_setParameterValue(PowerJuicePlugin::paramMix, value);
  193. }
  194. void PowerJuiceUI::d_uiIdle() {
  195. repaint();
  196. }
  197. void PowerJuiceUI::onDisplay()
  198. {
  199. if (fFirstDisplay)
  200. {
  201. initShm();
  202. fFirstDisplay = false;
  203. }
  204. fImgBackground.draw();
  205. if (shmData == nullptr)
  206. return;
  207. int w = 563; //waveform plane size, size of the plane in pixels;
  208. int w2 = 1126; //wavefowm array
  209. int h = 60; //waveform plane height
  210. int x = 28; //waveform plane positions
  211. int y = 51;
  212. int dc = 113; //0DC line y position
  213. //draw waveform
  214. for (int i=0; i<w2; i+=2) {
  215. //glEnable(GL_BLEND);
  216. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  217. //glEnable(GL_LINE_SMOOTH);
  218. //glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  219. glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
  220. glLineWidth(1.0f);
  221. glBegin(GL_LINES);
  222. glVertex2i(x+(i/2), shmData->input[i]*h+dc);
  223. glVertex2i(x+(i/2), shmData->input[i+1]*h+dc);
  224. glEnd();
  225. // reset color
  226. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  227. }
  228. //draw shits
  229. }
  230. void PowerJuiceUI::onClose()
  231. {
  232. // tell DSP to stop sending SHM data
  233. d_setState("shmKey", "");
  234. }
  235. void PowerJuiceUI::initShm()
  236. {
  237. // generate a random key
  238. static const char charSet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  239. static const int charSetLen = sizeof(charSet) - 1; // -1 to avoid trailing '\0'
  240. char shmKey[24+1];
  241. shmKey[24] = '\0';
  242. std::srand(std::time(nullptr));
  243. for (int i=0; i<24; ++i)
  244. shmKey[i] = charSet[std::rand() % charSetLen];
  245. // create shared memory
  246. shm = carla_shm_create(shmKey);
  247. if (! carla_is_shm_valid(shm))
  248. {
  249. carla_stderr2("Failed to created shared memory!");
  250. return;
  251. }
  252. if (! carla_shm_map<SharedMemData>(shm, shmData))
  253. {
  254. carla_stderr2("Failed to map shared memory!");
  255. return;
  256. }
  257. std::memset(shmData, 0, sizeof(SharedMemData));
  258. // tell DSP to use this key for SHM
  259. carla_stdout("Sending shmKey %s", shmKey);
  260. d_setState("shmKey", shmKey);
  261. }
  262. void PowerJuiceUI::closeShm()
  263. {
  264. fFirstDisplay = true;
  265. if (! carla_is_shm_valid(shm))
  266. return;
  267. if (shmData != nullptr)
  268. {
  269. carla_shm_unmap<SharedMemData>(shm, shmData);
  270. shmData = nullptr;
  271. }
  272. carla_shm_close(shm);
  273. }
  274. // -----------------------------------------------------------------------
  275. UI* createUI()
  276. {
  277. return new PowerJuiceUI();
  278. }
  279. // -----------------------------------------------------------------------
  280. END_NAMESPACE_DISTRHO