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.

1214 lines
27KB

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