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.

810 lines
19KB

  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 "Control_Track.H"
  23. #include <FL/Fl_Scrollbar.H>
  24. #include "Ruler_Track.H"
  25. // #include <FL/Fl_Image.H>
  26. // #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  27. #include "Track_Header.H"
  28. const float UPDATE_FREQ = 0.02f;
  29. #include "Playback_DS.H"
  30. #include "Transport.H"
  31. void
  32. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  33. {
  34. ((Timeline*)v)->cb_scroll( w );
  35. }
  36. void
  37. Timeline::cb_scroll ( Fl_Widget *w )
  38. {
  39. if ( w == vscroll )
  40. {
  41. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  42. yposition( vscroll->value() );
  43. int rh = h() - rulers->h();
  44. vscroll->value( vscroll->value(), 30, 0, max( tracks->h() - rh, rh) );
  45. }
  46. else
  47. {
  48. if ( hscroll->zoom_changed() )
  49. {
  50. _fpp = hscroll->zoom() * 1;
  51. int maxx = ts_to_x( _length );
  52. hscroll->range( 0, maxx );
  53. redraw();
  54. }
  55. else
  56. {
  57. xposition( hscroll->value() );
  58. }
  59. }
  60. }
  61. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  62. {
  63. box( FL_FLAT_BOX );
  64. xoffset = 0;
  65. _enable_measure_lines = true;
  66. X = Y = 0;
  67. {
  68. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  69. o->range( 0, 48000 * 300 );
  70. o->zoom_range( 2, 8192 );
  71. o->zoom( 256 );
  72. o->type( FL_HORIZONTAL );
  73. o->callback( cb_scroll, this );
  74. hscroll = o;
  75. }
  76. {
  77. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  78. o->type( FL_VERTICAL );
  79. // o->step( 10 );
  80. o->callback( cb_scroll, this );
  81. vscroll = o;
  82. }
  83. {
  84. Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  85. o->type( Fl_Pack::VERTICAL );
  86. {
  87. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  88. o->color( FL_RED );
  89. o->add( new Tempo_Point( 0, 120 ) );
  90. o->add( new Tempo_Point( 56000, 250 ) );
  91. o->label( "Tempo" );
  92. o->align( FL_ALIGN_LEFT );
  93. tempo_track = o;
  94. // o->end();
  95. }
  96. {
  97. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  98. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  99. o->add( new Time_Point( 0, 4, 4 ) );
  100. o->add( new Time_Point( 345344, 6, 8 ) );
  101. o->label( "Time" );
  102. o->align( FL_ALIGN_LEFT );
  103. time_track = o;
  104. // o->end();
  105. }
  106. {
  107. Ruler_Track *o = new Ruler_Track( 0, 24, 800, 24 );
  108. o->color( FL_GREEN );
  109. /* o->add( new Time_Point( 0, 4, 4 ) ); */
  110. /* o->add( new Time_Point( 345344, 6, 8 ) ); */
  111. o->label( "Ruler" );
  112. o->align( FL_ALIGN_LEFT );
  113. ruler_track = o;
  114. // o->end();
  115. }
  116. o->size( o->w(), o->child( 0 )->h() * o->children() );
  117. rulers = o;
  118. o->end();
  119. }
  120. {
  121. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  122. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  123. _sample_rate = 44100;
  124. _fpp = 256;
  125. _length = _sample_rate * 60 * 2;
  126. {
  127. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  128. o->type( Fl_Pack::VERTICAL );
  129. o->spacing( 0 );
  130. /* for ( int i = 1; i--; ) */
  131. /* { */
  132. /* // Track_Header *t = new Track_Header( 0, 0, W, 75 ); */
  133. /* Track_Header *t = new Track_Header( 0, 0, W, 30 ); */
  134. /* Track *o = new Audio_Track( 0, 0, 1, 100 ); */
  135. /* t->track( o ); */
  136. /* t->add( new Audio_Track( 0, 0, 1, 100 ) ); */
  137. /* t->add( new Audio_Track( 0, 0, 1, 100 ) ); */
  138. /* t->add_control( new Control_Track( 0, 0, 1, 100 ) ); */
  139. /* t->color( (Fl_Color)rand() ); */
  140. /* } */
  141. tracks = o;
  142. o->end();
  143. }
  144. /* scroll = o; */
  145. /* o->end(); */
  146. }
  147. /* make sure scrollbars are on top */
  148. add( vscroll );
  149. add( hscroll );
  150. vscroll->range( 0, tracks->h() );
  151. redraw();
  152. end();
  153. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  154. }
  155. float
  156. Timeline::beats_per_minute ( nframes_t when ) const
  157. {
  158. return tempo_track->beats_per_minute( when );
  159. }
  160. int
  161. Timeline::beats_per_bar ( nframes_t when ) const
  162. {
  163. time_sig t = time_track->time( when );
  164. return t.beats_per_bar;
  165. }
  166. void
  167. Timeline::beats_per_minute ( nframes_t when, float bpm )
  168. {
  169. tempo_track->add( new Tempo_Point( when, bpm ) );
  170. }
  171. #if 0
  172. struct BBT
  173. {
  174. int bar, beat, tick;
  175. BBT ( int bar, int beat, int tick ) : bar( bar ), beat( beat ), tick( tick )
  176. {
  177. }
  178. };
  179. /** returns the BBT value for timestamp /when/ by examining the tempo/time maps */
  180. BBT
  181. Timeline::bbt ( nframes_t when )
  182. {
  183. Tempo_Track *tempo = (Tempo_Track*)rulers->child( 0 );
  184. BBT bbt;
  185. for ( list <Track_Widget *>::const_iterator i = tempo.widgets.begin();
  186. i != tempo.widgets.end(); ++i )
  187. {
  188. Tempo_Point *p = *i;
  189. }
  190. };
  191. #endif
  192. /** return the absolute pixel of the nearest measure line to /x/ */
  193. int
  194. Timeline::nearest_line ( int ix )
  195. {
  196. for ( int x = ix - 10; x < ix + 10; ++x )
  197. {
  198. const int measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  199. // const int abs_x = ts_to_x( xoffset ) + x;
  200. if ( 0 == x % measure )
  201. return x;
  202. }
  203. return -1;
  204. }
  205. /** draw appropriate measure lines inside the given bounding box */
  206. /* FIXME: this function *really* needs to be optimized. Currently it
  207. searched both the time and tempo lists once for every horiontal
  208. pixel and performs a number of calculations--this is slow. */
  209. void
  210. Timeline::draw_measure ( int X, int Y, int W, int H, Fl_Color color, bool BBT )
  211. {
  212. if ( ! _enable_measure_lines )
  213. return;
  214. // fl_line_style( FL_DASH, 2 );
  215. fl_line_style( FL_DASH, 0 );
  216. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  217. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  218. int measure;
  219. for ( int x = X; x < X + W; ++x )
  220. {
  221. measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ) );
  222. const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
  223. if ( 0 == abs_x % measure )
  224. {
  225. int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
  226. if ( 0 == (abs_x / measure) % bpb )
  227. {
  228. if ( measure * bpb < 8 )
  229. break;
  230. if ( BBT )
  231. {
  232. fl_font( FL_HELVETICA, 14 );
  233. fl_color( fl_lighter( color ) );
  234. char pat[40];
  235. const nframes_t ts = x_to_ts( abs_x );
  236. // if ( draw_hms )
  237. {
  238. const double seconds = (double)ts / _sample_rate;
  239. int S = (int)seconds;
  240. int M = S / 60; S -= M * 60;
  241. int H = M / 60; M -= H * 60;
  242. snprintf( pat, sizeof( pat ), "%d:%d:%d", H, M, S );
  243. }
  244. // else if ( draw_bbt )
  245. {
  246. /* const int bar = */
  247. /* const int beat; */
  248. /* const int tick = 0; */
  249. /* snprintf( pat, sizeof( pat ), "%d:%d:%d", bar, beat, tick ); */
  250. }
  251. fl_draw( pat, x, Y + 14 );
  252. }
  253. fl_color( bar );
  254. }
  255. else
  256. {
  257. if ( measure < 8 )
  258. continue;
  259. fl_color( beat );
  260. }
  261. fl_line( x, Y, x, Y + H );
  262. }
  263. }
  264. fl_line_style( FL_SOLID, 0 );
  265. }
  266. void
  267. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  268. {
  269. draw_measure( X, Y, W, H, color, false );
  270. }
  271. /** just like draw mesure lines except that it also draws the BBT values. */
  272. void
  273. Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color )
  274. {
  275. draw_measure( X, Y, W, H, color, true );
  276. }
  277. void
  278. Timeline::xposition ( int X )
  279. {
  280. // _old_xposition = xoffset;
  281. xoffset = x_to_ts( X );
  282. damage( FL_DAMAGE_SCROLL );
  283. }
  284. void
  285. Timeline::yposition ( int Y )
  286. {
  287. // _old_yposition = _yposition;
  288. _yposition = Y;
  289. damage( FL_DAMAGE_SCROLL );
  290. }
  291. void
  292. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  293. {
  294. Timeline *tl = (Timeline *)v;
  295. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  296. fl_push_clip( X, Y, W, H );
  297. fl_color( rand() );
  298. fl_rectf( X, Y, X + W, Y + H );
  299. tl->draw_child( *tl->rulers );
  300. /* headers */
  301. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  302. tl->draw_child( *tl->tracks );
  303. fl_pop_clip();
  304. /* track bodies */
  305. 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() );
  306. tl->draw_child( *tl->tracks );
  307. fl_pop_clip();
  308. // tl->draw_child( *tl->tracks );
  309. fl_pop_clip();
  310. }
  311. static unsigned char *rect_image;
  312. void
  313. Timeline::draw ( void )
  314. {
  315. int X, Y, W, H;
  316. int bdx = 0;
  317. int bdw = 0;
  318. /* FIXME: hack to avoid clobbering the box corners of tracks. */
  319. if ( tracks->children() )
  320. {
  321. bdx = Fl::box_dx( tracks->child( 0 )->box() );
  322. bdw = Fl::box_dw( tracks->child( 0 )->box() );
  323. }
  324. X = tracks->x() + bdx + 1;
  325. Y = tracks->y();
  326. W = tracks->w() - bdw - 1;
  327. H = tracks->h();
  328. /* if ( damage() & FL_DAMAGE_USER1 ) */
  329. /* { */
  330. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  331. /* Rectangle &r = _selection; */
  332. /* make_current(); */
  333. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  334. /* return; */
  335. /* } */
  336. if ( (damage() & FL_DAMAGE_ALL)
  337. ||
  338. damage() & FL_DAMAGE_EXPOSE )
  339. {
  340. draw_box( box(), 0, 0, w(), h(), color() );
  341. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  342. draw_child( *rulers );
  343. fl_pop_clip();
  344. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  345. draw_child( *tracks );
  346. fl_pop_clip();
  347. draw_child( *hscroll );
  348. draw_child( *vscroll );
  349. redraw_overlay();
  350. /* Rectangle &r = _selection; */
  351. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  352. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  353. /* bi.color_average( FL_BLACK, 0.50f ); */
  354. /* bi.draw( r.x, r.y ); */
  355. /* delete[] data; */
  356. /* if ( r.w && r.h ) */
  357. /* { */
  358. /* const unsigned char data[] = { 0, 127, 0, 96, */
  359. /* 0, 96, 0, 127 }; */
  360. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  361. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  362. /* bi2->draw( r.x, r.y ); */
  363. /* delete bi2; */
  364. /* } */
  365. return;
  366. }
  367. if ( damage() & FL_DAMAGE_CHILD )
  368. {
  369. // draw_box( box(), 0, 0, w(), h(), color() );
  370. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  371. update_child( *rulers );
  372. fl_pop_clip();
  373. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  374. update_child( *tracks );
  375. fl_pop_clip();
  376. update_child( *hscroll );
  377. update_child( *vscroll );
  378. }
  379. if ( damage() & FL_DAMAGE_SCROLL )
  380. {
  381. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  382. int dy = _old_yposition - _yposition;
  383. if ( ! dy )
  384. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  385. Y = rulers->y() + rulers->h();
  386. H = h() - rulers->h() - hscroll->h();
  387. if ( dy == 0 )
  388. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  389. else
  390. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  391. _old_xposition = xoffset;
  392. _old_yposition = _yposition;
  393. }
  394. }
  395. void
  396. Timeline::draw_playhead ( void )
  397. {
  398. int x = ( ts_to_x( transport.frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track_Header::width();
  399. if ( x < tracks->x() + Track_Header::width() || x > tracks->x() + tracks->w() )
  400. return;
  401. fl_color( FL_RED );
  402. int y = rulers->y() + rulers->h();
  403. int h = this->h() - hscroll->h();
  404. fl_rectf( x - 2, y, 5, 2 );
  405. fl_line( x, y, x, h );
  406. }
  407. void
  408. Timeline::redraw_playhead ( void )
  409. {
  410. static nframes_t last_playhead = -1;
  411. if ( last_playhead != transport.frame )
  412. {
  413. redraw_overlay();
  414. last_playhead = transport.frame;
  415. }
  416. }
  417. /** called so many times a second to redraw the playhead etc. */
  418. void
  419. Timeline::update_cb ( void *arg )
  420. {
  421. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  422. Timeline *tl = (Timeline *)arg;
  423. tl->redraw_playhead();
  424. }
  425. void
  426. Timeline::draw_overlay ( void )
  427. {
  428. draw_playhead();
  429. if ( ! ( _selection.w && _selection.h ) )
  430. return;
  431. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  432. const Rectangle &r = _selection;
  433. fl_color( FL_BLACK );
  434. fl_line_style( FL_SOLID, 2 );
  435. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  436. fl_color( FL_MAGENTA );
  437. fl_line_style( FL_DASH, 2 );
  438. fl_rect( r.x, r.y, r.w, r.h );
  439. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  440. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  441. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  442. fl_line_style( FL_SOLID, 0 );
  443. /* const unsigned char data[] = { 127, 127, 127, 96, */
  444. /* 127, 96, 127, 40 }; */
  445. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  446. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  447. /* bi2->draw( r.x, r.y ); */
  448. /* delete bi2; */
  449. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  450. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  451. /* bi.color_average( FL_BLACK, 0.50f ); */
  452. /* bi.draw( r.x, r.y ); */
  453. /* delete[] rect_image; */
  454. /* rect_image = NULL; */
  455. fl_pop_clip();
  456. }
  457. // #include "Track_Widget.H"
  458. /** select all widgets in inside rectangle /r/ */
  459. void
  460. Timeline::select( const Rectangle &r )
  461. {
  462. const int Y = r.y;
  463. for ( int i = tracks->children(); i-- ; )
  464. {
  465. Track_Header *t = (Track_Header*)tracks->child( i );
  466. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  467. t->track()->select_range( r.x, r.w );
  468. }
  469. }
  470. int
  471. Timeline::handle ( int m )
  472. {
  473. static Drag *drag = NULL;
  474. switch ( m )
  475. {
  476. case FL_KEYBOARD:
  477. {
  478. switch ( Fl::event_key() )
  479. {
  480. case FL_Delete:
  481. {
  482. Track_Widget::delete_selected();
  483. return 1;
  484. }
  485. default:
  486. return Fl_Overlay_Window::handle( m );
  487. }
  488. return 0;
  489. }
  490. default:
  491. {
  492. int r = Fl_Overlay_Window::handle( m );
  493. if ( m != FL_RELEASE && r )
  494. return r;
  495. const int X = Fl::event_x();
  496. const int Y = Fl::event_y();
  497. switch ( m )
  498. {
  499. case FL_PUSH:
  500. {
  501. take_focus();
  502. if ( ! Fl::event_button1() )
  503. return 0;
  504. assert( ! drag );
  505. drag = new Drag( X - x(), Y - y() );
  506. _selection.x = drag->x;
  507. _selection.y = drag->y;
  508. break;
  509. }
  510. case FL_DRAG:
  511. {
  512. int ox = X - drag->x;
  513. int oy = Y - drag->y;
  514. if ( ox < 0 )
  515. _selection.x = X;
  516. if ( oy < 0 )
  517. _selection.y = Y;
  518. _selection.w = abs( ox );
  519. _selection.h = abs( oy );
  520. break;
  521. }
  522. case FL_RELEASE:
  523. {
  524. delete drag;
  525. drag = NULL;
  526. select( _selection );
  527. _selection.w = _selection.h = 0;
  528. break;
  529. }
  530. default:
  531. return 0;
  532. break;
  533. }
  534. redraw_overlay();
  535. return 1;
  536. }
  537. }
  538. }
  539. /**********/
  540. /* Engine */
  541. /**********/
  542. /** call process() on each track header */
  543. nframes_t
  544. Timeline::process ( nframes_t nframes )
  545. {
  546. for ( int i = tracks->children(); i-- ; )
  547. {
  548. Track_Header *t = (Track_Header*)tracks->child( i );
  549. t->process( nframes );
  550. }
  551. /* FIXME: BOGUS */
  552. return nframes;
  553. }
  554. /* THREAD: RT */
  555. void
  556. Timeline::seek ( nframes_t frame )
  557. {
  558. for ( int i = tracks->children(); i-- ; )
  559. {
  560. Track_Header *t = (Track_Header*)tracks->child( i );
  561. t->seek( frame );
  562. }
  563. }
  564. /* THREAD: RT */
  565. int
  566. Timeline::seek_pending ( void )
  567. {
  568. int r = 0;
  569. for ( int i = tracks->children(); i-- ; )
  570. {
  571. Track_Header *t = (Track_Header*)tracks->child( i );
  572. if ( t->playback_ds )
  573. r += t->playback_ds->buffer_percent() < 50;
  574. }
  575. }