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.

351 lines
10KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. EnvelopeFreeEdit.cpp - Envelope Edit View
  4. Copyright (C) 2016 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #include "EnvelopeFreeEdit.h"
  11. #include "../Misc/Util.h"
  12. #include <FL/Fl.H>
  13. #include <FL/fl_draw.H>
  14. #include <cstdlib>
  15. #include <cassert>
  16. #include <rtosc/rtosc.h>
  17. EnvelopeFreeEdit::EnvelopeFreeEdit(int x,int y, int w, int h, const char *label)
  18. :Fl_Box(x,y,w,h,label), Fl_Osc_Widget(this)
  19. {
  20. pair=NULL;
  21. currentpoint=-1;
  22. cpx=0;
  23. lastpoint=-1;
  24. }
  25. void EnvelopeFreeEdit::init(void)
  26. {
  27. oscRegister("Penvpoints");
  28. oscRegister("Penvdt");
  29. oscRegister("Penvval");
  30. oscRegister("Penvsustain");
  31. //register for non-bulk types
  32. for(int i=0; i<MAX_ENVELOPE_POINTS; ++i) {
  33. osc->createLink(loc+string("Penvdt") + to_s(i), this);
  34. osc->createLink(loc+string("Penvval") + to_s(i), this);
  35. }
  36. }
  37. void EnvelopeFreeEdit::OSC_raw(const char *msg)
  38. {
  39. const char *args = rtosc_argument_string(msg);
  40. if(strstr(msg,"Penvpoints") && !strcmp(args, "i")) {
  41. Penvpoints = rtosc_argument(msg, 0).i;
  42. } else if(strstr(msg,"Penvdt") && !strcmp(args, "b")) {
  43. rtosc_blob_t b = rtosc_argument(msg, 0).b;
  44. assert(b.len == MAX_ENVELOPE_POINTS);
  45. memcpy(Penvdt, b.data, MAX_ENVELOPE_POINTS);
  46. } else if(strstr(msg,"Penvval") && !strcmp(args, "b")) {
  47. rtosc_blob_t b = rtosc_argument(msg, 0).b;
  48. assert(b.len == MAX_ENVELOPE_POINTS);
  49. memcpy(Penvval, b.data, MAX_ENVELOPE_POINTS);
  50. } else if(strstr(msg, "Penvval") && !strcmp(args, "c")) {
  51. const char *str = strstr(msg, "Penvval");
  52. int id = atoi(str+7);
  53. assert(0 <= id && id < MAX_ENVELOPE_POINTS);
  54. Penvval[id] = rtosc_argument(msg, 0).i;
  55. } else if(strstr(msg, "Penvdt") && !strcmp(args, "c")) {
  56. const char *str = strstr(msg, "Penvdt");
  57. int id = atoi(str+6);
  58. assert(0 <= id && id < MAX_ENVELOPE_POINTS);
  59. Penvdt[id] = rtosc_argument(msg, 0).i;
  60. } else if(strstr(msg,"Penvsustain") && !strcmp(args, "i")) {
  61. Penvsustain = rtosc_argument(msg, 0).i;
  62. }
  63. redraw();
  64. do_callback();
  65. }
  66. void EnvelopeFreeEdit::setpair(Fl_Box *pair_)
  67. {
  68. pair=pair_;
  69. }
  70. int EnvelopeFreeEdit::getpointx(int n) const
  71. {
  72. const int lx=w()-10;
  73. int npoints=Penvpoints;
  74. float sum=0;
  75. for(int i=1; i<npoints; ++i)
  76. sum+=getdt(i)+1;
  77. float sumbefore=0;//the sum of all points before the computed point
  78. for(int i=1; i<=n; ++i)
  79. sumbefore+=getdt(i)+1;
  80. return (int) (sumbefore/(float) sum*lx);
  81. }
  82. int EnvelopeFreeEdit::getpointy(int n) const
  83. {
  84. const int ly=h()-10;
  85. return (1.0-Penvval[n]/127.0)*ly;
  86. }
  87. static inline int distance_fn(int dx, int dy) {
  88. return dx*dx+dy*dy;
  89. }
  90. int EnvelopeFreeEdit::getnearest(int x,int y) const
  91. {
  92. x-=5;y-=5;
  93. int nearestpoint=0;
  94. int nearest_distance_sq=distance_fn(x-getpointx(0), y-getpointy(0));
  95. for(int i=1; i<Penvpoints; ++i){
  96. int distance_sq=distance_fn(x-getpointx(i), y-getpointy(i));
  97. if (distance_sq<nearest_distance_sq) {
  98. nearestpoint=i;
  99. nearest_distance_sq=distance_sq;
  100. }
  101. }
  102. return nearestpoint;
  103. }
  104. static float dt(char val)
  105. {
  106. return (powf(2.0f, val / 127.0f * 12.0f) - 1.0f) * 10.0f; //miliseconds
  107. }
  108. float EnvelopeFreeEdit::getdt(int i) const
  109. {
  110. return dt(Penvdt[i]);
  111. }
  112. static bool ctrldown, altdown;
  113. void EnvelopeFreeEdit::draw(void)
  114. {
  115. int ox=x(),oy=y(),lx=w(),ly=h();
  116. //if (env->Pfreemode==0)
  117. // env->converttofree();
  118. const int npoints=Penvpoints;
  119. if (active_r()) fl_color(FL_BLACK);
  120. else fl_color(90,90,90);
  121. if (!active_r()) currentpoint=-1;
  122. fl_rectf(ox,oy,lx,ly);
  123. //Margins
  124. ox+=5;oy+=5;lx-=10;ly-=10;
  125. //draw the lines
  126. fl_color(FL_GRAY);
  127. const int midline = oy+ly*(1-64.0/127);
  128. fl_line_style(FL_SOLID);
  129. fl_line(ox+2,midline,ox+lx-2,midline);
  130. //draws the evelope points and lines
  131. Fl_Color alb=FL_WHITE;
  132. if (!active_r()) alb=fl_rgb_color(180,180,180);
  133. fl_color(alb);
  134. int oldxx=0,xx=0,oldyy=0,yy=getpointy(0);
  135. fl_rectf(ox-3,oy+yy-3,6,6);
  136. for (int i=1; i<npoints; ++i){
  137. oldxx=xx;oldyy=yy;
  138. xx=getpointx(i);yy=getpointy(i);
  139. if (i==currentpoint || (ctrldown && i==lastpoint))
  140. fl_color(FL_RED);
  141. else
  142. fl_color(alb);
  143. fl_line(ox+oldxx,oy+oldyy,ox+xx,oy+yy);
  144. fl_rectf(ox+xx-3,oy+yy-3,6,6);
  145. }
  146. //draw the last moved point point (if exists)
  147. if (lastpoint>=0){
  148. fl_color(FL_CYAN);
  149. fl_rectf(ox+getpointx(lastpoint)-5,oy+getpointy(lastpoint)-5,10,10);
  150. }
  151. //draw the sustain position
  152. if(Penvsustain>0){
  153. fl_color(FL_YELLOW);
  154. xx=getpointx(Penvsustain);
  155. fl_line(ox+xx,oy+0,ox+xx,oy+ly);
  156. }
  157. //Show the envelope duration and the current line duration
  158. fl_font(FL_HELVETICA|FL_BOLD,10);
  159. float time=0.0;
  160. if (currentpoint<=0 && (!ctrldown||lastpoint <= 0)){
  161. fl_color(alb);
  162. for(int i=1; i<npoints; ++i)
  163. time+=getdt(i);
  164. } else {
  165. fl_color(FL_RED);
  166. time=getdt(lastpoint);
  167. }
  168. char tmpstr[20];
  169. if (!altdown || ctrldown) {
  170. if (time<1000.0)
  171. snprintf((char *)&tmpstr,20,"%.1fms",time);
  172. else
  173. snprintf((char *)&tmpstr,20,"%.2fs",time/1000.0);
  174. fl_draw(tmpstr,ox+lx-20,oy+ly-10,20,10,FL_ALIGN_RIGHT,NULL,0);
  175. }
  176. if (!altdown || !ctrldown) {
  177. if (lastpoint>=0){
  178. snprintf((char *)&tmpstr,20,"%d", Penvval[lastpoint]);
  179. fl_draw(tmpstr,ox+lx-20,oy+ly-23,20,10,FL_ALIGN_RIGHT,NULL,0);
  180. }
  181. }
  182. }
  183. int EnvelopeFreeEdit::handle(int event)
  184. {
  185. const int x_=Fl::event_x()-x();
  186. const int y_=Fl::event_y()-y();
  187. static Fl_Widget *old_focus;
  188. int key, old_mod_state;
  189. switch(event) {
  190. case FL_ENTER:
  191. old_focus=Fl::focus();
  192. Fl::focus(this);
  193. // Otherwise the underlying window seems to regrab focus,
  194. // and I can't see the KEYDOWN action.
  195. return 1;
  196. case FL_LEAVE:
  197. Fl::focus(old_focus);
  198. break;
  199. case FL_KEYDOWN:
  200. case FL_KEYUP:
  201. key = Fl::event_key();
  202. if (key==FL_Alt_L || key==FL_Alt_R) {
  203. altdown = (event==FL_KEYDOWN);
  204. redraw();
  205. if (pair!=NULL) pair->redraw();
  206. }
  207. if (key==FL_Control_L || key==FL_Control_R){
  208. ctrldown = (event==FL_KEYDOWN);
  209. redraw();
  210. if (pair!=NULL) pair->redraw();
  211. }
  212. break;
  213. case FL_PUSH:
  214. currentpoint=getnearest(x_,y_);
  215. cpx=x_;
  216. cpy=y_;
  217. cpdt=Penvdt[currentpoint];
  218. cpval=Penvval[currentpoint];
  219. lastpoint=currentpoint;
  220. redraw();
  221. if (pair)
  222. pair->redraw();
  223. return 1;
  224. case FL_RELEASE:
  225. currentpoint=-1;
  226. redraw();
  227. if (pair)
  228. pair->redraw();
  229. return 1;
  230. case FL_MOUSEWHEEL:
  231. if (Fl::event_buttons())
  232. return 1;
  233. if (lastpoint>=0) {
  234. int delta = Fl::event_dy() * (Fl::event_shift() ? 4 : 1);
  235. if (!ctrldown) {
  236. int ny = Penvval[lastpoint] - delta;
  237. ny = ny < 0 ? 0 : ny > 127 ? 127 : ny;
  238. Penvval[lastpoint] = ny;
  239. oscWrite(to_s("Penvval")+to_s(lastpoint), "c", ny);
  240. oscWrite("Penvval","");
  241. } else if (lastpoint > 0) {
  242. int newdt = Penvdt[lastpoint] - delta;
  243. newdt = newdt < 0 ? 0 : newdt > 127 ? 127 : newdt;
  244. Penvdt[lastpoint] = newdt;
  245. oscWrite(to_s("Penvdt")+to_s(lastpoint), "c", newdt);
  246. oscWrite("Penvdt","");
  247. }
  248. redraw();
  249. if (pair!=NULL) pair->redraw();
  250. return 1;
  251. }
  252. case FL_DRAG:
  253. if (currentpoint>=0){
  254. old_mod_state = mod_state;
  255. mod_state = ctrldown << 1 | altdown;
  256. if (old_mod_state != mod_state) {
  257. cpx=x_;
  258. cpy=y_;
  259. cpdt=Penvdt[currentpoint];
  260. cpval=Penvval[currentpoint];
  261. old_mod_state = mod_state;
  262. }
  263. if (!altdown || !ctrldown) {
  264. const int dy=(int)((cpy-y_)/3.0);
  265. const int newval=limit(cpval+dy, 0, 127);
  266. Penvval[currentpoint]=newval;
  267. oscWrite(to_s("Penvval")+to_s(currentpoint), "c", newval);
  268. oscWrite("Penvval","");
  269. }
  270. if (!altdown || ctrldown) {
  271. const int dx=(int)((x_-cpx)*0.1);
  272. const int newdt=limit(cpdt+dx,0,127);
  273. if(currentpoint!=0)
  274. Penvdt[currentpoint]=newdt;
  275. else
  276. Penvdt[currentpoint]=0;
  277. oscWrite(to_s("Penvdt")+to_s(currentpoint), "c", newdt);
  278. oscWrite("Penvdt","");
  279. }
  280. redraw();
  281. if(pair)
  282. pair->redraw();
  283. return 1;
  284. }
  285. }
  286. // Needed to propagate undo/redo keys.
  287. return 0;
  288. }
  289. void EnvelopeFreeEdit::update(void)
  290. {
  291. oscWrite("Penvpoints");
  292. oscWrite("Penvdt");
  293. oscWrite("Penvval");
  294. oscWrite("Penvsustain");
  295. }
  296. void EnvelopeFreeEdit::rebase(std::string new_base)
  297. {
  298. osc->renameLink(loc+"Penvpoints", new_base+"Penvpoints", this);
  299. osc->renameLink(loc+"Penvdt", new_base+"Penvdt", this);
  300. osc->renameLink(loc+"Penvval", new_base+"Penvval", this);
  301. osc->renameLink(loc+"Penvsustain", new_base+"Penvsustain", this);
  302. for(int i=0; i<MAX_ENVELOPE_POINTS; ++i) {
  303. string dt = string("Penvdt") + to_s(i);
  304. string val = string("Penvval") + to_s(i);
  305. osc->renameLink(loc+dt, new_base+dt, this);
  306. osc->renameLink(loc+val, new_base+val, this);
  307. }
  308. loc = new_base;
  309. update();
  310. }