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.

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