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.

1235 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. int X, Y, W, H;
  351. int bdx = 0;
  352. int bdw = 0;
  353. X = tracks->x() + bdx + 1;
  354. Y = tracks->y();
  355. W = tracks->w() - bdw - 1;
  356. H = tracks->h();
  357. adjust_vscroll();
  358. /* if ( damage() & FL_DAMAGE_USER1 ) */
  359. /* { */
  360. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  361. /* Rectangle &r = _selection; */
  362. /* make_current(); */
  363. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  364. /* return; */
  365. /* } */
  366. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  367. {
  368. DMESSAGE( "complete redraw" );
  369. draw_box( box(), 0, 0, w(), h(), color() );
  370. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  371. draw_child( *rulers );
  372. fl_pop_clip();
  373. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  374. draw_child( *tracks );
  375. fl_pop_clip();
  376. draw_child( *hscroll );
  377. draw_child( *vscroll );
  378. redraw_overlay();
  379. /* Rectangle &r = _selection; */
  380. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  381. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  382. /* bi.color_average( FL_BLACK, 0.50f ); */
  383. /* bi.draw( r.x, r.y ); */
  384. /* delete[] data; */
  385. /* if ( r.w && r.h ) */
  386. /* { */
  387. /* const unsigned char data[] = { 0, 127, 0, 96, */
  388. /* 0, 96, 0, 127 }; */
  389. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  390. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  391. /* bi2->draw( r.x, r.y ); */
  392. /* delete bi2; */
  393. /* } */
  394. goto done;
  395. }
  396. if ( damage() & FL_DAMAGE_SCROLL )
  397. {
  398. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  399. int dy = _old_yposition - _yposition;
  400. /* draw_child( *rulers ); */
  401. if ( ! dy )
  402. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  403. Y = rulers->y() + rulers->h();
  404. H = h() - rulers->h() - hscroll->h();
  405. if ( dy == 0 )
  406. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  407. else
  408. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  409. }
  410. if ( damage() & FL_DAMAGE_CHILD )
  411. {
  412. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  413. update_child( *rulers );
  414. fl_pop_clip();
  415. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  416. update_child( *tracks );
  417. fl_pop_clip();
  418. update_child( *hscroll );
  419. update_child( *vscroll );
  420. }
  421. done:
  422. _old_xposition = xoffset;
  423. _old_yposition = _yposition;
  424. }
  425. void
  426. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  427. {
  428. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  429. if ( frame < xoffset )
  430. return;
  431. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  432. if ( x > tracks->x() + tracks->w() )
  433. return;
  434. fl_color( color );
  435. const int y = rulers->y() + rulers->h();
  436. const int h = this->h() - hscroll->h();
  437. fl_push_clip( Track::width(), y, tracks->w(), h );
  438. fl_line( x, y, x, h );
  439. fl_color( fl_darker( color ) );
  440. fl_line( x - 1, y, x - 1, h );
  441. fl_color( FL_BLACK );
  442. fl_line( x + 1, y, x + 1, h );
  443. /* draw cap */
  444. fl_color( color );
  445. fl_begin_polygon();
  446. fl_vertex( x - 8, y );
  447. fl_vertex( x, y + 8 );
  448. fl_vertex( x + 8, y );
  449. fl_end_polygon();
  450. /* draw cap outline */
  451. fl_color( FL_BLACK );
  452. fl_begin_line();
  453. fl_vertex( x - 8, y );
  454. fl_vertex( x, y + 8 );
  455. fl_vertex( x + 8, y );
  456. fl_end_line();
  457. fl_pop_clip();
  458. }
  459. void
  460. Timeline::draw_playhead ( void )
  461. {
  462. if ( p1 != p2 )
  463. {
  464. draw_cursor( p1, FL_BLUE );
  465. draw_cursor( p2, FL_GREEN );
  466. }
  467. draw_cursor( transport->frame, FL_RED );
  468. }
  469. void
  470. Timeline::redraw_playhead ( void )
  471. {
  472. static nframes_t last_playhead = -1;
  473. if ( last_playhead != transport->frame )
  474. {
  475. redraw_overlay();
  476. last_playhead = transport->frame;
  477. if ( follow_playhead )
  478. {
  479. if ( center_playhead && active() )
  480. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  481. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  482. xposition( ts_to_x( transport->frame ) );
  483. }
  484. }
  485. }
  486. /** called so many times a second to redraw the playhead etc. */
  487. void
  488. Timeline::update_cb ( void *arg )
  489. {
  490. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  491. Timeline *tl = (Timeline *)arg;
  492. tl->redraw_playhead();
  493. }
  494. void
  495. Timeline::draw_overlay ( void )
  496. {
  497. draw_playhead();
  498. if ( ! ( _selection.w && _selection.h ) )
  499. return;
  500. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  501. const Rectangle &r = _selection;
  502. fl_color( FL_BLACK );
  503. fl_line_style( FL_SOLID, 2 );
  504. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  505. fl_color( FL_MAGENTA );
  506. fl_line_style( FL_DASH, 2 );
  507. fl_rect( r.x, r.y, r.w, r.h );
  508. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  509. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  510. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  511. fl_line_style( FL_SOLID, 0 );
  512. /* const unsigned char data[] = { 127, 127, 127, 96, */
  513. /* 127, 96, 127, 40 }; */
  514. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  515. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  516. /* bi2->draw( r.x, r.y ); */
  517. /* delete bi2; */
  518. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  519. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  520. /* bi.color_average( FL_BLACK, 0.50f ); */
  521. /* bi.draw( r.x, r.y ); */
  522. /* delete[] rect_image; */
  523. /* rect_image = NULL; */
  524. fl_pop_clip();
  525. }
  526. // #include "Sequence_Widget.H"
  527. /** select all widgets in inside rectangle /r/ */
  528. void
  529. Timeline::select ( const Rectangle &r )
  530. {
  531. const int Y = r.y;
  532. for ( int i = tracks->children(); i-- ; )
  533. {
  534. Track *t = (Track*)tracks->child( i );
  535. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  536. t->select( r.x, r.y, r.w, r.h, true, true );
  537. }
  538. }
  539. void
  540. Timeline::delete_selected ( void )
  541. {
  542. Sequence_Widget::delete_selected();
  543. }
  544. void
  545. Timeline::select_none ( void )
  546. {
  547. Sequence_Widget::select_none();
  548. }
  549. /** An unfortunate necessity for implementing our own DND aside from
  550. * the (bogus) native FLTK system */
  551. Track *
  552. Timeline::track_under ( int Y )
  553. {
  554. for ( int i = tracks->children(); i-- ; )
  555. {
  556. Track *t = (Track*)tracks->child( i );
  557. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  558. return t;
  559. }
  560. return NULL;
  561. }
  562. int
  563. Timeline::handle ( int m )
  564. {
  565. static Drag *drag = NULL;
  566. switch ( m )
  567. {
  568. case FL_FOCUS:
  569. case FL_UNFOCUS:
  570. // redraw();
  571. return 1;
  572. case FL_KEYBOARD:
  573. case FL_SHORTCUT:
  574. {
  575. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  576. /* we don't want any keys with modifiers... */
  577. return 0;
  578. switch ( Fl::event_key() )
  579. {
  580. case FL_Delete:
  581. case FL_Home:
  582. case FL_End:
  583. /* keep scrollbar from eating these. */
  584. return 0;
  585. case 'p':
  586. {
  587. int X = Fl::event_x() - Track::width();
  588. if ( X > 0 )
  589. {
  590. transport->locate( xoffset + x_to_ts( X ) );
  591. }
  592. return 1;
  593. }
  594. case '[':
  595. {
  596. int X = Fl::event_x() - Track::width();
  597. if ( X > 0 )
  598. {
  599. p1 = xoffset + x_to_ts( X );
  600. }
  601. return 1;
  602. }
  603. case ']':
  604. {
  605. int X = Fl::event_x() - Track::width();
  606. if ( X > 0 )
  607. {
  608. p2 = xoffset + x_to_ts( X );
  609. }
  610. return 1;
  611. }
  612. default:
  613. return Fl_Overlay_Window::handle( m );
  614. }
  615. return 0;
  616. }
  617. default:
  618. {
  619. if ( m == FL_PUSH )
  620. Fl::focus( this );
  621. int r = Fl_Overlay_Window::handle( m );
  622. if ( m != FL_RELEASE && r )
  623. return r;
  624. const int X = Fl::event_x();
  625. const int Y = Fl::event_y();
  626. switch ( m )
  627. {
  628. case FL_PUSH:
  629. {
  630. // take_focus();
  631. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  632. return 0;
  633. if ( Fl::event_button1() )
  634. {
  635. assert( ! drag );
  636. drag = new Drag( X - x(), Y - y() );
  637. _selection.x = drag->x;
  638. _selection.y = drag->y;
  639. }
  640. else if ( Fl::event_button3() )
  641. {
  642. Fl_Menu_Item menu[] =
  643. {
  644. { "Add Track", 0, 0, 0, FL_SUBMENU },
  645. { "Audio", 0, 0, 0 },
  646. { 0 },
  647. { 0 },
  648. };
  649. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  650. if ( r == &menu[1] )
  651. {
  652. /* FIXME: prompt for I/O config? */
  653. Loggable::block_start();
  654. /* add audio track */
  655. char *name = get_unique_track_name( "Audio" );
  656. Track *t = new Track( name );
  657. Audio_Sequence *o = new Audio_Sequence( t );
  658. // new Control_Sequence( t );
  659. add_track( t );
  660. t->track( o );
  661. Loggable::block_end();
  662. }
  663. }
  664. else
  665. return 0;
  666. break;
  667. }
  668. case FL_DRAG:
  669. {
  670. int ox = X - drag->x;
  671. int oy = Y - drag->y;
  672. if ( ox < 0 )
  673. _selection.x = X;
  674. if ( oy < 0 )
  675. _selection.y = Y;
  676. _selection.w = abs( ox );
  677. _selection.h = abs( oy );
  678. break;
  679. }
  680. case FL_RELEASE:
  681. {
  682. delete drag;
  683. drag = NULL;
  684. select( _selection );
  685. _selection.w = _selection.h = 0;
  686. break;
  687. }
  688. default:
  689. return 0;
  690. break;
  691. }
  692. redraw_overlay();
  693. return 1;
  694. }
  695. }
  696. }
  697. void
  698. Timeline::zoom_in ( void )
  699. {
  700. hscroll->zoom_in();
  701. }
  702. void
  703. Timeline::zoom_out ( void )
  704. {
  705. hscroll->zoom_out();
  706. }
  707. /** zoom the display to show /secs/ seconds per screen */
  708. void
  709. Timeline::zoom ( float secs )
  710. {
  711. const int sw = tracks->w() - Track::width();
  712. int fpp = (int)((secs * sample_rate()) / sw);
  713. int p = 0;
  714. while ( 1 << p < fpp ) p++;
  715. hscroll->zoom( p );
  716. redraw();
  717. }
  718. void
  719. Timeline::zoom_fit ( void )
  720. {
  721. zoom( _length / (float)sample_rate() );
  722. }
  723. Track *
  724. Timeline::track_by_name ( const char *name )
  725. {
  726. for ( int i = tracks->children(); i-- ; )
  727. {
  728. Track *t = (Track*)tracks->child( i );
  729. if ( ! strcmp( name, t->name() ) )
  730. return t;
  731. }
  732. return NULL;
  733. }
  734. char *
  735. Timeline::get_unique_track_name ( const char *name )
  736. {
  737. char pat[256];
  738. strcpy( pat, name );
  739. for ( int i = 1; track_by_name( pat ); ++i )
  740. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  741. return strdup( pat );
  742. }
  743. void
  744. Timeline::add_track ( Track *track )
  745. {
  746. DMESSAGE( "added new track to the timeline" );
  747. /* FIXME: do locking */
  748. tracks->add( track );
  749. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  750. redraw();
  751. }
  752. void
  753. Timeline::remove_track ( Track *track )
  754. {
  755. DMESSAGE( "removed track from the timeline" );
  756. /* FIXME: do locking */
  757. /* FIXME: what to do about track contents? */
  758. tracks->remove( track );
  759. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  760. redraw();
  761. }
  762. /** Initiate recording for all armed tracks */
  763. bool
  764. Timeline::record ( void )
  765. {
  766. Loggable::block_start();
  767. for ( int i = tracks->children(); i-- ; )
  768. {
  769. Track *t = (Track*)tracks->child( i );
  770. if ( t->armed() && t->record_ds )
  771. t->record_ds->start( transport->frame );
  772. }
  773. deactivate();
  774. return true;
  775. }
  776. /** stop recording for all armed tracks */
  777. void
  778. Timeline::stop ( void )
  779. {
  780. for ( int i = tracks->children(); i-- ; )
  781. {
  782. Track *t = (Track*)tracks->child( i );
  783. if ( t->armed() && t->record_ds )
  784. t->record_ds->stop( transport->frame );
  785. }
  786. Loggable::block_end();
  787. activate();
  788. }
  789. /**********/
  790. /* Engine */
  791. /**********/
  792. /** call process() on each track header */
  793. nframes_t
  794. Timeline::process ( nframes_t nframes )
  795. {
  796. for ( int i = tracks->children(); i-- ; )
  797. {
  798. Track *t = (Track*)tracks->child( i );
  799. t->process( nframes );
  800. }
  801. /* FIXME: BOGUS */
  802. return nframes;
  803. }
  804. /* THREAD: RT */
  805. void
  806. Timeline::seek ( nframes_t frame )
  807. {
  808. for ( int i = tracks->children(); i-- ; )
  809. {
  810. Track *t = (Track*)tracks->child( i );
  811. t->seek( frame );
  812. }
  813. }
  814. /* THREAD: RT */
  815. int
  816. Timeline::seek_pending ( void )
  817. {
  818. int r = 0;
  819. for ( int i = tracks->children(); i-- ; )
  820. {
  821. Track *t = (Track*)tracks->child( i );
  822. if ( t->playback_ds )
  823. r += t->playback_ds->buffer_percent() < 50;
  824. }
  825. return r;
  826. }
  827. /* FIXME: shouldn't these belong to the engine? */
  828. int
  829. Timeline::total_input_buffer_percent ( void )
  830. {
  831. int r = 0;
  832. int cnt = 0;
  833. for ( int i = tracks->children(); i-- ; )
  834. {
  835. Track *t = (Track*)tracks->child( i );
  836. if ( t->record_ds )
  837. {
  838. ++cnt;
  839. r += t->record_ds->buffer_percent();
  840. }
  841. }
  842. if ( ! cnt )
  843. return 0;
  844. return r / cnt;
  845. }
  846. int
  847. Timeline::total_output_buffer_percent ( void )
  848. {
  849. int r = 0;
  850. int cnt = 0;
  851. for ( int i = tracks->children(); i-- ; )
  852. {
  853. Track *t = (Track*)tracks->child( i );
  854. if ( t->playback_ds )
  855. {
  856. ++cnt;
  857. r += t->playback_ds->buffer_percent();
  858. }
  859. }
  860. if ( ! cnt )
  861. return 0;
  862. return r / cnt;
  863. }
  864. int
  865. Timeline::total_playback_xruns ( void )
  866. {
  867. int r = 0;
  868. for ( int i = tracks->children(); i-- ; )
  869. {
  870. Track *t = (Track*)tracks->child( i );
  871. if ( t->playback_ds )
  872. r += t->playback_ds->xruns();
  873. }
  874. return r;
  875. }
  876. int
  877. Timeline::total_capture_xruns ( void )
  878. {
  879. int r = 0;
  880. for ( int i = tracks->children(); i-- ; )
  881. {
  882. Track *t = (Track*)tracks->child( i );
  883. if ( t->record_ds )
  884. r += t->record_ds->xruns();
  885. }
  886. return r;
  887. }