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.

ZamSynthUI.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * ZamSynth polyphonic synthesiser
  3. * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
  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 "ZamSynthPlugin.hpp"
  18. #include "ZamSynthUI.hpp"
  19. START_NAMESPACE_DISTRHO
  20. // -----------------------------------------------------------------------
  21. ZamSynthUI::ZamSynthUI()
  22. : UI()
  23. {
  24. // background
  25. fImgBackground = Image(ZamSynthArtwork::zamsynthData, ZamSynthArtwork::zamsynthWidth, ZamSynthArtwork::zamsynthHeight, GL_BGR);
  26. fDragging = false;
  27. fDragValid = false;
  28. // knob
  29. Image knobImage(ZamSynthArtwork::knobData, ZamSynthArtwork::knobWidth, ZamSynthArtwork::knobHeight);
  30. // button
  31. Image smoothrImage(ZamSynthArtwork::smoothrData, ZamSynthArtwork::smoothrWidth, ZamSynthArtwork::smoothrHeight);
  32. Image smoothyImage(ZamSynthArtwork::smoothyData, ZamSynthArtwork::smoothyWidth, ZamSynthArtwork::smoothyHeight);
  33. // toggle
  34. Image toggleonImage(ZamSynthArtwork::toggleonData, ZamSynthArtwork::toggleonWidth, ZamSynthArtwork::toggleonHeight);
  35. Image toggleoffImage(ZamSynthArtwork::toggleoffData, ZamSynthArtwork::toggleoffWidth, ZamSynthArtwork::toggleoffHeight);
  36. // knob
  37. fKnobGain = new ImageKnob(this, knobImage);
  38. fKnobGain->setAbsolutePos(284.75, 240);
  39. fKnobGain->setRange(-30.f, 30.0f);
  40. fKnobGain->setDefault(0.0f);
  41. fKnobGain->setRotationAngle(240);
  42. fKnobGain->setCallback(this);
  43. fKnobSpeed = new ImageKnob(this, knobImage);
  44. fKnobSpeed->setAbsolutePos(284.75, 92.5);
  45. fKnobSpeed->setRange(1.f, 20.0f);
  46. fKnobSpeed->setDefault(10.0f);
  47. fKnobSpeed->setStep(1.0f);
  48. fKnobSpeed->setRotationAngle(240);
  49. fKnobSpeed->setCallback(this);
  50. // button
  51. fButtonSmooth = new ImageButton(this, smoothrImage, smoothrImage, smoothyImage);
  52. fButtonSmooth->setAbsolutePos(265, 165);
  53. fButtonSmooth->setCallback(this);
  54. // drawing area
  55. fCanvasArea.setPos(10,10);
  56. fCanvasArea.setSize(AREAHEIGHT,AREAHEIGHT);
  57. for (int i = 0; i < AREAHEIGHT; i++) {
  58. wave_y[i] = -(AREAHEIGHT*(sin(2.*i*M_PI/AREAHEIGHT)-1.0))/2.;
  59. env_y[i] = -(2*AREAHEIGHT*(sin(2.*i*M_PI/AREAHEIGHT/2.)-1.0))/2. < AREAHEIGHT / 2. ? -(2*AREAHEIGHT*(sin(2.*i*M_PI/AREAHEIGHT/2.)-1.0))/2. : AREAHEIGHT / 2.;
  60. }
  61. // toggle
  62. fToggleGraph = new ImageToggle(this, toggleonImage, toggleoffImage);
  63. fToggleGraph->setAbsolutePos(300, 33);
  64. fToggleGraph->setCallback(this);
  65. fToggleGraph->setValue(0.f);
  66. // set default values
  67. d_programChanged(0);
  68. }
  69. void ZamSynthUI::d_stateChanged(const char* key, const char* value)
  70. {
  71. if (strcmp(key, "waveform") == 0) {
  72. char* tmp;
  73. int i = 0;
  74. char tmpbuf[4*AREAHEIGHT+1] = {0};
  75. snprintf(tmpbuf, 4*AREAHEIGHT, "%s", value);
  76. tmp = strtok(tmpbuf, " ");
  77. while ((tmp != NULL) && (i < AREAHEIGHT)) {
  78. wave_y[i] = AREAHEIGHT-((float)atoi(tmp));
  79. i++;
  80. //printf("reload dsp wave_y[%d]=%.2f ", i, wave_y[i]);
  81. tmp = strtok(NULL, " ");
  82. }
  83. } else if (strcmp(key, "envelope") == 0) {
  84. char* tmp;
  85. int i = 0;
  86. char tmpbuf[4*AREAHEIGHT+1] = {0};
  87. snprintf(tmpbuf, 4*AREAHEIGHT, "%s", value);
  88. tmp = strtok(tmpbuf, " ");
  89. while ((tmp != NULL) && (i < AREAHEIGHT)) {
  90. env_y[i] = AREAHEIGHT-((float)atoi(tmp));
  91. i++;
  92. //printf("reload dsp env_y[%d]=%.2f ", i, env_y[i]);
  93. tmp = strtok(NULL, " ");
  94. }
  95. }
  96. }
  97. // -----------------------------------------------------------------------
  98. // DSP Callbacks
  99. void ZamSynthUI::d_parameterChanged(uint32_t index, float value)
  100. {
  101. switch (index)
  102. {
  103. case ZamSynthPlugin::paramGain:
  104. fKnobGain->setValue(value);
  105. break;
  106. case ZamSynthPlugin::paramSpeed:
  107. fKnobSpeed->setValue(value);
  108. break;
  109. case ZamSynthPlugin::paramGraph:
  110. fToggleGraph->setValue(value);
  111. break;
  112. }
  113. }
  114. void ZamSynthUI::d_programChanged(uint32_t index)
  115. {
  116. if (index != 0)
  117. return;
  118. fKnobGain->setDefault(0.0f);
  119. fKnobSpeed->setDefault(10.0f);
  120. }
  121. // -----------------------------------------------------------------------
  122. // Widget Callbacks
  123. void ZamSynthUI::imageKnobDragStarted(ImageKnob* knob)
  124. {
  125. if (knob == fKnobGain)
  126. d_editParameter(ZamSynthPlugin::paramGain, true);
  127. else if (knob == fKnobSpeed)
  128. d_editParameter(ZamSynthPlugin::paramSpeed, true);
  129. }
  130. void ZamSynthUI::imageKnobDragFinished(ImageKnob* knob)
  131. {
  132. if (knob == fKnobGain)
  133. d_editParameter(ZamSynthPlugin::paramGain, false);
  134. else if (knob == fKnobSpeed)
  135. d_editParameter(ZamSynthPlugin::paramSpeed, false);
  136. }
  137. void ZamSynthUI::imageKnobValueChanged(ImageKnob* knob, float value)
  138. {
  139. if (knob == fKnobGain)
  140. d_setParameterValue(ZamSynthPlugin::paramGain, value);
  141. else if (knob == fKnobSpeed)
  142. d_setParameterValue(ZamSynthPlugin::paramSpeed, value);
  143. }
  144. void ZamSynthUI::imageButtonClicked(ImageButton*, int)
  145. {
  146. float wavesmooth[AREAHEIGHT];
  147. float xs[AREAHEIGHT];
  148. int i;
  149. for (i = 0; i < AREAHEIGHT; i++) {
  150. xs[i] = i;
  151. }
  152. float *gr;
  153. gr = (fToggleGraph->getValue() == 1.f) ? env_y : wave_y;
  154. gaussiansmooth(wavesmooth, xs, gr, AREAHEIGHT, 4);
  155. memcpy(gr, wavesmooth, AREAHEIGHT*sizeof(float));
  156. char tmp[4*AREAHEIGHT+1] = {0};
  157. for(i = 0; i < AREAHEIGHT; i++) {
  158. char wavestr[5] = {0};
  159. snprintf(wavestr, sizeof(wavestr), "%03d ", (int) (fCanvasArea.getHeight()-gr[i]));
  160. strcat(tmp, wavestr);
  161. }
  162. if (fToggleGraph->getValue() == 1.f)
  163. d_setState("envelope", tmp);
  164. else
  165. d_setState("waveform", tmp);
  166. }
  167. void ZamSynthUI::imageToggleClicked(ImageToggle*, int)
  168. {
  169. float toggle = fToggleGraph->getValue();
  170. fToggleGraph->setValue(toggle);
  171. d_setParameterValue(ZamSynthPlugin::paramGraph, toggle);
  172. }
  173. void ZamSynthUI::gaussiansmooth(float* smoothed, float* xs, float* ys, int n, int radius)
  174. {
  175. int i,j;
  176. float numer;
  177. float denom;
  178. float kernel;
  179. for (i = 0; i < n; i++) {
  180. numer = 0.f;
  181. denom = 0.f;
  182. for (j = 0; j < n; j++) {
  183. kernel = expf(-(i - xs[j])*(i - xs[j]) / (2. * radius));
  184. numer += kernel * ys[j];
  185. denom += kernel;
  186. }
  187. smoothed[i] = numer / denom;
  188. }
  189. }
  190. bool ZamSynthUI::onMouse(const MouseEvent& ev)
  191. {
  192. if (ev.button != 1)
  193. return false;
  194. if (ev.press)
  195. {
  196. if (! fCanvasArea.contains(ev.pos)) {
  197. //fDragValid = false;
  198. return false;
  199. }
  200. fDragging = true;
  201. fDragValid = true;
  202. return true;
  203. }
  204. else if (fDragging)
  205. {
  206. fDragging = false;
  207. return true;
  208. }
  209. return false;
  210. }
  211. bool ZamSynthUI::onMotion(const MotionEvent& ev)
  212. {
  213. if (! fDragging)
  214. return false;
  215. if (! fDragValid)
  216. {
  217. fDragValid = true;
  218. }
  219. int x = ev.pos.getX();
  220. int y = ev.pos.getY();
  221. if (x > fCanvasArea.getWidth()+10)
  222. x = fCanvasArea.getWidth()+10;
  223. if (x < 10) x = 10;
  224. if (y < 10) y = 10;
  225. float *gr;
  226. if (fToggleGraph->getValue() == 0.f) {
  227. gr = wave_y;
  228. if (y > fCanvasArea.getHeight()+10)
  229. y = fCanvasArea.getHeight()+10;
  230. } else {
  231. gr = env_y;
  232. if (y > fCanvasArea.getHeight() / 2. + 10)
  233. y = fCanvasArea.getHeight() / 2. + 10;
  234. }
  235. if (gr[x-10] != (y-10)) {
  236. char tmp[4*AREAHEIGHT+1] = {0};
  237. int i;
  238. for(i = 0; i < AREAHEIGHT; i++) {
  239. char wavestr[5] = {0};
  240. snprintf(wavestr, sizeof(wavestr), "%03d ", (int) (fCanvasArea.getHeight()-gr[i]));
  241. strcat(tmp, wavestr);
  242. }
  243. gr[x-10] = y-10;
  244. if (gr == env_y)
  245. d_setState("envelope",tmp);
  246. else
  247. d_setState("waveform",tmp);
  248. repaint();
  249. }
  250. return true;
  251. }
  252. void ZamSynthUI::onDisplay()
  253. {
  254. fImgBackground.draw();
  255. glEnable(GL_BLEND);
  256. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  257. glEnable(GL_LINE_SMOOTH);
  258. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  259. glLineWidth(2);
  260. float *gr;
  261. gr = (fToggleGraph->getValue() == 1.f) ? env_y : wave_y;
  262. int i;
  263. glColor4f(0.235f, 1.f, 0.235f, 1.0f);
  264. for (i = 2; i < AREAHEIGHT; ++i) {
  265. glBegin(GL_LINES);
  266. glVertex2i(i-1+fCanvasArea.getX(), gr[i-1]+fCanvasArea.getY());
  267. glVertex2i(i+fCanvasArea.getX(), gr[i]+fCanvasArea.getY());
  268. glEnd();
  269. }
  270. // reset color
  271. glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  272. }
  273. // -----------------------------------------------------------------------
  274. UI* createUI()
  275. {
  276. return new ZamSynthUI();
  277. }
  278. // -----------------------------------------------------------------------
  279. END_NAMESPACE_DISTRHO