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.

258 lines
6.8KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. Fl_Resonance_Graph.cpp - OSC Resonance Graph 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 "Fl_Resonance_Graph.H"
  11. #include <FL/Fl.H>
  12. #include <FL/fl_draw.H>
  13. #include <FL/Fl_Value_Output.H>
  14. #include <rtosc/rtosc.h>
  15. Fl_Resonance_Graph::Fl_Resonance_Graph(int x,int y, int w, int h, const char *label)
  16. :Fl_Box(x,y,w,h,label), Fl_Osc_Widget(this), khzvalue(NULL), dbvalue(NULL),
  17. oldx(-1), oldy(-1), cbwidget(NULL), applybutton(NULL), Pcenterfreq(0), Poctavesfreq(0),
  18. PmaxdB(0)
  19. {
  20. memset(Prespoints, 64, N_RES_POINTS);
  21. //Get values
  22. oscRegister("Prespoints");
  23. oscRegister("Pcenterfreq");
  24. oscRegister("Poctavesfreq");
  25. oscRegister("PmaxdB");
  26. cbwidget=NULL;
  27. applybutton=NULL;
  28. }
  29. Fl_Resonance_Graph::~Fl_Resonance_Graph(void)
  30. {
  31. }
  32. void Fl_Resonance_Graph::init(Fl_Value_Output *khzvalue_,Fl_Value_Output *dbvalue_)
  33. {
  34. khzvalue=khzvalue_;
  35. dbvalue=dbvalue_;
  36. oldx=-1;
  37. khzval=-1;
  38. }
  39. void Fl_Resonance_Graph::draw_freq_line(float freq,int type)
  40. {
  41. const float freqx=getfreqpos(freq);//XXX
  42. switch(type){
  43. case 0:fl_line_style(FL_SOLID);break;
  44. case 1:fl_line_style(FL_DOT);break;
  45. case 2:fl_line_style(FL_DASH);break;
  46. }
  47. if ((freqx>0.0)&&(freqx<1.0))
  48. fl_line(x()+(int) (freqx*w()),y(),
  49. x()+(int) (freqx*w()),y()+h());
  50. }
  51. void Fl_Resonance_Graph::draw()
  52. {
  53. const int ox=x(),oy=y(),lx=w(),ly=h();
  54. fl_color(FL_DARK1);
  55. fl_rectf(ox,oy,lx,ly);
  56. //draw the lines
  57. fl_color(FL_GRAY);
  58. fl_line_style(FL_SOLID);
  59. fl_line(ox+2,oy+ly/2,ox+lx-2,oy+ly/2);
  60. //Draw 1kHz line
  61. const float freqx=getfreqpos(1000.0);//XXX
  62. if ((freqx>0.0)&&(freqx<1.0))
  63. fl_line(ox+(int) (freqx*lx),oy,
  64. ox+(int) (freqx*lx),oy+ly);
  65. //Draw other frequency lines
  66. for (int i=1; i<10; ++i){
  67. if(i==1) {
  68. draw_freq_line(i*100.0,0);
  69. draw_freq_line(i*1000.0,0);
  70. } else
  71. if (i==5) {
  72. draw_freq_line(i*100.0,2);
  73. draw_freq_line(i*1000.0,2);
  74. } else {
  75. draw_freq_line(i*100.0,1);
  76. draw_freq_line(i*1000.0,1);
  77. }
  78. }
  79. draw_freq_line(10000.0,0);
  80. draw_freq_line(20000.0,1);
  81. //Draw dotted grid
  82. fl_line_style(FL_DOT);
  83. int GY=10;if (ly<GY*3) GY=-1;
  84. for (int i=1; i<GY; ++i){
  85. int tmp=(int)(ly/(float)GY*i);
  86. fl_line(ox+2,oy+tmp,ox+lx-2,oy+tmp);
  87. }
  88. //draw the data
  89. fl_color(FL_RED);
  90. fl_line_style(FL_SOLID,2);
  91. fl_begin_line();
  92. int oiy = ly*Prespoints[0]/128.0;//XXX easy
  93. for (int i=1; i<N_RES_POINTS; ++i){
  94. const int ix=(i*1.0/N_RES_POINTS*lx);
  95. const int iy= ly*Prespoints[i]/128.0;//XXX easy
  96. fl_vertex(ox+ix,oy+ly-oiy);
  97. oiy=iy;
  98. }
  99. fl_end_line();
  100. fl_line_style(FL_SOLID,0);
  101. }
  102. int Fl_Resonance_Graph::handle(int event)
  103. {
  104. int x_=Fl::event_x()-x();
  105. int y_=Fl::event_y()-y();
  106. if((x_>=0)&&(x_<w()) && (y_>=0)&&(y_<h())){
  107. khzvalue->value(getfreqx(x_*1.0/w())/1000.0);//XXX
  108. dbvalue->value((1.0-y_*2.0/h())*PmaxdB);//XXX
  109. }
  110. if((event==FL_PUSH)||(event==FL_DRAG)){
  111. const bool leftbutton = Fl::event_button() == FL_LEFT_MOUSE;
  112. if (x_<0) x_=0;if (y_<0) y_=0;
  113. if (x_>=w()) x_=w();if (y_>=h()-1) y_=h()-1;
  114. if ((oldx<0)||(oldx==x_)){
  115. int sn=(int)(x_*1.0/w()*N_RES_POINTS);
  116. int sp=127-(int)(y_*1.0/h()*127);
  117. if(leftbutton)
  118. setPoint(sn,sp);
  119. //oscWrite("setpoint", "ii", sn, sp);//respar->setpoint(sn,sp);//XXX easy
  120. else
  121. setPoint(sn,sp);
  122. //oscWrite("setpoint", "ii", sn, 64);//respar->setpoint(sn,64);//XXX easy
  123. } else {
  124. int x1=oldx;
  125. int x2=x_;
  126. int y1=oldy;
  127. int y2=y_;
  128. if (oldx>x_){
  129. x1=x_;y1=y_;
  130. x2=oldx;y2=oldy;
  131. }
  132. for (int i=0;i<x2-x1;i++){
  133. int sn=(int)((i+x1)*1.0/w()*N_RES_POINTS);
  134. float yy=(y2-y1)*1.0/(x2-x1)*i;
  135. int sp=127-(int)((y1+yy)/h()*127);
  136. if(leftbutton) //respar->setpoint(sn,sp);//XXX easy
  137. setPoint(sn, sp);
  138. //oscWrite("setpoint", "ii", sn, sp);
  139. else //respar->setpoint(sn,64);//XXX easy
  140. setPoint(sn, sp);
  141. //oscWrite("setpoint", "ii", sn, sp);
  142. }
  143. }
  144. oldx=x_;oldy=y_;
  145. redraw();
  146. }
  147. if(event==FL_RELEASE) {
  148. oldx=-1;
  149. if(cbwidget) {
  150. cbwidget->do_callback();
  151. if(applybutton) {
  152. applybutton->color(FL_RED);
  153. applybutton->redraw();
  154. }
  155. }
  156. }
  157. return 1;
  158. }
  159. void Fl_Resonance_Graph::setcbwidget(Fl_Widget *cbwidget,Fl_Widget *applybutton)
  160. {
  161. this->cbwidget=cbwidget;
  162. this->applybutton=applybutton;
  163. }
  164. void Fl_Resonance_Graph::update(void)
  165. {
  166. oscWrite("Prespoints");
  167. oscWrite("Pcenterfreq");
  168. oscWrite("Poctavesfreq");
  169. oscWrite("PmaxdB");
  170. }
  171. void Fl_Resonance_Graph::OSC_raw(const char *msg)
  172. {
  173. //TODO check the types (OSC regex)
  174. if(strstr(msg, "Prespoints")) {
  175. rtosc_blob_t arg = rtosc_argument(msg, 0).b;
  176. assert(arg.len == N_RES_POINTS);
  177. memcpy(Prespoints, arg.data, N_RES_POINTS);
  178. } else if(strstr(msg, "Pcenterfreq"))
  179. Pcenterfreq = rtosc_argument(msg, 0).i;
  180. else if(strstr(msg, "Poctavesfreq"))
  181. Poctavesfreq = rtosc_argument(msg, 0).i;
  182. else if(strstr(msg, "PmaxdB"))
  183. PmaxdB = rtosc_argument(msg, 0).i;
  184. else
  185. puts("I got an unknown message...");
  186. redraw();
  187. }
  188. float Fl_Resonance_Graph::getfreqx(float x) const
  189. {
  190. const float octf = powf(2.0f, getoctavesfreq());
  191. return getcenterfreq() / sqrt(octf) * powf(octf, limit(x, 0.0f, 1.0f));
  192. }
  193. /*
  194. * Get the x coordinate from frequency (used by the UI)
  195. */
  196. float Fl_Resonance_Graph::getfreqpos(float freq) const
  197. {
  198. return (logf(freq) - logf(getfreqx(0.0f))) / logf(2.0f) / getoctavesfreq();
  199. }
  200. /*
  201. * Get the center frequency of the resonance graph
  202. */
  203. float Fl_Resonance_Graph::getcenterfreq() const
  204. {
  205. return 10000.0f * powf(10, -(1.0f - Pcenterfreq / 127.0f) * 2.0f);
  206. }
  207. /*
  208. * Get the number of octave that the resonance functions applies to
  209. */
  210. float Fl_Resonance_Graph::getoctavesfreq() const
  211. {
  212. return 0.25f + 10.0f * Poctavesfreq / 127.0f;
  213. }
  214. void Fl_Resonance_Graph::setPoint(int idx, int val)
  215. {
  216. Prespoints[idx] = val;
  217. oscWrite(std::string("Prespoints")+to_s(idx), "c", val);
  218. redraw();
  219. }