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.

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