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.

363 lines
8.8KB

  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 "Timeline.H"
  19. #include "Tempo_Track.H"
  20. #include "Time_Track.H"
  21. #include "Audio_Track.H"
  22. #include <FL/Fl_Scrollbar.H>
  23. void
  24. cb_hscroll ( Fl_Widget *w, void *v )
  25. {
  26. Scalebar *sb = (Scalebar*)w;
  27. if ( sb->zoom_changed() )
  28. {
  29. timeline->fpp = sb->zoom() * 1;
  30. int maxx = timeline->ts_to_x( timeline->length );
  31. sb->range( 0, maxx );
  32. timeline->redraw();
  33. }
  34. else
  35. {
  36. timeline->position( sb->value() );
  37. }
  38. printf( "%lu\n", timeline->xoffset );
  39. }
  40. void
  41. cb_vscroll ( Fl_Widget *w, void *v )
  42. {
  43. Fl_Scrollbar *sb = (Fl_Scrollbar*)w;
  44. timeline->tracks->position( timeline->tracks->x(), (timeline->rulers->y() + timeline->rulers->h()) - sb->value() );
  45. timeline->yposition = sb->value();
  46. // timeline->vscroll->range( 0, timeline->tracks->h() - timeline->h() - timeline->rulers->h() );
  47. sb->value( sb->value(), 30, 0, min( timeline->tracks->h(), timeline->tracks->h() - timeline->h() - timeline->rulers->h() ) );
  48. timeline->damage( FL_DAMAGE_SCROLL );
  49. }
  50. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Group( X, Y, W, H, L )
  51. {
  52. xoffset = 0;
  53. {
  54. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  55. o->range( 0, 48000 * 2 );
  56. o->zoom_range( 2, 8192 );
  57. o->zoom( 256 );
  58. o->type( FL_HORIZONTAL );
  59. o->callback( cb_hscroll, 0 );
  60. hscroll = o;
  61. }
  62. {
  63. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  64. o->type( FL_VERTICAL );
  65. o->step( 10 );
  66. o->callback( cb_vscroll, 0 );
  67. vscroll = o;
  68. }
  69. {
  70. Fl_Pack *o = new Fl_Pack( X, Y, W - vscroll->w(), H - hscroll->h(), "rulers" );
  71. o->type( Fl_Pack::VERTICAL );
  72. {
  73. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  74. o->color( FL_RED );
  75. o->add( new Tempo_Point( 0, 120 ) );
  76. o->add( new Tempo_Point( 56000, 250 ) );
  77. tempo_track = o;
  78. o->end();
  79. }
  80. {
  81. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  82. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  83. o->add( new Time_Point( 0, 4, 4 ) );
  84. o->add( new Time_Point( 345344, 6, 8 ) );
  85. time_track = o;
  86. o->end();
  87. }
  88. o->size( o->w(), o->child( 0 )->h() * o->children() );
  89. rulers = o;
  90. o->end();
  91. }
  92. {
  93. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  94. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  95. sample_rate = 44100;
  96. fpp = 256;
  97. _beats_per_minute = 120;
  98. length = sample_rate * 60 * 2;
  99. {
  100. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  101. o->type( Fl_Pack::VERTICAL );
  102. o->spacing( 10 );
  103. Track *l = NULL;
  104. for ( int i = 16; i--; )
  105. {
  106. Track *o = new Audio_Track( 0, 0, 800, 100 );
  107. o->prev( l );
  108. if ( l )
  109. l->next( o );
  110. l = o;
  111. o->end();
  112. }
  113. tracks = o;
  114. o->end();
  115. }
  116. /* scroll = o; */
  117. /* o->end(); */
  118. }
  119. vscroll->range( 0, tracks->h() );
  120. redraw();
  121. end();
  122. }
  123. float
  124. Timeline::beats_per_minute ( nframes_t when ) const
  125. {
  126. return tempo_track->beats_per_minute( when );
  127. }
  128. int
  129. Timeline::beats_per_bar ( nframes_t when ) const
  130. {
  131. time_sig t = time_track->time( when );
  132. return t.beats_per_bar;
  133. }
  134. void
  135. Timeline::beats_per_minute ( nframes_t when, float bpm )
  136. {
  137. tempo_track->add( new Tempo_Point( when, bpm ) );
  138. }
  139. /* draw appropriate measure lines inside the given bounding box */
  140. void
  141. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  142. {
  143. fl_line_style( FL_DASH, 2 );
  144. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  145. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  146. // int measure = ts_to_x( sample_rate * 60 / beats_per_minute() );
  147. int measure;
  148. for ( int x = X; x < X + W; ++x )
  149. {
  150. measure = ts_to_x( (double)(sample_rate * 60) / beats_per_minute( x_to_ts( x ) + xoffset ));
  151. const int abs_x = ts_to_x( xoffset ) + x;
  152. if ( 0 == abs_x % measure )
  153. {
  154. int bpb = beats_per_bar( x_to_ts( x ) + xoffset );
  155. if ( 0 == (abs_x / measure) % bpb )
  156. {
  157. if ( measure * bpb < 8 )
  158. break;
  159. fl_color( bar );
  160. }
  161. else
  162. {
  163. if ( measure < 8 )
  164. continue;
  165. fl_color( beat );
  166. }
  167. fl_line( x, Y, x, Y + H );
  168. }
  169. }
  170. fl_line_style( FL_SOLID, 0 );
  171. }
  172. void
  173. Timeline::position ( int X )
  174. {
  175. _old_xposition = xoffset;
  176. xoffset = x_to_ts( X );
  177. damage( FL_DAMAGE_SCROLL );
  178. }
  179. void
  180. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  181. {
  182. Timeline *tl = (Timeline *)v;
  183. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  184. fl_push_clip( X, Y, W, H );
  185. fl_color( rand() );
  186. fl_rectf( X, Y, X + W, Y + H );
  187. tl->draw_child( *tl->tracks );
  188. tl->draw_child( *tl->rulers );
  189. fl_pop_clip();
  190. }
  191. void
  192. Timeline::draw ( void )
  193. {
  194. int X, Y, W, H;
  195. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  196. Y = tracks->y();
  197. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  198. H = tracks->h();
  199. /* fl_color( FL_RED ); */
  200. /* fl_rect( X, Y, X + W, Y + H ); */
  201. if ( damage() & FL_DAMAGE_ALL )
  202. // ( damage() & ( FL_DAMAGE_CHILD | FL_DAMAGE_SCROLL ) ) )
  203. {
  204. draw_box( box(), x(), y(), w(), h(), color() );
  205. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  206. draw_child( *rulers );
  207. fl_pop_clip();
  208. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - hscroll->h() );
  209. draw_child( *tracks );
  210. fl_pop_clip();
  211. draw_child( *hscroll );
  212. draw_child( *vscroll );
  213. return;
  214. }
  215. if ( damage() & FL_DAMAGE_CHILD )
  216. {
  217. /* if ( damage() & FL_DAMAGE_SCROLL ) */
  218. /* fl_push_no_clip(); */
  219. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  220. update_child( *rulers );
  221. fl_pop_clip();
  222. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  223. update_child( *tracks );
  224. fl_pop_clip();
  225. update_child( *hscroll );
  226. update_child( *vscroll );
  227. /* if ( damage() & FL_DAMAGE_SCROLL ) */
  228. /* fl_pop_clip(); */
  229. }
  230. if ( damage() & FL_DAMAGE_SCROLL )
  231. {
  232. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  233. int dy = _old_yposition - yposition;
  234. if ( ! dy )
  235. fl_scroll( X, rulers->y(), W, rulers->h(), dx, 0, draw_clip, this );
  236. Y = rulers->y() + rulers->h();
  237. H = h() - rulers->h() - hscroll->h();
  238. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  239. _old_xposition = xoffset;
  240. _old_yposition = yposition;
  241. }
  242. }
  243. int
  244. Timeline::handle ( int m )
  245. {
  246. switch ( m )
  247. {
  248. case FL_MOUSEWHEEL:
  249. {
  250. if ( hscroll->handle( m ) )
  251. return 1;
  252. return vscroll->handle( m );
  253. }
  254. default:
  255. return Fl_Group::handle( m );
  256. }
  257. }