Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
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.

186 lines
4.4KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #include <stdio.h>
  19. #include <FL/Enumerations.H>
  20. #include <FL/Fl.H>
  21. #include "Waveform.H"
  22. #include <math.h>
  23. extern Fl_Color velocity_colors[];
  24. Waveform::Waveform ( int X, int Y, int W, int H, const char *L ) : Fl_Widget( X, Y, W, H, L )
  25. {
  26. _scale = 1;
  27. }
  28. int measure = 50;
  29. void
  30. Waveform::draw ( void )
  31. {
  32. // draw_box( FL_PLASTIC_UP_BOX, x(), y(), w(), h(), color() );
  33. /* fl_color( fl_lighter( color() ) ); */
  34. /* for ( int nx = x(); nx < x() + w(); ++nx ) */
  35. /* if ( ! (nx % measure) ) */
  36. /* fl_line( nx, y(), nx, y() + h() ); */
  37. int X, Y, W, H;
  38. fl_clip_box( x(), y(), w(), h(), X, Y, W, H );
  39. draw( X, y(), W, h() );
  40. }
  41. void
  42. Waveform::draw ( int X, int Y, int W, int H )
  43. {
  44. fl_push_clip( X, Y, W, H );
  45. fl_push_matrix();
  46. int j;
  47. int start = (_start + (X - x())) * 2;
  48. j = 0;
  49. for ( int x = X; x < X + W; ++x )
  50. {
  51. float lo = _peaks[ start + j++ ];
  52. float hi = _peaks[ start + j++ ];
  53. int mid = Y + (H / 2);
  54. // FIXME: cache this stuff.
  55. // fl_color( fl_color_average( selection_color(), fl_contrast( fl_darker( FL_BLUE ), selection_color() ), fabs( hi - lo ) ) );
  56. fl_color( fl_color_average( FL_RED, selection_color(), fabs( hi - lo ) ) );
  57. hi *= _scale;
  58. lo *= _scale;
  59. if ( lo < -1.0 || hi > 1.0 )
  60. fl_color( FL_RED );
  61. fl_line( x, mid + (H / 2 * lo), x, mid + (H / 2 * hi) );
  62. }
  63. fl_color( fl_darker( fl_darker( selection_color() ) ) );
  64. fl_line_style( FL_SOLID, 2 );
  65. fl_begin_line();
  66. j = 0;
  67. for ( int x = X; x < X + W; ++x )
  68. {
  69. float v = _peaks[ start + j ] * _scale;
  70. j += 2;
  71. fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v ));
  72. }
  73. fl_end_line();
  74. fl_begin_line();
  75. j = 1;
  76. for ( int x = X; x < X + W; ++x )
  77. {
  78. float v = _peaks[ start + j ] * _scale;
  79. j += 2;
  80. fl_vertex( x, Y + (H / 2) + ((float)H / 2 * v ));
  81. }
  82. fl_end_line();
  83. fl_line_style( FL_SOLID, 0 );
  84. fl_pop_matrix();
  85. fl_pop_clip();
  86. }
  87. void
  88. Waveform::downsample ( int s, int e, float *mhi, float *mlo )
  89. {
  90. *mhi = -1.0;
  91. *mlo = 1.0;
  92. int start = s * 2;
  93. int end = e * 2;
  94. for ( int j = start; j < end; )
  95. {
  96. float lo = _peaks[ j++ ];
  97. float hi = _peaks[ j++ ];
  98. if ( hi > *mhi )
  99. *mhi = hi;
  100. if ( lo < *mlo )
  101. *mlo = lo;
  102. }
  103. }
  104. void
  105. Waveform::normalize ( void )
  106. {
  107. /* float mhi = -1.0; */
  108. /* float mlo = 1.0; */
  109. float mhi, mlo;
  110. downsample( _start, _end, &mhi, &mlo );
  111. /* int start = _start * 2; */
  112. /* int end = _end * 2; */
  113. /* for ( int j = start; j < end; ) */
  114. /* { */
  115. /* float lo = _peaks[ j++ ]; */
  116. /* float hi = _peaks[ j++ ]; */
  117. /* if ( hi > mhi ) */
  118. /* mhi = hi; */
  119. /* if ( lo < mlo ) */
  120. /* mlo = lo; */
  121. /* } */
  122. _scale = 1.0f / (float)mhi;
  123. if ( _scale * mlo < -1.0 )
  124. _scale = 1 / fabs( mlo );
  125. _scale = fabs( _scale );
  126. // printf( "scale = %f, hi=%f, lo=%f\n", _scale, mhi, mlo );
  127. redraw();
  128. }