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.

306 lines
8.5KB

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