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.

433 lines
11KB

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