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.

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