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.

332 lines
8.8KB

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