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.

243 lines
6.9KB

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