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.

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