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.

387 lines
9.5KB

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