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.

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