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.

1360 lines
31KB

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