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.

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