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.

Fl_Oscilloscope.h 4.2KB

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