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
30KB

  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. }
  516. done:
  517. _old_xposition = xoffset;
  518. _old_yposition = _yposition;
  519. }
  520. void
  521. Timeline::draw_cursor ( nframes_t frame, Fl_Color color, void (*symbol)(Fl_Color) )
  522. {
  523. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  524. if ( frame < xoffset )
  525. return;
  526. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  527. if ( x > tracks->x() + tracks->w() )
  528. return;
  529. fl_color( color );
  530. const int y = rulers->y() + rulers->h();
  531. const int h = this->h() - hscroll->h() - 1;
  532. fl_push_clip( tracks->x() + Track::width(), y, tracks->w(), h );
  533. fl_line( x, y, x, h );
  534. fl_color( fl_darker( color ) );
  535. fl_line( x - 1, y, x - 1, h );
  536. fl_color( FL_BLACK );
  537. fl_line( x + 1, y, x + 1, h );
  538. fl_push_matrix();
  539. fl_translate( x, y );
  540. fl_scale( 16, 8 );
  541. symbol( color );
  542. fl_pop_matrix();
  543. fl_pop_clip();
  544. }
  545. void
  546. Timeline::draw_playhead ( void )
  547. {
  548. draw_cursor( transport->frame, FL_RED, draw_full_arrow_symbol );
  549. }
  550. void
  551. Timeline::redraw_playhead ( void )
  552. {
  553. static nframes_t last_playhead = -1;
  554. if ( last_playhead != transport->frame )
  555. {
  556. redraw_overlay();
  557. last_playhead = transport->frame;
  558. if ( follow_playhead )
  559. {
  560. if ( center_playhead && active() )
  561. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  562. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  563. xposition( ts_to_x( transport->frame ) );
  564. }
  565. }
  566. }
  567. /** called so many times a second to redraw the playhead etc. */
  568. void
  569. Timeline::update_cb ( void *arg )
  570. {
  571. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  572. Timeline *tl = (Timeline *)arg;
  573. tl->redraw_playhead();
  574. }
  575. void
  576. Timeline::draw_overlay ( void )
  577. {
  578. draw_playhead();
  579. if ( ! ( _selection.w && _selection.h ) )
  580. return;
  581. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  582. const Rectangle &r = _selection;
  583. fl_color( FL_BLACK );
  584. fl_line_style( FL_SOLID, 2 );
  585. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  586. fl_color( FL_MAGENTA );
  587. fl_line_style( FL_DASH, 2 );
  588. fl_rect( r.x, r.y, r.w, r.h );
  589. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  590. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  591. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  592. fl_line_style( FL_SOLID, 0 );
  593. /* const unsigned char data[] = { 127, 127, 127, 96, */
  594. /* 127, 96, 127, 40 }; */
  595. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  596. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  597. /* bi2->draw( r.x, r.y ); */
  598. /* delete bi2; */
  599. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  600. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  601. /* bi.color_average( FL_BLACK, 0.50f ); */
  602. /* bi.draw( r.x, r.y ); */
  603. /* delete[] rect_image; */
  604. /* rect_image = NULL; */
  605. fl_pop_clip();
  606. }
  607. // #include "Sequence_Widget.H"
  608. /** select all widgets in inside rectangle /r/ */
  609. void
  610. Timeline::select ( const Rectangle &r )
  611. {
  612. const int Y = r.y;
  613. for ( int i = tracks->children(); i-- ; )
  614. {
  615. Track *t = (Track*)tracks->child( i );
  616. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  617. t->select( r.x, r.y, r.w, r.h, true, true );
  618. }
  619. }
  620. void
  621. Timeline::delete_selected ( void )
  622. {
  623. Sequence_Widget::delete_selected();
  624. }
  625. void
  626. Timeline::select_none ( void )
  627. {
  628. Sequence_Widget::select_none();
  629. }
  630. /** An unfortunate necessity for implementing our own DND aside from
  631. * the (bogus) native FLTK system */
  632. Track *
  633. Timeline::track_under ( int Y )
  634. {
  635. for ( int i = tracks->children(); i-- ; )
  636. {
  637. Track *t = (Track*)tracks->child( i );
  638. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  639. return t;
  640. }
  641. return NULL;
  642. }
  643. int
  644. Timeline::handle ( int m )
  645. {
  646. static Drag *drag = NULL;
  647. switch ( m )
  648. {
  649. case FL_FOCUS:
  650. case FL_UNFOCUS:
  651. // redraw();
  652. return 1;
  653. case FL_KEYBOARD:
  654. case FL_SHORTCUT:
  655. {
  656. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  657. /* we don't want any keys with modifiers... */
  658. return 0;
  659. switch ( Fl::event_key() )
  660. {
  661. case FL_Delete:
  662. case FL_Home:
  663. case FL_End:
  664. /* keep scrollbar from eating these. */
  665. return 0;
  666. case 'p':
  667. {
  668. int X = Fl::event_x() - Track::width();
  669. if ( X > 0 )
  670. {
  671. transport->locate( xoffset + x_to_ts( X ) );
  672. }
  673. return 1;
  674. }
  675. case '[':
  676. {
  677. int X = Fl::event_x() - Track::width();
  678. if ( X > 0 )
  679. {
  680. p1 = xoffset + x_to_ts( X );
  681. }
  682. /* FIXME: only needs to damage the location of the old cursor! */
  683. redraw();
  684. return 1;
  685. }
  686. case ']':
  687. {
  688. int X = Fl::event_x() - Track::width();
  689. if ( X > 0 )
  690. {
  691. p2 = xoffset + x_to_ts( X );
  692. }
  693. /* FIXME: only needs to damage the location of the old cursor! */
  694. redraw();
  695. return 1;
  696. }
  697. default:
  698. return Fl_Overlay_Window::handle( m );
  699. }
  700. return 0;
  701. }
  702. default:
  703. {
  704. if ( m == FL_PUSH )
  705. Fl::focus( this );
  706. int r = Fl_Overlay_Window::handle( m );
  707. if ( m != FL_RELEASE && r )
  708. return r;
  709. const int X = Fl::event_x();
  710. const int Y = Fl::event_y();
  711. switch ( m )
  712. {
  713. case FL_PUSH:
  714. {
  715. // take_focus();
  716. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  717. return 0;
  718. if ( Fl::event_button1() )
  719. {
  720. assert( ! drag );
  721. drag = new Drag( X - x(), Y - y() );
  722. _selection.x = drag->x;
  723. _selection.y = drag->y;
  724. }
  725. else if ( Fl::test_shortcut( FL_BUTTON3 ) && ! Fl::event_shift() )
  726. {
  727. Fl_Menu_Item menu[] =
  728. {
  729. { "Add Track", 0, 0, 0, FL_SUBMENU },
  730. { "Audio", 0, 0, 0 },
  731. { 0 },
  732. { 0 },
  733. };
  734. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  735. if ( r == &menu[1] )
  736. {
  737. /* FIXME: prompt for I/O config? */
  738. Loggable::block_start();
  739. /* add audio track */
  740. char *name = get_unique_track_name( "Audio" );
  741. Track *t = new Track( name );
  742. Audio_Sequence *o = new Audio_Sequence( t );
  743. // new Control_Sequence( t );
  744. add_track( t );
  745. t->sequence( o );
  746. Loggable::block_end();
  747. }
  748. }
  749. else
  750. return 0;
  751. break;
  752. }
  753. case FL_DRAG:
  754. {
  755. int ox = X - drag->x;
  756. int oy = Y - drag->y;
  757. if ( ox < 0 )
  758. _selection.x = X;
  759. if ( oy < 0 )
  760. _selection.y = Y;
  761. _selection.w = abs( ox );
  762. _selection.h = abs( oy );
  763. break;
  764. }
  765. case FL_RELEASE:
  766. {
  767. delete drag;
  768. drag = NULL;
  769. select( _selection );
  770. _selection.w = _selection.h = 0;
  771. break;
  772. }
  773. default:
  774. return 0;
  775. break;
  776. }
  777. redraw_overlay();
  778. return 1;
  779. }
  780. }
  781. }
  782. void
  783. Timeline::zoom_in ( void )
  784. {
  785. hscroll->zoom_in();
  786. }
  787. void
  788. Timeline::zoom_out ( void )
  789. {
  790. hscroll->zoom_out();
  791. }
  792. /** zoom the display to show /secs/ seconds per screen */
  793. void
  794. Timeline::zoom ( float secs )
  795. {
  796. const int sw = tracks->w() - Track::width();
  797. int fpp = (int)((secs * sample_rate()) / sw);
  798. int p = 0;
  799. while ( 1 << p < fpp ) p++;
  800. hscroll->zoom( p );
  801. redraw();
  802. }
  803. void
  804. Timeline::zoom_fit ( void )
  805. {
  806. zoom( _length / (float)sample_rate() );
  807. }
  808. Track *
  809. Timeline::track_by_name ( const char *name )
  810. {
  811. for ( int i = tracks->children(); i-- ; )
  812. {
  813. Track *t = (Track*)tracks->child( i );
  814. if ( ! strcmp( name, t->name() ) )
  815. return t;
  816. }
  817. return NULL;
  818. }
  819. char *
  820. Timeline::get_unique_track_name ( const char *name )
  821. {
  822. char pat[256];
  823. strcpy( pat, name );
  824. for ( int i = 1; track_by_name( pat ); ++i )
  825. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  826. return strdup( pat );
  827. }
  828. void
  829. Timeline::add_track ( Track *track )
  830. {
  831. DMESSAGE( "added new track to the timeline" );
  832. /* FIXME: do locking */
  833. tracks->add( track );
  834. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  835. redraw();
  836. }
  837. void
  838. Timeline::remove_track ( Track *track )
  839. {
  840. DMESSAGE( "removed track from the timeline" );
  841. /* FIXME: do locking */
  842. /* FIXME: what to do about track contents? */
  843. tracks->remove( track );
  844. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  845. redraw();
  846. }
  847. /** Initiate recording for all armed tracks */
  848. bool
  849. Timeline::record ( void )
  850. {
  851. /* FIXME: right place for this? */
  852. transport->recording = true;
  853. Loggable::block_start();
  854. for ( int i = tracks->children(); i-- ; )
  855. {
  856. Track *t = (Track*)tracks->child( i );
  857. if ( t->armed() && t->record_ds )
  858. t->record_ds->start( transport->frame );
  859. }
  860. deactivate();
  861. return true;
  862. }
  863. /** stop recording for all armed tracks */
  864. void
  865. Timeline::stop ( void )
  866. {
  867. nframes_t frame = transport->frame;
  868. for ( int i = tracks->children(); i-- ; )
  869. {
  870. Track *t = (Track*)tracks->child( i );
  871. if ( t->armed() && t->record_ds )
  872. t->record_ds->stop( frame );
  873. }
  874. Loggable::block_end();
  875. activate();
  876. transport->recording = false;
  877. }
  878. /**********/
  879. /* Engine */
  880. /**********/
  881. /** call process() on each track header */
  882. nframes_t
  883. Timeline::process ( nframes_t nframes )
  884. {
  885. for ( int i = tracks->children(); i-- ; )
  886. {
  887. Track *t = (Track*)tracks->child( i );
  888. t->process( nframes );
  889. }
  890. /* FIXME: BOGUS */
  891. return nframes;
  892. }
  893. /* THREAD: RT */
  894. void
  895. Timeline::seek ( nframes_t frame )
  896. {
  897. for ( int i = tracks->children(); i-- ; )
  898. {
  899. Track *t = (Track*)tracks->child( i );
  900. t->seek( frame );
  901. }
  902. }
  903. /* THREAD: RT (non-RT) */
  904. void
  905. Timeline::resize_buffers ( nframes_t nframes )
  906. {
  907. for ( int i = tracks->children(); i-- ; )
  908. {
  909. Track *t = (Track*)tracks->child( i );
  910. t->resize_buffers( nframes );
  911. }
  912. }
  913. /* THREAD: RT */
  914. int
  915. Timeline::seek_pending ( void )
  916. {
  917. int r = 0;
  918. for ( int i = tracks->children(); i-- ; )
  919. {
  920. Track *t = (Track*)tracks->child( i );
  921. if ( t->playback_ds )
  922. r += t->playback_ds->buffer_percent() < 50;
  923. }
  924. return r;
  925. }
  926. /* FIXME: shouldn't these belong to the engine? */
  927. int
  928. Timeline::total_input_buffer_percent ( void )
  929. {
  930. int r = 0;
  931. int cnt = 0;
  932. for ( int i = tracks->children(); i-- ; )
  933. {
  934. Track *t = (Track*)tracks->child( i );
  935. if ( t->record_ds )
  936. {
  937. ++cnt;
  938. r += t->record_ds->buffer_percent();
  939. }
  940. }
  941. if ( ! cnt )
  942. return 0;
  943. return r / cnt;
  944. }
  945. int
  946. Timeline::total_output_buffer_percent ( void )
  947. {
  948. int r = 0;
  949. int cnt = 0;
  950. for ( int i = tracks->children(); i-- ; )
  951. {
  952. Track *t = (Track*)tracks->child( i );
  953. if ( t->playback_ds )
  954. {
  955. ++cnt;
  956. r += t->playback_ds->buffer_percent();
  957. }
  958. }
  959. if ( ! cnt )
  960. return 0;
  961. return r / cnt;
  962. }
  963. /** wait for I/O threads to fill their buffers */
  964. void
  965. Timeline::wait_for_buffers ( void )
  966. {
  967. while ( total_output_buffer_percent() + total_input_buffer_percent() < 200 )
  968. usleep( 5000 );
  969. }
  970. int
  971. Timeline::total_playback_xruns ( void )
  972. {
  973. int r = 0;
  974. for ( int i = tracks->children(); i-- ; )
  975. {
  976. Track *t = (Track*)tracks->child( i );
  977. if ( t->playback_ds )
  978. r += t->playback_ds->xruns();
  979. }
  980. return r;
  981. }
  982. int
  983. Timeline::total_capture_xruns ( void )
  984. {
  985. int r = 0;
  986. for ( int i = tracks->children(); i-- ; )
  987. {
  988. Track *t = (Track*)tracks->child( i );
  989. if ( t->record_ds )
  990. r += t->record_ds->xruns();
  991. }
  992. return r;
  993. }