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.

153 lines
4.1KB

  1. #pragma once
  2. #include "Fl_Osc_Pane.H"
  3. #include <FL/Fl_Box.H>
  4. #include <FL/fl_draw.H>
  5. #include "Fl_Osc_Widget.H"
  6. #include "Fl_Osc_Pane.H"
  7. #include "Fl_Osc_Interface.h"
  8. #include "common.H"
  9. #include <cassert>
  10. #include <cstdio>
  11. #include "../globals.h"
  12. class Fl_Osc_Group;
  13. //Consider merging in Fl_OscilSpectrum
  14. class Fl_Oscilloscope : public Fl_Box, public Fl_Osc_Widget
  15. {
  16. public:
  17. Fl_Oscilloscope(int x,int y, int w, int h, const char *label=0)
  18. :Fl_Box(x,y,w,h,label), Fl_Osc_Widget(this), smps(0), oscilsize(0)
  19. {
  20. phase=64;
  21. box(FL_FLAT_BOX);
  22. bkgnd = fl_color_average( FL_BLACK, FL_BACKGROUND_COLOR, 0.5 );
  23. }
  24. ~Fl_Oscilloscope(void)
  25. {
  26. delete[] smps;
  27. }
  28. void init(bool base_waveform_p)
  29. {
  30. ext = (base_waveform_p ? "base-waveform": "waveform");
  31. osc->createLink("/oscilsize", this);
  32. osc->requestValue("/oscilsize");
  33. assert(osc);
  34. oscRegister(ext.c_str());
  35. }
  36. void update(void)
  37. {
  38. oscWrite(ext);
  39. }
  40. virtual void OSC_value(int smp)
  41. {
  42. if(smp == oscilsize)
  43. return;
  44. oscilsize = smp;
  45. delete []smps;
  46. smps = new float[oscilsize];
  47. memset(smps, 0, oscilsize*sizeof(float));
  48. }
  49. virtual void OSC_value(unsigned N, void *data) override
  50. {
  51. if(oscilsize == 0)
  52. OSC_value((int)N/4);
  53. assert(N==(unsigned)(oscilsize*4));
  54. memcpy(smps, data, N);
  55. //normalize
  56. float max=0;
  57. for (int i=0;i<oscilsize;i++)
  58. if(max<fabs(smps[i]))
  59. max=fabs(smps[i]);
  60. if (max<0.00001) max=1.0;
  61. max *= -1.05;
  62. for(int i=0; i < oscilsize; ++i)
  63. smps[i] /= max;
  64. //Get widget to redraw new data
  65. redraw();
  66. }
  67. void draw(void)
  68. {
  69. int ox=x(),oy=y(),lx=w(),ly=h()-1;
  70. if (damage()!=1) {
  71. fl_color(bkgnd);
  72. fl_rectf(ox,oy,lx,ly);
  73. }
  74. //draw
  75. fl_line_style(FL_DASH);
  76. if (this->active_r()) fl_color(this->parent()->labelcolor());
  77. else fl_color(this->parent()->color());
  78. int GX=16;if (lx<GX*3) GX=-1;
  79. for (int i=1;i<GX;i++){
  80. int tmp=(int)(lx/(float)GX*i);
  81. fl_line(ox+tmp,oy+2,ox+tmp,oy+ly-2);
  82. }
  83. int GY=8; 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 function
  89. fl_line_style(0,1);
  90. fl_line(ox+2,oy+ly/2,ox+lx-2,oy+ly/2);
  91. if (this->active_r()) fl_color(this->parent()->selection_color());
  92. else fl_color(this->parent()->labelcolor());
  93. fl_color( fl_color_add_alpha( fl_color(), 127 ) );
  94. if(smps) {
  95. int lw=2;
  96. fl_line_style(FL_SOLID,lw);
  97. fl_begin_line();
  98. double ph=((phase-64.0)/128.0*oscilsize+oscilsize);
  99. for (int i=1;i<lx;i++){
  100. int k2=(oscilsize*i/lx)+ph;
  101. fl_vertex(i+ox,(smps[k2%oscilsize]+1)*(ly-1)/2+oy+0.5);
  102. }
  103. fl_end_line();
  104. }
  105. // Erase stray pixels on margin
  106. fl_color(bkgnd);
  107. fl_line_style(FL_SOLID,1);
  108. fl_rect(ox-1,oy-1,lx+2,ly+2);
  109. fl_line_style(FL_SOLID,0);
  110. }
  111. //allows UI to manipuate phase of displayed waveform
  112. int phase;
  113. private:
  114. Fl_Osc_Pane *fetch_osc_pane(Fl_Widget *w)
  115. {
  116. if(!w)
  117. return NULL;
  118. Fl_Osc_Pane *pane = dynamic_cast<Fl_Osc_Pane*>(w->parent());
  119. if(pane)
  120. return pane;
  121. return fetch_osc_pane(w->parent());
  122. }
  123. float *smps;
  124. int oscilsize;
  125. Fl_Color bkgnd;
  126. };