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.

1412 lines
32KB

  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 <FL/Fl_Scroll.H>
  19. #include <FL/Fl_Pack.H>
  20. #include <FL/Fl_Scrollbar.H>
  21. #include <FL/Fl_Widget.H>
  22. #include <FL/fl_draw.H>
  23. #include "Timeline.H"
  24. #include "Tempo_Sequence.H"
  25. #include "Time_Sequence.H"
  26. #include "Audio_Sequence.H"
  27. #include "Control_Sequence.H"
  28. #include <FL/Fl_Scrollbar.H>
  29. #include "Scalebar.H"
  30. #include "Sequence.H"
  31. #include "Annotation_Sequence.H"
  32. // #include <FL/Fl_Image.H>
  33. // #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  34. #include "Track.H"
  35. bool Timeline::draw_with_measure_lines = true;
  36. Timeline::snap_e Timeline::snap_to = Bars;
  37. bool Timeline::snap_magnetic = true;
  38. bool Timeline::follow_playhead = true;
  39. bool Timeline::center_playhead = true;
  40. const float UPDATE_FREQ = 0.02f;
  41. #include "Playback_DS.H"
  42. #include "Record_DS.H"
  43. #include "Transport.H"
  44. /** return the combined height of all visible children of (veritcal)
  45. pack, /p/. This is necessary because pack sizes are adjusted only
  46. when the relevant areas are exposes. */
  47. static int
  48. pack_visible_height ( const Fl_Pack *p )
  49. {
  50. int th = 0;
  51. const Fl_Widget* const *w = p->array();
  52. for ( int i = p->children(); i--; ++w )
  53. if ( (*w)->visible() )
  54. th += (*w)->h() + p->spacing();
  55. return th;
  56. }
  57. #define BP fl_begin_polygon()
  58. #define EP fl_end_polygon()
  59. #define vv(x,y) fl_vertex( x, y )
  60. #define BL fl_begin_line()
  61. #define EL fl_end_line()
  62. static void
  63. draw_full_arrow_symbol ( Fl_Color color )
  64. {
  65. /* draw cap */
  66. fl_color( color );
  67. BP;
  68. vv( -1, -1 );
  69. vv( 0, 1 );
  70. vv( 1, -1 );
  71. EP;
  72. /* draw cap outline */
  73. fl_color( FL_BLACK );
  74. BL;
  75. vv( -1, -1 );
  76. vv( 0, 1 );
  77. vv( 1, -1 );
  78. EL;
  79. }
  80. /** recalculate the size of vertical scrolling area and inform scrollbar */
  81. void
  82. Timeline::adjust_vscroll ( void )
  83. {
  84. vscroll->value( _yposition, h() - rulers->h() - hscroll->h(), 0, pack_visible_height( tracks ) );
  85. }
  86. void
  87. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  88. {
  89. ((Timeline*)v)->cb_scroll( w );
  90. }
  91. void
  92. Timeline::cb_scroll ( Fl_Widget *w )
  93. {
  94. if ( w == vscroll )
  95. {
  96. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  97. yposition( vscroll->value() );
  98. adjust_vscroll();
  99. }
  100. else
  101. {
  102. if ( hscroll->zoom_changed() )
  103. {
  104. nframes_t under_mouse = x_to_offset( Fl::event_x() );
  105. _fpp = hscroll->zoom();
  106. const int tw = tracks->w() - Track::width();
  107. // hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( _length ) );
  108. hscroll->value( max( 0, ts_to_x( under_mouse ) - ( Fl::event_x() - tracks->x() - Track::width() ) ),
  109. tw, 0, ts_to_x( _length ) );
  110. redraw();
  111. }
  112. xposition( hscroll->value() );
  113. }
  114. }
  115. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  116. {
  117. box( FL_FLAT_BOX );
  118. xoffset = 0;
  119. _yposition = 0;
  120. _old_yposition = 0;
  121. _old_xposition = 0;
  122. X = Y = 0;
  123. p1 = p2 = 0;
  124. {
  125. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  126. o->range( 0, 48000 * 300 );
  127. // o->zoom_range( 1, 16384 );
  128. // o->zoom_range( 1, 65536 << 4 );
  129. o->zoom_range( 1, 20 );
  130. o->zoom( 8 );
  131. o->type( FL_HORIZONTAL );
  132. o->callback( cb_scroll, this );
  133. hscroll = o;
  134. }
  135. {
  136. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  137. o->type( FL_VERTICAL );
  138. o->callback( cb_scroll, this );
  139. vscroll = o;
  140. }
  141. {
  142. Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  143. o->type( Fl_Pack::VERTICAL );
  144. {
  145. Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
  146. o->color( fl_gray_ramp( 18 ) );
  147. o->label( "Tempo" );
  148. o->align( FL_ALIGN_LEFT );
  149. tempo_track = o;
  150. }
  151. {
  152. Time_Sequence *o = new Time_Sequence( 0, 24, 800, 24 );
  153. o->color( fl_gray_ramp( 16 ) );
  154. o->label( "Time" );
  155. o->align( FL_ALIGN_LEFT );
  156. time_track = o;
  157. }
  158. /* { */
  159. /* Annotation_Sequence *o = new Annotation_Sequence( 0, 24, 800, 24 ); */
  160. /* o->color( fl_gray_ramp( 'F' ) ); */
  161. /* o->label( "Ruler" ); */
  162. /* o->align( FL_ALIGN_LEFT ); */
  163. /* ruler_track = o; */
  164. /* } */
  165. o->size( o->w(), o->child( 0 )->h() * o->children() );
  166. rulers = o;
  167. o->end();
  168. }
  169. {
  170. // sample_rate() = engine->sample_rate();
  171. _fpp = 8;
  172. // _length = sample_rate() * 60 * 2;
  173. /* FIXME: hack */
  174. _length = -1;
  175. {
  176. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
  177. o->type( Fl_Pack::VERTICAL );
  178. o->spacing( 1 );
  179. tracks = o;
  180. o->end();
  181. resizable( o );
  182. }
  183. }
  184. /* rulers go above tracks... */
  185. add( rulers );
  186. /* make sure scrollbars are on top */
  187. add( vscroll );
  188. add( hscroll );
  189. vscroll->range( 0, tracks->h() );
  190. redraw();
  191. end();
  192. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  193. }
  194. /* float */
  195. /* Timeline::beats_per_minute ( nframes_t when ) const */
  196. /* { */
  197. /* /\* return tempo_track->beats_per_minute( when ); *\/ */
  198. /* } */
  199. /* int */
  200. /* Timeline::beats_per_bar ( nframes_t when ) const */
  201. /* { */
  202. /* time_sig t = time_track->time( when ); */
  203. /* return t.beats_per_bar; */
  204. /* } */
  205. void
  206. Timeline::beats_per_minute ( nframes_t when, float bpm )
  207. {
  208. tempo_track->add( new Tempo_Point( when, bpm ) );
  209. }
  210. void
  211. Timeline::time ( nframes_t when, int bpb, int note_type )
  212. {
  213. time_track->add( new Time_Point( when, bpb, note_type ) );
  214. }
  215. /************/
  216. /* Snapping */
  217. /************/
  218. struct nearest_line_arg
  219. {
  220. nframes_t original;
  221. nframes_t closest;
  222. };
  223. const int snap_pixel = 10;
  224. static nframes_t
  225. abs_diff ( nframes_t n1, nframes_t n2 )
  226. {
  227. return n1 > n2 ? n1 - n2 : n2 - n1;
  228. }
  229. void
  230. nearest_line_cb ( nframes_t frame, const BBT &bbt, void *arg )
  231. {
  232. nearest_line_arg *n = (nearest_line_arg *)arg;
  233. if ( Timeline::snap_to == Timeline::Bars && bbt.beat )
  234. return;
  235. if ( Timeline::snap_magnetic &&
  236. abs_diff( frame, n->original ) > timeline->x_to_ts( snap_pixel ) )
  237. return;
  238. if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
  239. n->closest = frame;
  240. }
  241. /** Set the value pointed to by /frame/ to the frame number of the of
  242. the nearest measure line to /when/. Returns true if the new value of
  243. *frame is valid, false otherwise. */
  244. bool
  245. Timeline::nearest_line ( nframes_t when, nframes_t *frame ) const
  246. {
  247. if ( snap_to == None )
  248. return false;
  249. nearest_line_arg n = { when, -1 };
  250. render_tempomap( when - x_to_ts( w() >> 1 ), x_to_ts( w() ), nearest_line_cb, &n );
  251. *frame = n.closest;
  252. return *frame != (nframes_t)-1;
  253. }
  254. nframes_t
  255. Timeline::x_to_offset ( int x ) const
  256. {
  257. return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
  258. }
  259. /** draws a single measure line */
  260. static void
  261. draw_measure_cb ( nframes_t frame, const BBT &bbt, void *arg )
  262. {
  263. Fl_Color *color = (Fl_Color*)arg;
  264. fl_color( FL_BLACK );
  265. fl_line_style( FL_DASH, 0 );
  266. if ( bbt.beat )
  267. ++color;
  268. fl_color( *color );
  269. const int x = timeline->ts_to_x( frame - timeline->xoffset ) + Track::width();
  270. fl_line( x, 0, x, 5000 );
  271. fl_line_style( FL_SOLID, 0 );
  272. }
  273. /* FIXME: wrong place for this */
  274. const float ticks_per_beat = 1920.0;
  275. void
  276. Timeline::update_tempomap ( void )
  277. {
  278. /* FIXME: we need some type of locking! */
  279. _tempomap.clear();
  280. for ( list <Sequence_Widget *>::const_iterator i = time_track->_widgets.begin();
  281. i != time_track->_widgets.end(); ++i )
  282. _tempomap.push_back( *i );
  283. for ( list <Sequence_Widget *>::const_iterator i = tempo_track->_widgets.begin();
  284. i != tempo_track->_widgets.end(); ++i )
  285. _tempomap.push_back( *i );
  286. /* FIXME: shouldn't we ensure that time points always precede
  287. tempo points at the same position? */
  288. _tempomap.sort( Sequence_Widget::sort_func );
  289. }
  290. position_info
  291. Timeline::solve_tempomap ( nframes_t frame ) const
  292. {
  293. return render_tempomap( frame, 0, 0, 0 );
  294. }
  295. /* THREAD: UI and RT */
  296. /** draw appropriate measure lines inside the given bounding box */
  297. position_info
  298. Timeline::render_tempomap( nframes_t start, nframes_t length, measure_line_callback * cb, void *arg ) const
  299. {
  300. const nframes_t end = start + length;
  301. position_info pos;
  302. memset( &pos, 0, sizeof( pos ) );
  303. BBT &bbt = pos.bbt;
  304. const nframes_t samples_per_minute = sample_rate() * 60;
  305. float bpm = 120.0f;
  306. time_sig sig;
  307. sig.beats_per_bar = 4;
  308. sig.beat_type = 4;
  309. nframes_t f = 0;
  310. nframes_t next = 0;
  311. nframes_t frames_per_beat = samples_per_minute / bpm;
  312. /* FIXME: don't we need to sort so that Time_Points always preceed Tempo_Points? */
  313. if ( ! _tempomap.size() )
  314. return pos;
  315. for ( list <const Sequence_Widget *>::const_iterator i = _tempomap.begin();
  316. i != _tempomap.end(); ++i )
  317. {
  318. if ( ! strcmp( (*i)->class_name(), "Tempo_Point" ) )
  319. {
  320. const Tempo_Point *p = (Tempo_Point*)(*i);
  321. bpm = p->tempo();
  322. frames_per_beat = samples_per_minute / bpm;
  323. }
  324. else
  325. {
  326. const Time_Point *p = (Time_Point*)(*i);
  327. sig = p->time();
  328. /* Time point resets beat */
  329. bbt.beat = 0;
  330. }
  331. {
  332. list <const Sequence_Widget *>::const_iterator n = i;
  333. ++n;
  334. if ( n == _tempomap.end() )
  335. next = end;
  336. else
  337. // next = min( (*n)->start(), end );
  338. /* points may not always be aligned with beat boundaries, so we must align here */
  339. next = (*n)->start() - ( ( (*n)->start() - (*i)->start() ) % frames_per_beat );
  340. }
  341. for ( ; f < next; ++bbt.beat, f += frames_per_beat )
  342. {
  343. if ( bbt.beat == sig.beats_per_bar )
  344. {
  345. bbt.beat = 0;
  346. ++bbt.bar;
  347. }
  348. if ( f >= start )
  349. {
  350. /* in the zone */
  351. if ( cb )
  352. cb( f, bbt, arg );
  353. }
  354. /* ugliness to avoid failing out at -1 */
  355. if ( end >= frames_per_beat )
  356. {
  357. if ( f >= end - frames_per_beat )
  358. goto done;
  359. }
  360. else if ( f + frames_per_beat >= end )
  361. goto done;
  362. }
  363. }
  364. done:
  365. pos.frame = f;
  366. pos.tempo = bpm;
  367. pos.beats_per_bar = sig.beats_per_bar;
  368. pos.beat_type = sig.beat_type;
  369. assert( f <= end );
  370. assert( end - f <= frames_per_beat );
  371. /* FIXME: this this right? */
  372. const double frames_per_tick = frames_per_beat / ticks_per_beat;
  373. bbt.tick = ( end - f ) / frames_per_tick;
  374. return pos;
  375. }
  376. void
  377. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  378. {
  379. if ( ! draw_with_measure_lines )
  380. return;
  381. Fl_Color colors[2];
  382. colors[1] = fl_color_average( FL_BLACK, color, 0.65f );
  383. colors[0] = fl_color_average( FL_RED, colors[1], 0.65f );
  384. const nframes_t start = x_to_offset( X );
  385. const nframes_t length = x_to_ts( W );
  386. fl_push_clip( X, Y, W, H );
  387. render_tempomap( start, length, draw_measure_cb, &colors );
  388. fl_pop_clip();
  389. }
  390. /* /\** just like draw mesure lines except that it also draws the BBT values. *\/ */
  391. /* void */
  392. /* Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color ) */
  393. /* { */
  394. /* // render_tempomap( X, Y, W, H, color, true ); */
  395. /* } */
  396. void
  397. Timeline::xposition ( int X )
  398. {
  399. // _old_xposition = xoffset;
  400. /* FIXME: shouldn't have to do this... */
  401. X = min( X, ts_to_x( _length ) - tracks->w() - Track::width() );
  402. xoffset = x_to_ts( X );
  403. damage( FL_DAMAGE_SCROLL );
  404. }
  405. void
  406. Timeline::yposition ( int Y )
  407. {
  408. // _old_yposition = _yposition;
  409. _yposition = Y;
  410. damage( FL_DAMAGE_SCROLL );
  411. }
  412. void
  413. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  414. {
  415. Timeline *tl = (Timeline *)v;
  416. fl_push_clip( X, Y, W, H );
  417. /* fl_color( rand() ); */
  418. /* fl_rectf( X, Y, X + W, Y + H ); */
  419. tl->draw_box();
  420. tl->draw_child( *tl->rulers );
  421. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  422. tl->draw_child( *tl->tracks );
  423. fl_pop_clip();
  424. fl_pop_clip();
  425. }
  426. // static unsigned char *rect_image;
  427. void
  428. Timeline::resize ( int X, int Y, int W, int H )
  429. {
  430. Fl_Overlay_Window::resize( X, Y, W, H );
  431. /* why is this necessary? */
  432. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  433. /* why is THIS necessary? */
  434. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  435. vscroll->size( vscroll->w(), H - 18 );
  436. }
  437. void
  438. Timeline::draw ( void )
  439. {
  440. int X, Y, W, H;
  441. int bdx = 0;
  442. int bdw = 0;
  443. X = tracks->x() + bdx + 1;
  444. Y = tracks->y();
  445. W = tracks->w() - bdw - 1;
  446. H = tracks->h();
  447. adjust_vscroll();
  448. /* if ( damage() & FL_DAMAGE_USER1 ) */
  449. /* { */
  450. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  451. /* Rectangle &r = _selection; */
  452. /* make_current(); */
  453. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  454. /* return; */
  455. /* } */
  456. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  457. {
  458. DMESSAGE( "complete redraw" );
  459. draw_box( box(), 0, 0, w(), h(), color() );
  460. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  461. draw_child( *rulers );
  462. fl_pop_clip();
  463. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  464. draw_child( *tracks );
  465. fl_pop_clip();
  466. draw_child( *hscroll );
  467. draw_child( *vscroll );
  468. if ( p1 != p2 )
  469. {
  470. draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
  471. draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
  472. }
  473. redraw_overlay();
  474. /* Rectangle &r = _selection; */
  475. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  476. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  477. /* bi.color_average( FL_BLACK, 0.50f ); */
  478. /* bi.draw( r.x, r.y ); */
  479. /* delete[] data; */
  480. /* if ( r.w && r.h ) */
  481. /* { */
  482. /* const unsigned char data[] = { 0, 127, 0, 96, */
  483. /* 0, 96, 0, 127 }; */
  484. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  485. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  486. /* bi2->draw( r.x, r.y ); */
  487. /* delete bi2; */
  488. /* } */
  489. goto done;
  490. }
  491. if ( damage() & FL_DAMAGE_SCROLL )
  492. {
  493. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  494. int dy = _old_yposition - _yposition;
  495. /* draw_child( *rulers ); */
  496. if ( ! dy )
  497. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  498. Y = rulers->y() + rulers->h();
  499. H = h() - rulers->h() - hscroll->h();
  500. if ( dy == 0 )
  501. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  502. else
  503. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  504. }
  505. if ( damage() & FL_DAMAGE_CHILD )
  506. {
  507. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  508. update_child( *rulers );
  509. fl_pop_clip();
  510. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  511. update_child( *tracks );
  512. fl_pop_clip();
  513. update_child( *hscroll );
  514. update_child( *vscroll );
  515. if ( p1 != p2 )
  516. {
  517. draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
  518. draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
  519. }
  520. }
  521. done:
  522. _old_xposition = xoffset;
  523. _old_yposition = _yposition;
  524. }
  525. void
  526. Timeline::draw_cursor ( nframes_t frame, Fl_Color color, void (*symbol)(Fl_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() - 1;
  537. fl_push_clip( tracks->x() + 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. fl_push_matrix();
  544. fl_translate( x, y );
  545. fl_scale( 16, 8 );
  546. symbol( color );
  547. fl_pop_matrix();
  548. fl_pop_clip();
  549. }
  550. void
  551. Timeline::draw_playhead ( void )
  552. {
  553. draw_cursor( transport->frame, FL_RED, draw_full_arrow_symbol );
  554. }
  555. void
  556. Timeline::redraw_playhead ( void )
  557. {
  558. static nframes_t last_playhead = -1;
  559. if ( last_playhead != transport->frame )
  560. {
  561. redraw_overlay();
  562. last_playhead = transport->frame;
  563. if ( follow_playhead )
  564. {
  565. if ( center_playhead && active() )
  566. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  567. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  568. xposition( ts_to_x( transport->frame ) );
  569. }
  570. }
  571. }
  572. /** called so many times a second to redraw the playhead etc. */
  573. void
  574. Timeline::update_cb ( void *arg )
  575. {
  576. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  577. Timeline *tl = (Timeline *)arg;
  578. tl->redraw_playhead();
  579. }
  580. void
  581. Timeline::draw_overlay ( void )
  582. {
  583. draw_playhead();
  584. if ( ! ( _selection.w && _selection.h ) )
  585. return;
  586. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  587. const Rectangle &r = _selection;
  588. fl_color( FL_BLACK );
  589. fl_line_style( FL_SOLID, 2 );
  590. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  591. fl_color( FL_MAGENTA );
  592. fl_line_style( FL_DASH, 2 );
  593. fl_rect( r.x, r.y, r.w, r.h );
  594. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  595. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  596. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  597. fl_line_style( FL_SOLID, 0 );
  598. /* const unsigned char data[] = { 127, 127, 127, 96, */
  599. /* 127, 96, 127, 40 }; */
  600. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  601. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  602. /* bi2->draw( r.x, r.y ); */
  603. /* delete bi2; */
  604. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  605. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  606. /* bi.color_average( FL_BLACK, 0.50f ); */
  607. /* bi.draw( r.x, r.y ); */
  608. /* delete[] rect_image; */
  609. /* rect_image = NULL; */
  610. fl_pop_clip();
  611. }
  612. // #include "Sequence_Widget.H"
  613. /** select all widgets in inside rectangle /r/ */
  614. void
  615. Timeline::select ( const Rectangle &r )
  616. {
  617. const int Y = r.y;
  618. for ( int i = tracks->children(); i-- ; )
  619. {
  620. Track *t = (Track*)tracks->child( i );
  621. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  622. t->select( r.x, r.y, r.w, r.h, true, true );
  623. }
  624. }
  625. void
  626. Timeline::delete_selected ( void )
  627. {
  628. Sequence_Widget::delete_selected();
  629. }
  630. void
  631. Timeline::select_none ( void )
  632. {
  633. Sequence_Widget::select_none();
  634. }
  635. /** An unfortunate necessity for implementing our own DND aside from
  636. * the (bogus) native FLTK system */
  637. Track *
  638. Timeline::track_under ( int Y )
  639. {
  640. for ( int i = tracks->children(); i-- ; )
  641. {
  642. Track *t = (Track*)tracks->child( i );
  643. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  644. return t;
  645. }
  646. return NULL;
  647. }
  648. int
  649. Timeline::handle ( int m )
  650. {
  651. static Drag *drag = NULL;
  652. static bool range = false;
  653. switch ( m )
  654. {
  655. case FL_FOCUS:
  656. case FL_UNFOCUS:
  657. // redraw();
  658. return 1;
  659. case FL_KEYDOWN:
  660. if ( Fl::event_key() == 'r' )
  661. {
  662. range = true;
  663. return 1;
  664. }
  665. return 0;
  666. case FL_KEYUP:
  667. if ( Fl::event_key() == 'r' )
  668. {
  669. range = false;
  670. return 1;
  671. }
  672. return 0;
  673. // case FL_KEYBOARD:
  674. case FL_SHORTCUT:
  675. {
  676. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  677. /* we don't want any keys with modifiers... */
  678. return 0;
  679. switch ( Fl::event_key() )
  680. {
  681. case FL_Delete:
  682. case FL_Home:
  683. case FL_End:
  684. /* keep scrollbar from eating these. */
  685. return 0;
  686. case 'p':
  687. {
  688. int X = Fl::event_x() - Track::width();
  689. if ( X > 0 )
  690. {
  691. transport->locate( 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. p1 = xoffset + x_to_ts( X );
  701. }
  702. /* FIXME: only needs to damage the location of the old cursor! */
  703. redraw();
  704. return 1;
  705. }
  706. case ']':
  707. {
  708. int X = Fl::event_x() - Track::width();
  709. if ( X > 0 )
  710. {
  711. p2 = xoffset + x_to_ts( X );
  712. }
  713. /* FIXME: only needs to damage the location of the old cursor! */
  714. redraw();
  715. return 1;
  716. }
  717. default:
  718. return Fl_Overlay_Window::handle( m );
  719. }
  720. return 0;
  721. }
  722. default:
  723. {
  724. if ( m == FL_PUSH )
  725. Fl::focus( this );
  726. int r = Fl_Overlay_Window::handle( m );
  727. if ( m != FL_RELEASE && r )
  728. return r;
  729. const int X = Fl::event_x();
  730. const int Y = Fl::event_y();
  731. switch ( m )
  732. {
  733. case FL_PUSH:
  734. {
  735. // take_focus();
  736. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  737. return 0;
  738. if ( Fl::event_button1() )
  739. {
  740. assert( ! drag );
  741. drag = new Drag( X - x(), Y - y() );
  742. _selection.x = drag->x;
  743. _selection.y = drag->y;
  744. }
  745. else if ( Fl::test_shortcut( FL_BUTTON3 ) && ! Fl::event_shift() )
  746. {
  747. Fl_Menu_Item menu[] =
  748. {
  749. { "Add Track", 0, 0, 0, FL_SUBMENU },
  750. { "Audio", 0, 0, 0 },
  751. { 0 },
  752. { 0 },
  753. };
  754. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  755. if ( r == &menu[1] )
  756. {
  757. /* FIXME: prompt for I/O config? */
  758. Loggable::block_start();
  759. /* add audio track */
  760. char *name = get_unique_track_name( "Audio" );
  761. Track *t = new Track( name );
  762. Audio_Sequence *o = new Audio_Sequence( t );
  763. // new Control_Sequence( t );
  764. add_track( t );
  765. t->sequence( o );
  766. Loggable::block_end();
  767. }
  768. }
  769. else
  770. return 0;
  771. break;
  772. }
  773. case FL_DRAG:
  774. {
  775. int ox = X - drag->x;
  776. int oy = Y - drag->y;
  777. if ( ox < 0 )
  778. _selection.x = X;
  779. if ( oy < 0 )
  780. _selection.y = Y;
  781. _selection.w = abs( ox );
  782. _selection.h = abs( oy );
  783. if ( range )
  784. {
  785. p1 = x_to_offset( _selection.x );
  786. p2 = x_to_offset( _selection.x + _selection.w );
  787. redraw();
  788. }
  789. break;
  790. }
  791. case FL_RELEASE:
  792. {
  793. delete drag;
  794. drag = NULL;
  795. if ( range )
  796. {
  797. p1 = x_to_offset( _selection.x );
  798. p2 = x_to_offset( _selection.x + _selection.w );
  799. redraw();
  800. }
  801. else
  802. select( _selection );
  803. _selection.w = _selection.h = 0;
  804. break;
  805. }
  806. default:
  807. return 0;
  808. break;
  809. }
  810. redraw_overlay();
  811. return 1;
  812. }
  813. }
  814. }
  815. void
  816. Timeline::zoom_in ( void )
  817. {
  818. hscroll->zoom_in();
  819. }
  820. void
  821. Timeline::zoom_out ( void )
  822. {
  823. hscroll->zoom_out();
  824. }
  825. /** zoom the display to show /secs/ seconds per screen */
  826. void
  827. Timeline::zoom ( float secs )
  828. {
  829. const int sw = tracks->w() - Track::width();
  830. int fpp = (int)((secs * sample_rate()) / sw);
  831. int p = 0;
  832. while ( 1 << p < fpp ) p++;
  833. hscroll->zoom( p );
  834. redraw();
  835. }
  836. void
  837. Timeline::zoom_fit ( void )
  838. {
  839. zoom( _length / (float)sample_rate() );
  840. }
  841. Track *
  842. Timeline::track_by_name ( const char *name )
  843. {
  844. for ( int i = tracks->children(); i-- ; )
  845. {
  846. Track *t = (Track*)tracks->child( i );
  847. if ( ! strcmp( name, t->name() ) )
  848. return t;
  849. }
  850. return NULL;
  851. }
  852. char *
  853. Timeline::get_unique_track_name ( const char *name )
  854. {
  855. char pat[256];
  856. strcpy( pat, name );
  857. for ( int i = 1; track_by_name( pat ); ++i )
  858. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  859. return strdup( pat );
  860. }
  861. void
  862. Timeline::add_track ( Track *track )
  863. {
  864. DMESSAGE( "added new track to the timeline" );
  865. /* FIXME: do locking */
  866. tracks->add( track );
  867. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  868. redraw();
  869. }
  870. void
  871. Timeline::remove_track ( Track *track )
  872. {
  873. DMESSAGE( "removed track from the timeline" );
  874. /* FIXME: do locking */
  875. /* FIXME: what to do about track contents? */
  876. tracks->remove( track );
  877. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  878. redraw();
  879. }
  880. /** Initiate recording for all armed tracks */
  881. bool
  882. Timeline::record ( void )
  883. {
  884. /* FIXME: right place for this? */
  885. transport->recording = true;
  886. Loggable::block_start();
  887. for ( int i = tracks->children(); i-- ; )
  888. {
  889. Track *t = (Track*)tracks->child( i );
  890. if ( t->armed() && t->record_ds )
  891. t->record_ds->start( transport->frame );
  892. }
  893. deactivate();
  894. return true;
  895. }
  896. /** stop recording for all armed tracks */
  897. void
  898. Timeline::stop ( void )
  899. {
  900. nframes_t frame = transport->frame;
  901. for ( int i = tracks->children(); i-- ; )
  902. {
  903. Track *t = (Track*)tracks->child( i );
  904. if ( t->armed() && t->record_ds )
  905. t->record_ds->stop( frame );
  906. }
  907. Loggable::block_end();
  908. activate();
  909. transport->recording = false;
  910. }
  911. /**********/
  912. /* Engine */
  913. /**********/
  914. /** call process() on each track header */
  915. nframes_t
  916. Timeline::process ( nframes_t nframes )
  917. {
  918. for ( int i = tracks->children(); i-- ; )
  919. {
  920. Track *t = (Track*)tracks->child( i );
  921. t->process( nframes );
  922. }
  923. /* FIXME: BOGUS */
  924. return nframes;
  925. }
  926. /* THREAD: RT */
  927. void
  928. Timeline::seek ( nframes_t frame )
  929. {
  930. for ( int i = tracks->children(); i-- ; )
  931. {
  932. Track *t = (Track*)tracks->child( i );
  933. t->seek( frame );
  934. }
  935. }
  936. /* THREAD: RT (non-RT) */
  937. void
  938. Timeline::resize_buffers ( nframes_t nframes )
  939. {
  940. for ( int i = tracks->children(); i-- ; )
  941. {
  942. Track *t = (Track*)tracks->child( i );
  943. t->resize_buffers( nframes );
  944. }
  945. }
  946. /* THREAD: RT */
  947. int
  948. Timeline::seek_pending ( void )
  949. {
  950. int r = 0;
  951. for ( int i = tracks->children(); i-- ; )
  952. {
  953. Track *t = (Track*)tracks->child( i );
  954. if ( t->playback_ds )
  955. r += t->playback_ds->buffer_percent() < 50;
  956. }
  957. return r;
  958. }
  959. /* FIXME: shouldn't these belong to the engine? */
  960. int
  961. Timeline::total_input_buffer_percent ( void )
  962. {
  963. int r = 0;
  964. int cnt = 0;
  965. for ( int i = tracks->children(); i-- ; )
  966. {
  967. Track *t = (Track*)tracks->child( i );
  968. if ( t->record_ds )
  969. {
  970. ++cnt;
  971. r += t->record_ds->buffer_percent();
  972. }
  973. }
  974. if ( ! cnt )
  975. return 0;
  976. return r / cnt;
  977. }
  978. int
  979. Timeline::total_output_buffer_percent ( void )
  980. {
  981. int r = 0;
  982. int cnt = 0;
  983. for ( int i = tracks->children(); i-- ; )
  984. {
  985. Track *t = (Track*)tracks->child( i );
  986. if ( t->playback_ds )
  987. {
  988. ++cnt;
  989. r += t->playback_ds->buffer_percent();
  990. }
  991. }
  992. if ( ! cnt )
  993. return 0;
  994. return r / cnt;
  995. }
  996. /** wait for I/O threads to fill their buffers */
  997. void
  998. Timeline::wait_for_buffers ( void )
  999. {
  1000. while ( total_output_buffer_percent() + total_input_buffer_percent() < 200 )
  1001. usleep( 5000 );
  1002. }
  1003. int
  1004. Timeline::total_playback_xruns ( void )
  1005. {
  1006. int r = 0;
  1007. for ( int i = tracks->children(); i-- ; )
  1008. {
  1009. Track *t = (Track*)tracks->child( i );
  1010. if ( t->playback_ds )
  1011. r += t->playback_ds->xruns();
  1012. }
  1013. return r;
  1014. }
  1015. int
  1016. Timeline::total_capture_xruns ( void )
  1017. {
  1018. int r = 0;
  1019. for ( int i = tracks->children(); i-- ; )
  1020. {
  1021. Track *t = (Track*)tracks->child( i );
  1022. if ( t->record_ds )
  1023. r += t->record_ds->xruns();
  1024. }
  1025. return r;
  1026. }