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.

1286 lines
29KB

  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 "Ruler_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. _fpp = hscroll->zoom();
  75. const int tw = tracks->w() - Track::width() - vscroll->w();
  76. hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( _length ) );
  77. redraw();
  78. }
  79. xposition( hscroll->value() );
  80. }
  81. }
  82. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  83. {
  84. box( FL_FLAT_BOX );
  85. xoffset = 0;
  86. _yposition = 0;
  87. _old_yposition = 0;
  88. _old_xposition = 0;
  89. X = Y = 0;
  90. p1 = p2 = 0;
  91. {
  92. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  93. o->range( 0, 48000 * 300 );
  94. // o->zoom_range( 1, 16384 );
  95. // o->zoom_range( 1, 65536 << 4 );
  96. o->zoom_range( 1, 20 );
  97. o->zoom( 8 );
  98. o->type( FL_HORIZONTAL );
  99. o->callback( cb_scroll, this );
  100. hscroll = o;
  101. }
  102. {
  103. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  104. o->type( FL_VERTICAL );
  105. o->callback( cb_scroll, this );
  106. vscroll = o;
  107. }
  108. {
  109. Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  110. o->type( Fl_Pack::VERTICAL );
  111. {
  112. Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
  113. o->color( fl_gray_ramp( 18 ) );
  114. o->label( "Tempo" );
  115. o->align( FL_ALIGN_LEFT );
  116. tempo_track = o;
  117. o->beats_per_minute( 0, 120 );
  118. o->beats_per_minute( 48000 * 50, 250 );
  119. o->beats_per_minute( 48000 * 120, 60 );
  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. o->time( 0, 4, 4 );
  128. }
  129. {
  130. Ruler_Sequence *o = new Ruler_Sequence( 0, 24, 800, 24 );
  131. o->color( fl_gray_ramp( 'F' ) );
  132. o->label( "Ruler" );
  133. o->align( FL_ALIGN_LEFT );
  134. ruler_track = o;
  135. }
  136. o->size( o->w(), o->child( 0 )->h() * o->children() );
  137. rulers = o;
  138. o->end();
  139. }
  140. {
  141. // sample_rate() = engine->sample_rate();
  142. _fpp = 8;
  143. // _length = sample_rate() * 60 * 2;
  144. /* FIXME: hack */
  145. _length = -1;
  146. {
  147. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
  148. o->type( Fl_Pack::VERTICAL );
  149. o->spacing( 1 );
  150. tracks = o;
  151. o->end();
  152. resizable( o );
  153. }
  154. }
  155. /* rulers go above tracks... */
  156. add( rulers );
  157. /* make sure scrollbars are on top */
  158. add( vscroll );
  159. add( hscroll );
  160. vscroll->range( 0, tracks->h() );
  161. redraw();
  162. end();
  163. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  164. }
  165. float
  166. Timeline::beats_per_minute ( nframes_t when ) const
  167. {
  168. return tempo_track->beats_per_minute( when );
  169. }
  170. int
  171. Timeline::beats_per_bar ( nframes_t when ) const
  172. {
  173. time_sig t = time_track->time( when );
  174. return t.beats_per_bar;
  175. }
  176. void
  177. Timeline::beats_per_minute ( nframes_t when, float bpm )
  178. {
  179. tempo_track->add( new Tempo_Point( when, bpm ) );
  180. }
  181. #if 0
  182. struct BBT
  183. {
  184. int bar, beat, tick;
  185. BBT ( int bar, int beat, int tick ) : bar( bar ), beat( beat ), tick( tick )
  186. {
  187. }
  188. };
  189. /** returns the BBT value for timestamp /when/ by examining the tempo/time maps */
  190. BBT
  191. Timeline::bbt ( nframes_t when )
  192. {
  193. Tempo_Sequence *tempo = (Tempo_Sequence*)rulers->child( 0 );
  194. BBT bbt;
  195. for ( list <Sequence_Widget *>::const_iterator i = tempo.widgets.begin();
  196. i != tempo.widgets.end(); ++i )
  197. {
  198. Tempo_Point *p = *i;
  199. }
  200. };
  201. #endif
  202. /** return the absolute pixel of the nearest measure line to /x/ */
  203. int
  204. Timeline::nearest_line ( int ix )
  205. {
  206. if ( snap_to == None )
  207. return -1;
  208. const nframes_t samples_per_minute = sample_rate() * 60;
  209. for ( int x = ix - 10; x < ix + 10; ++x )
  210. {
  211. const int measure = ts_to_x( samples_per_minute / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  212. if ( measure == 0 )
  213. break;
  214. // const int abs_x = ts_to_x( xoffset ) + x - Track::width();
  215. const int abs_x = ts_to_x( xoffset ) + x;
  216. int bpb = beats_per_bar( x_to_ts( x -Track::width() ) + xoffset );
  217. if ( 0 == x % measure )
  218. {
  219. if ( snap_to == Bars )
  220. {
  221. if ( 0 == (abs_x / measure) % bpb )
  222. return x;
  223. }
  224. else if ( snap_to == Beats )
  225. {
  226. return x;
  227. }
  228. }
  229. }
  230. return -1;
  231. }
  232. nframes_t
  233. Timeline::x_to_offset ( int x ) const
  234. {
  235. return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
  236. }
  237. /** draw appropriate measure lines inside the given bounding box */
  238. /* FIXME: this function *really* needs to be optimized. Currently it
  239. searched both the time and tempo lists once for every horiontal
  240. pixel and performs a number of calculations--this is slow. */
  241. void
  242. Timeline::draw_measure ( int X, int Y, int W, int H, Fl_Color color, bool BBT )
  243. {
  244. if ( ! draw_with_measure_lines )
  245. return;
  246. // fl_push_clip( X, Y, W, H );
  247. // fl_line_style( FL_DASH, 2 );
  248. fl_line_style( FL_DASH, 0 );
  249. const Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  250. const Fl_Color bar = fl_color_average( FL_RED, beat, 0.65f );
  251. const nframes_t samples_per_minute = sample_rate() * 60;
  252. /* we need to back up a bit in order to catch all the numbers */
  253. /* if ( BBT ) */
  254. /* { */
  255. /* X -= 40; */
  256. /* W += 40; */
  257. /* } */
  258. nframes_t when = x_to_offset( X );
  259. list <Sequence_Widget*>::const_iterator tpi;
  260. list <Sequence_Widget*>::const_iterator mpi;
  261. /* find the first points before our range */
  262. for ( list <Sequence_Widget *>::const_reverse_iterator i = tempo_track->_widgets.rbegin();
  263. i != tempo_track->_widgets.rend(); i++ )
  264. if ( (*i)->offset() <= when )
  265. {
  266. tpi = i.base();
  267. break;
  268. }
  269. for ( list <Sequence_Widget *>::const_reverse_iterator i = time_track->_widgets.rbegin();
  270. i != time_track->_widgets.rend(); i++ )
  271. if ( (*i)->offset() <= when )
  272. {
  273. mpi = i.base();
  274. break;
  275. }
  276. --tpi;
  277. /* start on the next beat */
  278. const Tempo_Point *tp = (Tempo_Point*)(*tpi);
  279. nframes_t beat_inc = samples_per_minute / tp->tempo();
  280. nframes_t f = when - ( ( when - tp->offset() ) % beat_inc );
  281. for ( ; tpi != tempo_track->_widgets.end(); ++tpi )
  282. {
  283. list <Sequence_Widget*>::const_iterator ntpi = tpi;
  284. ++ntpi;
  285. tp = (Tempo_Point*)(*tpi);
  286. const Tempo_Point *ntp = (Tempo_Point*)(*ntpi);
  287. const nframes_t ntpo = ntp ? ntp->offset() : when + x_to_ts( W + 1 );
  288. beat_inc = samples_per_minute / tp->tempo();
  289. const int incx = ts_to_x( beat_inc );
  290. if ( incx < 8 )
  291. continue;
  292. for ( ; f < ntpo; f += beat_inc )
  293. {
  294. const int x = ts_to_x( f - xoffset ) + Track::width();
  295. fl_color( beat );
  296. fl_line( x, Y, x, Y + H );
  297. }
  298. }
  299. #if 0
  300. for ( int x = X; x < X + W; ++x )
  301. {
  302. // measure = ts_to_x( (double)(sample_rate() * 60) / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  303. const int measure = ts_to_x( samples_per_minute / beats_per_minute( x_to_ts( x - Track::width() ) + xoffset ) );
  304. if ( measure == 0 )
  305. break;
  306. const int abs_x = ts_to_x( xoffset ) + x - Track::width();
  307. if ( 0 == abs_x % measure )
  308. {
  309. int bpb = beats_per_bar( x_to_ts( x -Track::width() ) + xoffset );
  310. if ( 0 == (abs_x / measure) % bpb )
  311. {
  312. if ( measure * bpb < 8 )
  313. break;
  314. if ( BBT )
  315. {
  316. fl_font( FL_HELVETICA, 14 );
  317. fl_color( fl_lighter( color ) );
  318. char pat[40];
  319. const nframes_t ts = x_to_ts( abs_x );
  320. // if ( draw_hms )
  321. {
  322. const double seconds = (double)ts / sample_rate();
  323. int S = (int)seconds;
  324. int M = S / 60; S -= M * 60;
  325. int H = M / 60; M -= H * 60;
  326. snprintf( pat, sizeof( pat ), "%d:%d:%d", H, M, S );
  327. }
  328. // else if ( draw_bbt )
  329. {
  330. /* const int bar = */
  331. /* const int beat; */
  332. /* const int tick = 0; */
  333. /* snprintf( pat, sizeof( pat ), "%d:%d:%d", bar, beat, tick ); */
  334. }
  335. fl_draw( pat, x, Y + 14 );
  336. }
  337. fl_color( bar );
  338. }
  339. else
  340. {
  341. if ( measure < 8 )
  342. continue;
  343. fl_color( beat );
  344. }
  345. fl_line( x, Y, x, Y + H );
  346. }
  347. }
  348. #endif
  349. fl_line_style( FL_SOLID, 0 );
  350. // fl_pop_clip();
  351. }
  352. void
  353. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  354. {
  355. draw_measure( X, Y, W, H, color, false );
  356. }
  357. /** just like draw mesure lines except that it also draws the BBT values. */
  358. void
  359. Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color )
  360. {
  361. draw_measure( X, Y, W, H, color, true );
  362. }
  363. void
  364. Timeline::xposition ( int X )
  365. {
  366. // _old_xposition = xoffset;
  367. /* FIXME: shouldn't have to do this... */
  368. X = min( X, ts_to_x( _length ) - tracks->w() - Track::width() );
  369. xoffset = x_to_ts( X );
  370. damage( FL_DAMAGE_SCROLL );
  371. }
  372. void
  373. Timeline::yposition ( int Y )
  374. {
  375. // _old_yposition = _yposition;
  376. _yposition = Y;
  377. damage( FL_DAMAGE_SCROLL );
  378. }
  379. void
  380. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  381. {
  382. Timeline *tl = (Timeline *)v;
  383. fl_push_clip( X, Y, W, H );
  384. /* fl_color( rand() ); */
  385. /* fl_rectf( X, Y, X + W, Y + H ); */
  386. tl->draw_box();
  387. tl->draw_child( *tl->rulers );
  388. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  389. tl->draw_child( *tl->tracks );
  390. fl_pop_clip();
  391. fl_pop_clip();
  392. }
  393. // static unsigned char *rect_image;
  394. void
  395. Timeline::resize ( int X, int Y, int W, int H )
  396. {
  397. Fl_Overlay_Window::resize( X, Y, W, H );
  398. /* why is this necessary? */
  399. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  400. /* why is THIS necessary? */
  401. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  402. vscroll->size( vscroll->w(), H - 18 );
  403. }
  404. void
  405. Timeline::draw ( void )
  406. {
  407. int X, Y, W, H;
  408. int bdx = 0;
  409. int bdw = 0;
  410. X = tracks->x() + bdx + 1;
  411. Y = tracks->y();
  412. W = tracks->w() - bdw - 1;
  413. H = tracks->h();
  414. adjust_vscroll();
  415. /* if ( damage() & FL_DAMAGE_USER1 ) */
  416. /* { */
  417. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  418. /* Rectangle &r = _selection; */
  419. /* make_current(); */
  420. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  421. /* return; */
  422. /* } */
  423. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  424. {
  425. printf( "complete redraw\n" );
  426. draw_box( box(), 0, 0, w(), h(), color() );
  427. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  428. draw_child( *rulers );
  429. fl_pop_clip();
  430. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  431. draw_child( *tracks );
  432. fl_pop_clip();
  433. draw_child( *hscroll );
  434. draw_child( *vscroll );
  435. redraw_overlay();
  436. /* Rectangle &r = _selection; */
  437. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  438. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  439. /* bi.color_average( FL_BLACK, 0.50f ); */
  440. /* bi.draw( r.x, r.y ); */
  441. /* delete[] data; */
  442. /* if ( r.w && r.h ) */
  443. /* { */
  444. /* const unsigned char data[] = { 0, 127, 0, 96, */
  445. /* 0, 96, 0, 127 }; */
  446. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  447. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  448. /* bi2->draw( r.x, r.y ); */
  449. /* delete bi2; */
  450. /* } */
  451. return;
  452. }
  453. if ( damage() & FL_DAMAGE_SCROLL )
  454. {
  455. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  456. int dy = _old_yposition - _yposition;
  457. /* draw_child( *rulers ); */
  458. if ( ! dy )
  459. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  460. Y = rulers->y() + rulers->h();
  461. H = h() - rulers->h() - hscroll->h();
  462. if ( dy == 0 )
  463. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  464. else
  465. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  466. }
  467. _old_xposition = xoffset;
  468. _old_yposition = _yposition;
  469. if ( damage() & FL_DAMAGE_CHILD )
  470. {
  471. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  472. update_child( *rulers );
  473. fl_pop_clip();
  474. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  475. update_child( *tracks );
  476. fl_pop_clip();
  477. update_child( *hscroll );
  478. update_child( *vscroll );
  479. }
  480. }
  481. void
  482. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  483. {
  484. int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  485. if ( x < tracks->x() + Track::width() || x > tracks->x() + tracks->w() )
  486. return;
  487. fl_color( color );
  488. int y = rulers->y() + rulers->h();
  489. int h = this->h() - hscroll->h();
  490. fl_push_clip( Track::width(), y, tracks->w(), h );
  491. fl_line( x, y, x, h );
  492. fl_color( fl_darker( color ) );
  493. fl_line( x - 1, y, x - 1, h );
  494. fl_color( FL_BLACK );
  495. fl_line( x + 1, y, x + 1, h );
  496. /* draw cap */
  497. fl_color( color );
  498. fl_begin_polygon();
  499. fl_vertex( x - 8, y );
  500. fl_vertex( x, y + 8 );
  501. fl_vertex( x + 8, y );
  502. fl_end_polygon();
  503. /* draw cap outline */
  504. fl_color( FL_BLACK );
  505. fl_begin_line();
  506. fl_vertex( x - 8, y );
  507. fl_vertex( x, y + 8 );
  508. fl_vertex( x + 8, y );
  509. fl_end_line();
  510. fl_pop_clip();
  511. }
  512. void
  513. Timeline::draw_playhead ( void )
  514. {
  515. if ( p1 != p2 )
  516. {
  517. draw_cursor( p1, FL_BLUE );
  518. draw_cursor( p2, FL_GREEN );
  519. }
  520. draw_cursor( transport->frame, FL_RED );
  521. }
  522. void
  523. Timeline::redraw_playhead ( void )
  524. {
  525. static nframes_t last_playhead = -1;
  526. if ( last_playhead != transport->frame )
  527. {
  528. redraw_overlay();
  529. last_playhead = transport->frame;
  530. if ( follow_playhead )
  531. {
  532. if ( center_playhead && active() )
  533. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  534. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  535. xposition( ts_to_x( transport->frame ) );
  536. }
  537. }
  538. }
  539. /** called so many times a second to redraw the playhead etc. */
  540. void
  541. Timeline::update_cb ( void *arg )
  542. {
  543. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  544. Timeline *tl = (Timeline *)arg;
  545. tl->redraw_playhead();
  546. }
  547. void
  548. Timeline::draw_overlay ( void )
  549. {
  550. draw_playhead();
  551. if ( ! ( _selection.w && _selection.h ) )
  552. return;
  553. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  554. const Rectangle &r = _selection;
  555. fl_color( FL_BLACK );
  556. fl_line_style( FL_SOLID, 2 );
  557. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  558. fl_color( FL_MAGENTA );
  559. fl_line_style( FL_DASH, 2 );
  560. fl_rect( r.x, r.y, r.w, r.h );
  561. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  562. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  563. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  564. fl_line_style( FL_SOLID, 0 );
  565. /* const unsigned char data[] = { 127, 127, 127, 96, */
  566. /* 127, 96, 127, 40 }; */
  567. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  568. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  569. /* bi2->draw( r.x, r.y ); */
  570. /* delete bi2; */
  571. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  572. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  573. /* bi.color_average( FL_BLACK, 0.50f ); */
  574. /* bi.draw( r.x, r.y ); */
  575. /* delete[] rect_image; */
  576. /* rect_image = NULL; */
  577. fl_pop_clip();
  578. }
  579. // #include "Sequence_Widget.H"
  580. /** select all widgets in inside rectangle /r/ */
  581. void
  582. Timeline::select ( const Rectangle &r )
  583. {
  584. const int Y = r.y;
  585. for ( int i = tracks->children(); i-- ; )
  586. {
  587. Track *t = (Track*)tracks->child( i );
  588. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  589. t->select( r.x, r.y, r.w, r.h, true, true );
  590. }
  591. }
  592. void
  593. Timeline::delete_selected ( void )
  594. {
  595. Sequence_Widget::delete_selected();
  596. }
  597. void
  598. Timeline::select_none ( void )
  599. {
  600. Sequence_Widget::select_none();
  601. }
  602. /** An unfortunate necessity for implementing our own DND aside from
  603. * the (bogus) native FLTK system */
  604. Track *
  605. Timeline::track_under ( int Y )
  606. {
  607. for ( int i = tracks->children(); i-- ; )
  608. {
  609. Track *t = (Track*)tracks->child( i );
  610. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  611. return t;
  612. }
  613. }
  614. int
  615. Timeline::handle ( int m )
  616. {
  617. static Drag *drag = NULL;
  618. switch ( m )
  619. {
  620. case FL_FOCUS:
  621. case FL_UNFOCUS:
  622. // redraw();
  623. return 1;
  624. case FL_KEYBOARD:
  625. case FL_SHORTCUT:
  626. {
  627. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  628. /* we don't want any keys with modifiers... */
  629. return 0;
  630. switch ( Fl::event_key() )
  631. {
  632. case FL_Delete:
  633. case FL_Home:
  634. case FL_End:
  635. /* keep scrollbar from eating these. */
  636. return 0;
  637. case 'p':
  638. {
  639. int X = Fl::event_x() - Track::width();
  640. if ( X > 0 )
  641. {
  642. transport->locate( xoffset + x_to_ts( X ) );
  643. }
  644. return 1;
  645. }
  646. case '[':
  647. {
  648. int X = Fl::event_x() - Track::width();
  649. if ( X > 0 )
  650. {
  651. p1 = xoffset + x_to_ts( X );
  652. }
  653. return 1;
  654. }
  655. case ']':
  656. {
  657. int X = Fl::event_x() - Track::width();
  658. if ( X > 0 )
  659. {
  660. p2 = xoffset + x_to_ts( X );
  661. }
  662. return 1;
  663. }
  664. default:
  665. return Fl_Overlay_Window::handle( m );
  666. }
  667. return 0;
  668. }
  669. default:
  670. {
  671. if ( m == FL_PUSH )
  672. Fl::focus( this );
  673. int r = Fl_Overlay_Window::handle( m );
  674. if ( m != FL_RELEASE && r )
  675. return r;
  676. const int X = Fl::event_x();
  677. const int Y = Fl::event_y();
  678. switch ( m )
  679. {
  680. case FL_PUSH:
  681. {
  682. // take_focus();
  683. if ( Fl::event_button1() )
  684. {
  685. assert( ! drag );
  686. drag = new Drag( X - x(), Y - y() );
  687. _selection.x = drag->x;
  688. _selection.y = drag->y;
  689. }
  690. else if ( Fl::event_button3() )
  691. {
  692. Fl_Menu_Item menu[] =
  693. {
  694. { "Add Track", 0, 0, 0, FL_SUBMENU },
  695. { "Audio", 0, 0, 0 },
  696. { 0 },
  697. { 0 },
  698. };
  699. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  700. if ( r == &menu[1] )
  701. {
  702. /* FIXME: prompt for I/O config? */
  703. Loggable::block_start();
  704. /* add audio track */
  705. char *name = get_unique_track_name( "Audio" );
  706. Track *t = new Track( name );
  707. Sequence *o = new Audio_Sequence( t );
  708. // new Control_Sequence( t );
  709. add_track( t );
  710. t->track( o );
  711. Loggable::block_end();
  712. }
  713. }
  714. else
  715. return 0;
  716. break;
  717. }
  718. case FL_DRAG:
  719. {
  720. int ox = X - drag->x;
  721. int oy = Y - drag->y;
  722. if ( ox < 0 )
  723. _selection.x = X;
  724. if ( oy < 0 )
  725. _selection.y = Y;
  726. _selection.w = abs( ox );
  727. _selection.h = abs( oy );
  728. break;
  729. }
  730. case FL_RELEASE:
  731. {
  732. delete drag;
  733. drag = NULL;
  734. select( _selection );
  735. _selection.w = _selection.h = 0;
  736. break;
  737. }
  738. default:
  739. return 0;
  740. break;
  741. }
  742. redraw_overlay();
  743. return 1;
  744. }
  745. }
  746. }
  747. void
  748. Timeline::zoom_in ( void )
  749. {
  750. hscroll->zoom_in();
  751. }
  752. void
  753. Timeline::zoom_out ( void )
  754. {
  755. hscroll->zoom_out();
  756. }
  757. /** zoom the display to show /secs/ seconds per screen */
  758. void
  759. Timeline::zoom ( float secs )
  760. {
  761. const int sw = w() - vscroll->w() - Track::width();
  762. /* FIXME: we actually need to set this in the scalebar */
  763. // _fpp = (int)((secs * sample_rate()) / sw);
  764. redraw();
  765. }
  766. Track *
  767. Timeline::track_by_name ( const char *name )
  768. {
  769. for ( int i = tracks->children(); i-- ; )
  770. {
  771. Track *t = (Track*)tracks->child( i );
  772. if ( ! strcmp( name, t->name() ) )
  773. return t;
  774. }
  775. return NULL;
  776. }
  777. char *
  778. Timeline::get_unique_track_name ( const char *name )
  779. {
  780. char pat[256];
  781. strcpy( pat, name );
  782. for ( int i = 1; track_by_name( pat ); ++i )
  783. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  784. return strdup( pat );
  785. }
  786. void
  787. Timeline::add_track ( Track *track )
  788. {
  789. printf( "added new track to the timeline\n" );
  790. /* FIXME: do locking */
  791. tracks->add( track );
  792. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  793. redraw();
  794. }
  795. void
  796. Timeline::remove_track ( Track *track )
  797. {
  798. printf( "removed track from the timeline\n" );
  799. /* FIXME: do locking */
  800. /* FIXME: what to do about track contents? */
  801. tracks->remove( track );
  802. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  803. redraw();
  804. }
  805. /** Initiate recording for all armed tracks */
  806. bool
  807. Timeline::record ( void )
  808. {
  809. Loggable::block_start();
  810. for ( int i = tracks->children(); i-- ; )
  811. {
  812. Track *t = (Track*)tracks->child( i );
  813. if ( t->armed() && t->record_ds )
  814. t->record_ds->start( transport->frame );
  815. }
  816. deactivate();
  817. return true;
  818. }
  819. /** stop recording for all armed tracks */
  820. void
  821. Timeline::stop ( void )
  822. {
  823. for ( int i = tracks->children(); i-- ; )
  824. {
  825. Track *t = (Track*)tracks->child( i );
  826. if ( t->armed() && t->record_ds )
  827. t->record_ds->stop( transport->frame );
  828. }
  829. Loggable::block_end();
  830. activate();
  831. }
  832. /**********/
  833. /* Engine */
  834. /**********/
  835. /** call process() on each track header */
  836. nframes_t
  837. Timeline::process ( nframes_t nframes )
  838. {
  839. for ( int i = tracks->children(); i-- ; )
  840. {
  841. Track *t = (Track*)tracks->child( i );
  842. t->process( nframes );
  843. }
  844. /* FIXME: BOGUS */
  845. return nframes;
  846. }
  847. /* THREAD: RT */
  848. void
  849. Timeline::seek ( nframes_t frame )
  850. {
  851. for ( int i = tracks->children(); i-- ; )
  852. {
  853. Track *t = (Track*)tracks->child( i );
  854. t->seek( frame );
  855. }
  856. }
  857. /* THREAD: RT */
  858. int
  859. Timeline::seek_pending ( void )
  860. {
  861. int r = 0;
  862. for ( int i = tracks->children(); i-- ; )
  863. {
  864. Track *t = (Track*)tracks->child( i );
  865. if ( t->playback_ds )
  866. r += t->playback_ds->buffer_percent() < 50;
  867. }
  868. return r;
  869. }
  870. /* FIXME: shouldn't these belong to the engine? */
  871. int
  872. Timeline::total_input_buffer_percent ( void )
  873. {
  874. int r = 0;
  875. int cnt = 0;
  876. for ( int i = tracks->children(); i-- ; )
  877. {
  878. Track *t = (Track*)tracks->child( i );
  879. if ( t->record_ds )
  880. {
  881. ++cnt;
  882. r += t->record_ds->buffer_percent();
  883. }
  884. }
  885. if ( ! cnt )
  886. return 0;
  887. return r / cnt;
  888. }
  889. int
  890. Timeline::total_output_buffer_percent ( void )
  891. {
  892. int r = 0;
  893. int cnt = 0;
  894. for ( int i = tracks->children(); i-- ; )
  895. {
  896. Track *t = (Track*)tracks->child( i );
  897. if ( t->playback_ds )
  898. {
  899. ++cnt;
  900. r += t->playback_ds->buffer_percent();
  901. }
  902. }
  903. if ( ! cnt )
  904. return 0;
  905. return r / cnt;
  906. }
  907. int
  908. Timeline::total_playback_xruns ( void )
  909. {
  910. int r = 0;
  911. for ( int i = tracks->children(); i-- ; )
  912. {
  913. Track *t = (Track*)tracks->child( i );
  914. if ( t->playback_ds )
  915. r += t->playback_ds->xruns();
  916. }
  917. return r;
  918. }
  919. int
  920. Timeline::total_capture_xruns ( void )
  921. {
  922. int r = 0;
  923. for ( int i = tracks->children(); i-- ; )
  924. {
  925. Track *t = (Track*)tracks->child( i );
  926. if ( t->record_ds )
  927. r += t->record_ds->xruns();
  928. }
  929. return r;
  930. }