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.

1154 lines
26KB

  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_Sequence.H"
  20. #include "Time_Sequence.H"
  21. #include "Audio_Sequence.H"
  22. #include "Control_Sequence.H"
  23. #include <FL/Fl_Scrollbar.H>
  24. #include "Ruler_Sequence.H"
  25. // #include <FL/Fl_Image.H>
  26. // #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  27. #include "Track.H"
  28. bool Timeline::draw_with_measure_lines = true;
  29. Timeline::snap_e Timeline::snap_to = Bars;
  30. bool Timeline::snap_magnetic = true;
  31. bool Timeline::follow_playhead = true;
  32. const float UPDATE_FREQ = 0.02f;
  33. #include "Playback_DS.H"
  34. #include "Record_DS.H"
  35. #include "Transport.H"
  36. /** return the combined height of all visible children of (veritcal)
  37. pack, /p/. This is necessary because pack sizes are adjusted only
  38. when the relevant areas are exposes. */
  39. static int
  40. pack_visible_height ( const Fl_Pack *p )
  41. {
  42. int th = 0;
  43. const Fl_Widget* const *w = p->array();
  44. for ( int i = p->children(); i--; ++w )
  45. if ( (*w)->visible() )
  46. th += (*w)->h() + p->spacing();
  47. return th;
  48. }
  49. /** recalculate the size of vertical scrolling area and inform scrollbar */
  50. void
  51. Timeline::adjust_vscroll ( void )
  52. {
  53. vscroll->value( _yposition, h() - rulers->h() - hscroll->h(), 0, pack_visible_height( tracks ) );
  54. }
  55. void
  56. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  57. {
  58. ((Timeline*)v)->cb_scroll( w );
  59. }
  60. void
  61. Timeline::cb_scroll ( Fl_Widget *w )
  62. {
  63. if ( w == vscroll )
  64. {
  65. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  66. yposition( vscroll->value() );
  67. adjust_vscroll();
  68. }
  69. else
  70. {
  71. if ( hscroll->zoom_changed() )
  72. {
  73. _fpp = hscroll->zoom();
  74. const int tw = tracks->w() - Track::width() - vscroll->w();
  75. hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( _length ) );
  76. redraw();
  77. }
  78. xposition( hscroll->value() );
  79. }
  80. }
  81. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  82. {
  83. box( FL_FLAT_BOX );
  84. xoffset = 0;
  85. _yposition = 0;
  86. _old_yposition = 0;
  87. _old_xposition = 0;
  88. X = Y = 0;
  89. p1 = p2 = 0;
  90. {
  91. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  92. o->range( 0, 48000 * 300 );
  93. // o->zoom_range( 1, 16384 );
  94. // o->zoom_range( 1, 65536 << 4 );
  95. o->zoom_range( 1, 20 );
  96. o->zoom( 8 );
  97. o->type( FL_HORIZONTAL );
  98. o->callback( cb_scroll, this );
  99. hscroll = o;
  100. }
  101. {
  102. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  103. o->type( FL_VERTICAL );
  104. o->callback( cb_scroll, this );
  105. vscroll = o;
  106. }
  107. {
  108. Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  109. o->type( Fl_Pack::VERTICAL );
  110. {
  111. Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
  112. o->color( fl_gray_ramp( 18 ) );
  113. o->label( "Tempo" );
  114. o->align( FL_ALIGN_LEFT );
  115. tempo_track = o;
  116. }
  117. {
  118. Time_Sequence *o = new Time_Sequence( 0, 24, 800, 24 );
  119. o->color( fl_gray_ramp( 16 ) );
  120. o->label( "Time" );
  121. o->align( FL_ALIGN_LEFT );
  122. time_track = o;
  123. }
  124. {
  125. Ruler_Sequence *o = new Ruler_Sequence( 0, 24, 800, 24 );
  126. o->color( fl_gray_ramp( 'F' ) );
  127. o->label( "Ruler" );
  128. o->align( FL_ALIGN_LEFT );
  129. ruler_track = o;
  130. }
  131. o->size( o->w(), o->child( 0 )->h() * o->children() );
  132. rulers = o;
  133. o->end();
  134. }
  135. {
  136. // sample_rate() = engine->sample_rate();
  137. _fpp = 8;
  138. // _length = sample_rate() * 60 * 2;
  139. /* FIXME: hack */
  140. _length = -1;
  141. {
  142. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
  143. o->type( Fl_Pack::VERTICAL );
  144. o->spacing( 1 );
  145. tracks = o;
  146. o->end();
  147. resizable( o );
  148. }
  149. }
  150. /* rulers go above tracks... */
  151. add( rulers );
  152. /* make sure scrollbars are on top */
  153. add( vscroll );
  154. add( hscroll );
  155. vscroll->range( 0, tracks->h() );
  156. redraw();
  157. end();
  158. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  159. }
  160. float
  161. Timeline::beats_per_minute ( nframes_t when ) const
  162. {
  163. return tempo_track->beats_per_minute( when );
  164. }
  165. int
  166. Timeline::beats_per_bar ( nframes_t when ) const
  167. {
  168. time_sig t = time_track->time( when );
  169. return t.beats_per_bar;
  170. }
  171. void
  172. Timeline::beats_per_minute ( nframes_t when, float bpm )
  173. {
  174. tempo_track->add( new Tempo_Point( when, bpm ) );
  175. }
  176. #if 0
  177. struct BBT
  178. {
  179. int bar, beat, tick;
  180. BBT ( int bar, int beat, int tick ) : bar( bar ), beat( beat ), tick( tick )
  181. {
  182. }
  183. };
  184. /** returns the BBT value for timestamp /when/ by examining the tempo/time maps */
  185. BBT
  186. Timeline::bbt ( nframes_t when )
  187. {
  188. Tempo_Sequence *tempo = (Tempo_Sequence*)rulers->child( 0 );
  189. BBT bbt;
  190. for ( list <Sequence_Widget *>::const_iterator i = tempo.widgets.begin();
  191. i != tempo.widgets.end(); ++i )
  192. {
  193. Tempo_Point *p = *i;
  194. }
  195. };
  196. #endif
  197. /** return the absolute pixel of the nearest measure line to /x/ */
  198. int
  199. Timeline::nearest_line ( int ix )
  200. {
  201. if ( snap_to == None )
  202. return -1;
  203. const nframes_t samples_per_minute = sample_rate() * 60;
  204. for ( int x = ix - 10; x < ix + 10; ++x )
  205. {
  206. const int measure = ts_to_x( samples_per_minute / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  207. if ( measure == 0 )
  208. break;
  209. // const int abs_x = ts_to_x( xoffset ) + x - Track::width();
  210. const int abs_x = ts_to_x( xoffset ) + x;
  211. int bpb = beats_per_bar( x_to_ts( x -Track::width() ) + xoffset );
  212. if ( 0 == x % measure )
  213. {
  214. if ( snap_to == Bars )
  215. {
  216. if ( 0 == (abs_x / measure) % bpb )
  217. return x;
  218. }
  219. else if ( snap_to == Beats )
  220. {
  221. return x;
  222. }
  223. }
  224. }
  225. return -1;
  226. }
  227. /** draw appropriate measure lines inside the given bounding box */
  228. /* FIXME: this function *really* needs to be optimized. Currently it
  229. searched both the time and tempo lists once for every horiontal
  230. pixel and performs a number of calculations--this is slow. */
  231. void
  232. Timeline::draw_measure ( int X, int Y, int W, int H, Fl_Color color, bool BBT )
  233. {
  234. if ( ! draw_with_measure_lines )
  235. return;
  236. fl_push_clip( X, Y, W, H );
  237. // fl_line_style( FL_DASH, 2 );
  238. fl_line_style( FL_DASH, 0 );
  239. const Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  240. const Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  241. const nframes_t samples_per_minute = sample_rate() * 60;
  242. /* we need to back up a bit in order to catch all the numbers */
  243. if ( BBT )
  244. {
  245. X -= 40;
  246. W += 40;
  247. }
  248. for ( int x = X; x < X + W; ++x )
  249. {
  250. // measure = ts_to_x( (double)(sample_rate() * 60) / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  251. const int measure = ts_to_x( samples_per_minute / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  252. if ( measure == 0 )
  253. break;
  254. const int abs_x = ts_to_x( xoffset ) + x - Track::width();
  255. if ( 0 == abs_x % measure )
  256. {
  257. int bpb = beats_per_bar( x_to_ts( x -Track::width() ) + xoffset );
  258. if ( 0 == (abs_x / measure) % bpb )
  259. {
  260. if ( measure * bpb < 8 )
  261. break;
  262. if ( BBT )
  263. {
  264. fl_font( FL_HELVETICA, 14 );
  265. fl_color( fl_lighter( color ) );
  266. char pat[40];
  267. const nframes_t ts = x_to_ts( abs_x );
  268. // if ( draw_hms )
  269. {
  270. const double seconds = (double)ts / sample_rate();
  271. int S = (int)seconds;
  272. int M = S / 60; S -= M * 60;
  273. int H = M / 60; M -= H * 60;
  274. snprintf( pat, sizeof( pat ), "%d:%d:%d", H, M, S );
  275. }
  276. // else if ( draw_bbt )
  277. {
  278. /* const int bar = */
  279. /* const int beat; */
  280. /* const int tick = 0; */
  281. /* snprintf( pat, sizeof( pat ), "%d:%d:%d", bar, beat, tick ); */
  282. }
  283. fl_draw( pat, x, Y + 14 );
  284. }
  285. fl_color( bar );
  286. }
  287. else
  288. {
  289. if ( measure < 8 )
  290. continue;
  291. fl_color( beat );
  292. }
  293. fl_line( x, Y, x, Y + H );
  294. }
  295. }
  296. fl_line_style( FL_SOLID, 0 );
  297. fl_pop_clip();
  298. }
  299. void
  300. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  301. {
  302. draw_measure( X, Y, W, H, color, false );
  303. }
  304. /** just like draw mesure lines except that it also draws the BBT values. */
  305. void
  306. Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color )
  307. {
  308. draw_measure( X, Y, W, H, color, true );
  309. }
  310. void
  311. Timeline::xposition ( int X )
  312. {
  313. // _old_xposition = xoffset;
  314. /* FIXME: shouldn't have to do this... */
  315. X = min( X, ts_to_x( _length ) - tracks->w() - Track::width() );
  316. xoffset = x_to_ts( X );
  317. damage( FL_DAMAGE_SCROLL );
  318. }
  319. void
  320. Timeline::yposition ( int Y )
  321. {
  322. // _old_yposition = _yposition;
  323. _yposition = Y;
  324. damage( FL_DAMAGE_SCROLL );
  325. }
  326. void
  327. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  328. {
  329. Timeline *tl = (Timeline *)v;
  330. fl_push_clip( X, Y, W, H );
  331. /* fl_color( rand() ); */
  332. /* fl_rectf( X, Y, X + W, Y + H ); */
  333. tl->draw_box();
  334. tl->draw_child( *tl->rulers );
  335. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  336. tl->draw_child( *tl->tracks );
  337. fl_pop_clip();
  338. fl_pop_clip();
  339. }
  340. // static unsigned char *rect_image;
  341. void
  342. Timeline::resize ( int X, int Y, int W, int H )
  343. {
  344. Fl_Overlay_Window::resize( X, Y, W, H );
  345. /* why is this necessary? */
  346. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  347. /* why is THIS necessary? */
  348. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  349. vscroll->size( vscroll->w(), H - 18 );
  350. }
  351. void
  352. Timeline::draw ( void )
  353. {
  354. int X, Y, W, H;
  355. int bdx = 0;
  356. int bdw = 0;
  357. X = tracks->x() + bdx + 1;
  358. Y = tracks->y();
  359. W = tracks->w() - bdw - 1;
  360. H = tracks->h();
  361. adjust_vscroll();
  362. /* if ( damage() & FL_DAMAGE_USER1 ) */
  363. /* { */
  364. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  365. /* Rectangle &r = _selection; */
  366. /* make_current(); */
  367. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  368. /* return; */
  369. /* } */
  370. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  371. {
  372. printf( "complete redraw\n" );
  373. draw_box( box(), 0, 0, w(), h(), color() );
  374. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  375. draw_child( *rulers );
  376. fl_pop_clip();
  377. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  378. draw_child( *tracks );
  379. fl_pop_clip();
  380. draw_child( *hscroll );
  381. draw_child( *vscroll );
  382. redraw_overlay();
  383. /* Rectangle &r = _selection; */
  384. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  385. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  386. /* bi.color_average( FL_BLACK, 0.50f ); */
  387. /* bi.draw( r.x, r.y ); */
  388. /* delete[] data; */
  389. /* if ( r.w && r.h ) */
  390. /* { */
  391. /* const unsigned char data[] = { 0, 127, 0, 96, */
  392. /* 0, 96, 0, 127 }; */
  393. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  394. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  395. /* bi2->draw( r.x, r.y ); */
  396. /* delete bi2; */
  397. /* } */
  398. return;
  399. }
  400. if ( damage() & FL_DAMAGE_SCROLL )
  401. {
  402. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  403. int dy = _old_yposition - _yposition;
  404. if ( ! dy )
  405. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  406. Y = rulers->y() + rulers->h();
  407. H = h() - rulers->h() - hscroll->h();
  408. if ( dy == 0 )
  409. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  410. else
  411. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  412. _old_xposition = xoffset;
  413. _old_yposition = _yposition;
  414. }
  415. if ( damage() & FL_DAMAGE_CHILD )
  416. {
  417. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  418. update_child( *rulers );
  419. fl_pop_clip();
  420. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  421. update_child( *tracks );
  422. fl_pop_clip();
  423. update_child( *hscroll );
  424. update_child( *vscroll );
  425. }
  426. }
  427. void
  428. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  429. {
  430. int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  431. if ( x < tracks->x() + Track::width() || x > tracks->x() + tracks->w() )
  432. return;
  433. fl_color( color );
  434. int y = rulers->y() + rulers->h();
  435. int h = this->h() - hscroll->h();
  436. fl_push_clip( Track::width(), y, tracks->w(), h );
  437. fl_line( x, y, x, h );
  438. fl_color( fl_darker( color ) );
  439. fl_line( x - 1, y, x - 1, h );
  440. fl_color( FL_BLACK );
  441. fl_line( x + 1, y, x + 1, h );
  442. /* draw cap */
  443. fl_color( color );
  444. fl_begin_polygon();
  445. fl_vertex( x - 8, y );
  446. fl_vertex( x, y + 8 );
  447. fl_vertex( x + 8, y );
  448. fl_end_polygon();
  449. /* draw cap outline */
  450. fl_color( FL_BLACK );
  451. fl_begin_line();
  452. fl_vertex( x - 8, y );
  453. fl_vertex( x, y + 8 );
  454. fl_vertex( x + 8, y );
  455. fl_end_line();
  456. fl_pop_clip();
  457. }
  458. void
  459. Timeline::draw_playhead ( void )
  460. {
  461. if ( p1 != p2 )
  462. {
  463. draw_cursor( p1, FL_BLUE );
  464. draw_cursor( p2, FL_GREEN );
  465. }
  466. draw_cursor( transport->frame, FL_RED );
  467. }
  468. void
  469. Timeline::redraw_playhead ( void )
  470. {
  471. static nframes_t last_playhead = -1;
  472. if ( last_playhead != transport->frame )
  473. {
  474. redraw_overlay();
  475. last_playhead = transport->frame;
  476. if ( follow_playhead )
  477. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  478. }
  479. }
  480. /** called so many times a second to redraw the playhead etc. */
  481. void
  482. Timeline::update_cb ( void *arg )
  483. {
  484. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  485. Timeline *tl = (Timeline *)arg;
  486. tl->redraw_playhead();
  487. }
  488. void
  489. Timeline::draw_overlay ( void )
  490. {
  491. draw_playhead();
  492. if ( ! ( _selection.w && _selection.h ) )
  493. return;
  494. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  495. const Rectangle &r = _selection;
  496. fl_color( FL_BLACK );
  497. fl_line_style( FL_SOLID, 2 );
  498. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  499. fl_color( FL_MAGENTA );
  500. fl_line_style( FL_DASH, 2 );
  501. fl_rect( r.x, r.y, r.w, r.h );
  502. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  503. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  504. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  505. fl_line_style( FL_SOLID, 0 );
  506. /* const unsigned char data[] = { 127, 127, 127, 96, */
  507. /* 127, 96, 127, 40 }; */
  508. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  509. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  510. /* bi2->draw( r.x, r.y ); */
  511. /* delete bi2; */
  512. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  513. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  514. /* bi.color_average( FL_BLACK, 0.50f ); */
  515. /* bi.draw( r.x, r.y ); */
  516. /* delete[] rect_image; */
  517. /* rect_image = NULL; */
  518. fl_pop_clip();
  519. }
  520. // #include "Sequence_Widget.H"
  521. /** select all widgets in inside rectangle /r/ */
  522. void
  523. Timeline::select ( const Rectangle &r )
  524. {
  525. const int Y = r.y;
  526. for ( int i = tracks->children(); i-- ; )
  527. {
  528. Track *t = (Track*)tracks->child( i );
  529. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  530. t->select( r.x, r.y, r.w, r.h, true, true );
  531. }
  532. }
  533. void
  534. Timeline::delete_selected ( void )
  535. {
  536. Sequence_Widget::delete_selected();
  537. }
  538. void
  539. Timeline::select_none ( void )
  540. {
  541. Sequence_Widget::select_none();
  542. }
  543. int
  544. Timeline::handle ( int m )
  545. {
  546. static Drag *drag = NULL;
  547. switch ( m )
  548. {
  549. case FL_FOCUS:
  550. case FL_UNFOCUS:
  551. // redraw();
  552. return 1;
  553. case FL_KEYBOARD:
  554. case FL_SHORTCUT:
  555. {
  556. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  557. /* we don't want any keys with modifiers... */
  558. return 0;
  559. switch ( Fl::event_key() )
  560. {
  561. case FL_Delete:
  562. case FL_Home:
  563. case FL_End:
  564. /* keep scrollbar from eating these. */
  565. return 0;
  566. case 'p':
  567. {
  568. int X = Fl::event_x() - Track::width();
  569. if ( X > 0 )
  570. {
  571. transport->locate( xoffset + x_to_ts( X ) );
  572. }
  573. return 1;
  574. }
  575. case '[':
  576. {
  577. int X = Fl::event_x() - Track::width();
  578. if ( X > 0 )
  579. {
  580. p1 = xoffset + x_to_ts( X );
  581. }
  582. return 1;
  583. }
  584. case ']':
  585. {
  586. int X = Fl::event_x() - Track::width();
  587. if ( X > 0 )
  588. {
  589. p2 = xoffset + x_to_ts( X );
  590. }
  591. return 1;
  592. }
  593. default:
  594. return Fl_Overlay_Window::handle( m );
  595. }
  596. return 0;
  597. }
  598. default:
  599. {
  600. if ( m == FL_PUSH )
  601. Fl::focus( this );
  602. int r = Fl_Overlay_Window::handle( m );
  603. if ( m != FL_RELEASE && r )
  604. return r;
  605. const int X = Fl::event_x();
  606. const int Y = Fl::event_y();
  607. switch ( m )
  608. {
  609. case FL_PUSH:
  610. {
  611. // take_focus();
  612. if ( Fl::event_button1() )
  613. {
  614. assert( ! drag );
  615. drag = new Drag( X - x(), Y - y() );
  616. _selection.x = drag->x;
  617. _selection.y = drag->y;
  618. }
  619. else if ( Fl::event_button3() )
  620. {
  621. Fl_Menu_Item menu[] =
  622. {
  623. { "Add Track", 0, 0, 0, FL_SUBMENU },
  624. { "Audio", 0, 0, 0 },
  625. { 0 },
  626. { 0 },
  627. };
  628. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  629. if ( r == &menu[1] )
  630. {
  631. /* FIXME: prompt for I/O config? */
  632. Loggable::block_start();
  633. /* add audio track */
  634. char *name = get_unique_track_name( "Audio" );
  635. Track *t = new Track( name );
  636. Sequence *o = new Audio_Sequence( t );
  637. new Control_Sequence( t );
  638. add_track( t );
  639. t->track( o );
  640. Loggable::block_end();
  641. }
  642. }
  643. else
  644. return 0;
  645. break;
  646. }
  647. case FL_DRAG:
  648. {
  649. int ox = X - drag->x;
  650. int oy = Y - drag->y;
  651. if ( ox < 0 )
  652. _selection.x = X;
  653. if ( oy < 0 )
  654. _selection.y = Y;
  655. _selection.w = abs( ox );
  656. _selection.h = abs( oy );
  657. break;
  658. }
  659. case FL_RELEASE:
  660. {
  661. delete drag;
  662. drag = NULL;
  663. select( _selection );
  664. _selection.w = _selection.h = 0;
  665. break;
  666. }
  667. default:
  668. return 0;
  669. break;
  670. }
  671. redraw_overlay();
  672. return 1;
  673. }
  674. }
  675. }
  676. void
  677. Timeline::zoom_in ( void )
  678. {
  679. hscroll->zoom_in();
  680. }
  681. void
  682. Timeline::zoom_out ( void )
  683. {
  684. hscroll->zoom_out();
  685. }
  686. /** zoom the display to show /secs/ seconds per screen */
  687. void
  688. Timeline::zoom ( float secs )
  689. {
  690. const int sw = w() - vscroll->w() - Track::width();
  691. /* FIXME: we actually need to set this in the scalebar */
  692. // _fpp = (int)((secs * sample_rate()) / sw);
  693. redraw();
  694. }
  695. Track *
  696. Timeline::track_by_name ( const char *name )
  697. {
  698. for ( int i = tracks->children(); i-- ; )
  699. {
  700. Track *t = (Track*)tracks->child( i );
  701. if ( ! strcmp( name, t->name() ) )
  702. return t;
  703. }
  704. return NULL;
  705. }
  706. char *
  707. Timeline::get_unique_track_name ( const char *name )
  708. {
  709. char pat[256];
  710. strcpy( pat, name );
  711. for ( int i = 1; track_by_name( pat ); ++i )
  712. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  713. return strdup( pat );
  714. }
  715. void
  716. Timeline::add_track ( Track *track )
  717. {
  718. printf( "added new track to the timeline\n" );
  719. /* FIXME: do locking */
  720. tracks->add( track );
  721. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  722. redraw();
  723. }
  724. void
  725. Timeline::remove_track ( Track *track )
  726. {
  727. printf( "removed track from the timeline\n" );
  728. /* FIXME: do locking */
  729. /* FIXME: what to do about track contents? */
  730. tracks->remove( track );
  731. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  732. redraw();
  733. }
  734. /** Initiate recording for all armed tracks */
  735. bool
  736. Timeline::record ( void )
  737. {
  738. Loggable::block_start();
  739. for ( int i = tracks->children(); i-- ; )
  740. {
  741. Track *t = (Track*)tracks->child( i );
  742. if ( t->armed() && t->record_ds )
  743. t->record_ds->start( transport->frame );
  744. }
  745. deactivate();
  746. return true;
  747. }
  748. /** stop recording for all armed tracks */
  749. void
  750. Timeline::stop ( void )
  751. {
  752. for ( int i = tracks->children(); i-- ; )
  753. {
  754. Track *t = (Track*)tracks->child( i );
  755. if ( t->armed() && t->record_ds )
  756. t->record_ds->stop( transport->frame );
  757. }
  758. Loggable::block_end();
  759. activate();
  760. }
  761. /**********/
  762. /* Engine */
  763. /**********/
  764. /** call process() on each track header */
  765. nframes_t
  766. Timeline::process ( nframes_t nframes )
  767. {
  768. for ( int i = tracks->children(); i-- ; )
  769. {
  770. Track *t = (Track*)tracks->child( i );
  771. t->process( nframes );
  772. }
  773. /* FIXME: BOGUS */
  774. return nframes;
  775. }
  776. /* THREAD: RT */
  777. void
  778. Timeline::seek ( nframes_t frame )
  779. {
  780. for ( int i = tracks->children(); i-- ; )
  781. {
  782. Track *t = (Track*)tracks->child( i );
  783. t->seek( frame );
  784. }
  785. }
  786. /* THREAD: RT */
  787. int
  788. Timeline::seek_pending ( void )
  789. {
  790. int r = 0;
  791. for ( int i = tracks->children(); i-- ; )
  792. {
  793. Track *t = (Track*)tracks->child( i );
  794. if ( t->playback_ds )
  795. r += t->playback_ds->buffer_percent() < 50;
  796. }
  797. return r;
  798. }
  799. /* FIXME: shouldn't these belong to the engine? */
  800. int
  801. Timeline::total_input_buffer_percent ( void )
  802. {
  803. int r = 0;
  804. int cnt = 0;
  805. for ( int i = tracks->children(); i-- ; )
  806. {
  807. Track *t = (Track*)tracks->child( i );
  808. if ( t->record_ds )
  809. {
  810. ++cnt;
  811. r += t->record_ds->buffer_percent();
  812. }
  813. }
  814. if ( ! cnt )
  815. return 0;
  816. return r / cnt;
  817. }
  818. int
  819. Timeline::total_output_buffer_percent ( void )
  820. {
  821. int r = 0;
  822. int cnt = 0;
  823. for ( int i = tracks->children(); i-- ; )
  824. {
  825. Track *t = (Track*)tracks->child( i );
  826. if ( t->playback_ds )
  827. {
  828. ++cnt;
  829. r += t->playback_ds->buffer_percent();
  830. }
  831. }
  832. if ( ! cnt )
  833. return 0;
  834. return r / cnt;
  835. }