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.

1226 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. 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. Ruler_Sequence *o = new Ruler_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 != -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. printf( "complete redraw\n" );
  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. }
  559. int
  560. Timeline::handle ( int m )
  561. {
  562. static Drag *drag = NULL;
  563. switch ( m )
  564. {
  565. case FL_FOCUS:
  566. case FL_UNFOCUS:
  567. // redraw();
  568. return 1;
  569. case FL_KEYBOARD:
  570. case FL_SHORTCUT:
  571. {
  572. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  573. /* we don't want any keys with modifiers... */
  574. return 0;
  575. switch ( Fl::event_key() )
  576. {
  577. case FL_Delete:
  578. case FL_Home:
  579. case FL_End:
  580. /* keep scrollbar from eating these. */
  581. return 0;
  582. case 'p':
  583. {
  584. int X = Fl::event_x() - Track::width();
  585. if ( X > 0 )
  586. {
  587. transport->locate( xoffset + x_to_ts( X ) );
  588. }
  589. return 1;
  590. }
  591. case '[':
  592. {
  593. int X = Fl::event_x() - Track::width();
  594. if ( X > 0 )
  595. {
  596. p1 = xoffset + x_to_ts( X );
  597. }
  598. return 1;
  599. }
  600. case ']':
  601. {
  602. int X = Fl::event_x() - Track::width();
  603. if ( X > 0 )
  604. {
  605. p2 = xoffset + x_to_ts( X );
  606. }
  607. return 1;
  608. }
  609. default:
  610. return Fl_Overlay_Window::handle( m );
  611. }
  612. return 0;
  613. }
  614. default:
  615. {
  616. if ( m == FL_PUSH )
  617. Fl::focus( this );
  618. int r = Fl_Overlay_Window::handle( m );
  619. if ( m != FL_RELEASE && r )
  620. return r;
  621. const int X = Fl::event_x();
  622. const int Y = Fl::event_y();
  623. switch ( m )
  624. {
  625. case FL_PUSH:
  626. {
  627. // take_focus();
  628. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  629. return 0;
  630. if ( Fl::event_button1() )
  631. {
  632. assert( ! drag );
  633. drag = new Drag( X - x(), Y - y() );
  634. _selection.x = drag->x;
  635. _selection.y = drag->y;
  636. }
  637. else if ( Fl::event_button3() )
  638. {
  639. Fl_Menu_Item menu[] =
  640. {
  641. { "Add Track", 0, 0, 0, FL_SUBMENU },
  642. { "Audio", 0, 0, 0 },
  643. { 0 },
  644. { 0 },
  645. };
  646. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  647. if ( r == &menu[1] )
  648. {
  649. /* FIXME: prompt for I/O config? */
  650. Loggable::block_start();
  651. /* add audio track */
  652. char *name = get_unique_track_name( "Audio" );
  653. Track *t = new Track( name );
  654. Sequence *o = new Audio_Sequence( t );
  655. // new Control_Sequence( t );
  656. add_track( t );
  657. t->track( o );
  658. Loggable::block_end();
  659. }
  660. }
  661. else
  662. return 0;
  663. break;
  664. }
  665. case FL_DRAG:
  666. {
  667. int ox = X - drag->x;
  668. int oy = Y - drag->y;
  669. if ( ox < 0 )
  670. _selection.x = X;
  671. if ( oy < 0 )
  672. _selection.y = Y;
  673. _selection.w = abs( ox );
  674. _selection.h = abs( oy );
  675. break;
  676. }
  677. case FL_RELEASE:
  678. {
  679. delete drag;
  680. drag = NULL;
  681. select( _selection );
  682. _selection.w = _selection.h = 0;
  683. break;
  684. }
  685. default:
  686. return 0;
  687. break;
  688. }
  689. redraw_overlay();
  690. return 1;
  691. }
  692. }
  693. }
  694. void
  695. Timeline::zoom_in ( void )
  696. {
  697. hscroll->zoom_in();
  698. }
  699. void
  700. Timeline::zoom_out ( void )
  701. {
  702. hscroll->zoom_out();
  703. }
  704. /** zoom the display to show /secs/ seconds per screen */
  705. void
  706. Timeline::zoom ( float secs )
  707. {
  708. const int sw = tracks->w() - Track::width();
  709. int fpp = (int)((secs * sample_rate()) / sw);
  710. int p = 0;
  711. while ( 1 << p < fpp ) p++;
  712. hscroll->zoom( p );
  713. redraw();
  714. }
  715. void
  716. Timeline::zoom_fit ( void )
  717. {
  718. zoom( _length / (float)sample_rate() );
  719. }
  720. Track *
  721. Timeline::track_by_name ( const char *name )
  722. {
  723. for ( int i = tracks->children(); i-- ; )
  724. {
  725. Track *t = (Track*)tracks->child( i );
  726. if ( ! strcmp( name, t->name() ) )
  727. return t;
  728. }
  729. return NULL;
  730. }
  731. char *
  732. Timeline::get_unique_track_name ( const char *name )
  733. {
  734. char pat[256];
  735. strcpy( pat, name );
  736. for ( int i = 1; track_by_name( pat ); ++i )
  737. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  738. return strdup( pat );
  739. }
  740. void
  741. Timeline::add_track ( Track *track )
  742. {
  743. printf( "added new track to the timeline\n" );
  744. /* FIXME: do locking */
  745. tracks->add( track );
  746. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  747. redraw();
  748. }
  749. void
  750. Timeline::remove_track ( Track *track )
  751. {
  752. printf( "removed track from the timeline\n" );
  753. /* FIXME: do locking */
  754. /* FIXME: what to do about track contents? */
  755. tracks->remove( track );
  756. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  757. redraw();
  758. }
  759. /** Initiate recording for all armed tracks */
  760. bool
  761. Timeline::record ( void )
  762. {
  763. Loggable::block_start();
  764. for ( int i = tracks->children(); i-- ; )
  765. {
  766. Track *t = (Track*)tracks->child( i );
  767. if ( t->armed() && t->record_ds )
  768. t->record_ds->start( transport->frame );
  769. }
  770. deactivate();
  771. return true;
  772. }
  773. /** stop recording for all armed tracks */
  774. void
  775. Timeline::stop ( void )
  776. {
  777. for ( int i = tracks->children(); i-- ; )
  778. {
  779. Track *t = (Track*)tracks->child( i );
  780. if ( t->armed() && t->record_ds )
  781. t->record_ds->stop( transport->frame );
  782. }
  783. Loggable::block_end();
  784. activate();
  785. }
  786. /**********/
  787. /* Engine */
  788. /**********/
  789. /** call process() on each track header */
  790. nframes_t
  791. Timeline::process ( nframes_t nframes )
  792. {
  793. for ( int i = tracks->children(); i-- ; )
  794. {
  795. Track *t = (Track*)tracks->child( i );
  796. t->process( nframes );
  797. }
  798. /* FIXME: BOGUS */
  799. return nframes;
  800. }
  801. /* THREAD: RT */
  802. void
  803. Timeline::seek ( nframes_t frame )
  804. {
  805. for ( int i = tracks->children(); i-- ; )
  806. {
  807. Track *t = (Track*)tracks->child( i );
  808. t->seek( frame );
  809. }
  810. }
  811. /* THREAD: RT */
  812. int
  813. Timeline::seek_pending ( void )
  814. {
  815. int r = 0;
  816. for ( int i = tracks->children(); i-- ; )
  817. {
  818. Track *t = (Track*)tracks->child( i );
  819. if ( t->playback_ds )
  820. r += t->playback_ds->buffer_percent() < 50;
  821. }
  822. return r;
  823. }
  824. /* FIXME: shouldn't these belong to the engine? */
  825. int
  826. Timeline::total_input_buffer_percent ( void )
  827. {
  828. int r = 0;
  829. int cnt = 0;
  830. for ( int i = tracks->children(); i-- ; )
  831. {
  832. Track *t = (Track*)tracks->child( i );
  833. if ( t->record_ds )
  834. {
  835. ++cnt;
  836. r += t->record_ds->buffer_percent();
  837. }
  838. }
  839. if ( ! cnt )
  840. return 0;
  841. return r / cnt;
  842. }
  843. int
  844. Timeline::total_output_buffer_percent ( void )
  845. {
  846. int r = 0;
  847. int cnt = 0;
  848. for ( int i = tracks->children(); i-- ; )
  849. {
  850. Track *t = (Track*)tracks->child( i );
  851. if ( t->playback_ds )
  852. {
  853. ++cnt;
  854. r += t->playback_ds->buffer_percent();
  855. }
  856. }
  857. if ( ! cnt )
  858. return 0;
  859. return r / cnt;
  860. }
  861. int
  862. Timeline::total_playback_xruns ( void )
  863. {
  864. int r = 0;
  865. for ( int i = tracks->children(); i-- ; )
  866. {
  867. Track *t = (Track*)tracks->child( i );
  868. if ( t->playback_ds )
  869. r += t->playback_ds->xruns();
  870. }
  871. return r;
  872. }
  873. int
  874. Timeline::total_capture_xruns ( void )
  875. {
  876. int r = 0;
  877. for ( int i = tracks->children(); i-- ; )
  878. {
  879. Track *t = (Track*)tracks->child( i );
  880. if ( t->record_ds )
  881. r += t->record_ds->xruns();
  882. }
  883. return r;
  884. }