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.

233 lines
6.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. /* Digital clock widget to show points on the timeline. May be
  19. switched between Bar Beat Tick and Wallclock displays */
  20. #include <FL/Fl_Widget.H>
  21. #include <FL/fl_draw.H>
  22. #include <FL/Fl.H>
  23. #include "Timeline.H"
  24. #include "types.h"
  25. const float CLOCK_UPDATE_FREQ = 0.06f;
  26. /* TODO: frames per second? */
  27. class Clock : public Fl_Widget
  28. {
  29. /* not permitted */
  30. Clock ( const Clock &rhs );
  31. Clock & operator = ( const Clock &rhs );
  32. nframes_t _when;
  33. nframes_t *_v;
  34. static void
  35. update_cb ( void *v )
  36. {
  37. ((Clock*)v)->update_cb();
  38. }
  39. void
  40. update_cb ( void )
  41. {
  42. Fl::repeat_timeout( CLOCK_UPDATE_FREQ, update_cb, this );
  43. set( *_v );
  44. }
  45. public:
  46. enum { HMS = 0, BBT, Timecode, Sample, TYPE_MAX };
  47. static void
  48. frame_to_Timecode ( char *dst, int n, nframes_t frame )
  49. {
  50. float S = (double)frame / timeline->sample_rate();
  51. int M = S / 60; S -= M * 60;
  52. int H = M / 60; M -= H * 60;
  53. int HS = ((int)(S * 100)) - (((int)S) * 100);
  54. snprintf( dst, n, "%02d:%02d:%02.0f:%02d", H, M, S, HS );
  55. }
  56. static void
  57. frame_to_HMS ( char *dst, int n, nframes_t frame )
  58. {
  59. float S = (double)frame / timeline->sample_rate();
  60. int M = S / 60; S -= M * 60;
  61. int H = M / 60; M -= H * 60;
  62. snprintf( dst, n, "%02d:%02d:%05.3f", H, M, S );
  63. }
  64. static void
  65. frame_to_Sample ( char *dst, int n, nframes_t frame )
  66. {
  67. snprintf( dst, n, "%lu", (unsigned long)frame );
  68. }
  69. static void
  70. frame_to_BBT ( char *dst, int n, nframes_t frame )
  71. {
  72. snprintf( dst, n, "unimplemented" );
  73. }
  74. Clock ( int X, int Y, int W, int H, const char *L=0 )
  75. : Fl_Widget( X, Y, W, H, L )
  76. {
  77. _when = 0;
  78. _v = 0;
  79. box( FL_BORDER_BOX );
  80. type( HMS );
  81. /* force size */
  82. size( 170, 40 );
  83. }
  84. ~Clock ( )
  85. {
  86. Fl::remove_timeout( update_cb );
  87. }
  88. void run ( nframes_t *v )
  89. {
  90. _v = v;
  91. Fl::add_timeout( CLOCK_UPDATE_FREQ, update_cb, this );
  92. }
  93. void set ( nframes_t frame )
  94. {
  95. if ( _when != frame )
  96. {
  97. _when = frame;
  98. redraw();
  99. }
  100. }
  101. void draw ( void )
  102. {
  103. draw_box();
  104. fl_push_clip( x(), y(), w(), h() );
  105. char buf[15];
  106. *buf = '\0';
  107. switch ( type() )
  108. {
  109. case HMS:
  110. frame_to_HMS( buf, sizeof( buf ), _when );
  111. break;
  112. case BBT:
  113. frame_to_BBT( buf, sizeof( buf ), _when );
  114. break;
  115. case Timecode:
  116. frame_to_Timecode( buf, sizeof( buf ), _when );
  117. break;
  118. case Sample:
  119. frame_to_Sample( buf, sizeof( buf ), _when );
  120. break;
  121. default:
  122. printf( "error: invalid clock type\n" );
  123. }
  124. fl_font( FL_COURIER, 24 );
  125. Fl_Color c = FL_GREEN;
  126. fl_color( c );
  127. const int dx = x() + Fl::box_dx( box() );
  128. const int dy = y() + Fl::box_dy( box() );
  129. const int dw = w() - Fl::box_dw( box() );
  130. const int dh = h() - Fl::box_dh( box() );
  131. fl_draw( buf, dx, dy, dw, dh - 9, FL_ALIGN_CENTER );
  132. for ( int i = strlen( buf ); i--; )
  133. if ( isdigit( buf[ i ] ) )
  134. buf[ i ] = ' ';
  135. fl_color( fl_darker( c ) );
  136. fl_draw( buf, dx, dy, dw, dh - 9, FL_ALIGN_CENTER );
  137. fl_font( FL_HELVETICA, 9 );
  138. const char *types[] = { "HMS", "BBT", "Timecode", "Sample" };
  139. fl_color( FL_CYAN );
  140. switch ( type() )
  141. {
  142. case Timecode:
  143. snprintf( buf, sizeof( buf ), "%.1f", 30.0 );
  144. fl_draw( buf, dx, dy, dw, dh, FL_ALIGN_BOTTOM );
  145. break;
  146. case Sample:
  147. snprintf( buf, sizeof( buf ), "%lu", (unsigned long)timeline->sample_rate() );
  148. fl_draw( buf, dx, dy, dw, dh, FL_ALIGN_BOTTOM );
  149. break;
  150. default:
  151. break;
  152. }
  153. const char *s = types[ type() ];
  154. fl_color( FL_RED );
  155. fl_draw( s, dx + 4, dy, dw, dh, (Fl_Align)( FL_ALIGN_LEFT | FL_ALIGN_BOTTOM ) );
  156. if ( label() )
  157. fl_draw( label(), dx, dy, dw, dh, (Fl_Align)( FL_ALIGN_RIGHT | FL_ALIGN_BOTTOM ) );
  158. fl_pop_clip();
  159. }
  160. int handle ( int m )
  161. {
  162. if ( m == FL_PUSH )
  163. {
  164. int t = type() + 1;
  165. if ( t >= TYPE_MAX )
  166. t = 0;
  167. type( t );
  168. redraw();
  169. return 0;
  170. }
  171. return 0;
  172. }
  173. };