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 3.8KB

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