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.

229 lines
6.3KB

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