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.

1296 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 = ntpi == tempo_track->_widgets.end() ? NULL : (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. if ( ! visible_r() )
  408. return;
  409. int X, Y, W, H;
  410. int bdx = 0;
  411. int bdw = 0;
  412. X = tracks->x() + bdx + 1;
  413. Y = tracks->y();
  414. W = tracks->w() - bdw - 1;
  415. H = tracks->h();
  416. adjust_vscroll();
  417. /* if ( damage() & FL_DAMAGE_USER1 ) */
  418. /* { */
  419. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  420. /* Rectangle &r = _selection; */
  421. /* make_current(); */
  422. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  423. /* return; */
  424. /* } */
  425. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  426. {
  427. printf( "complete redraw\n" );
  428. draw_box( box(), 0, 0, w(), h(), color() );
  429. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  430. draw_child( *rulers );
  431. fl_pop_clip();
  432. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  433. draw_child( *tracks );
  434. fl_pop_clip();
  435. draw_child( *hscroll );
  436. draw_child( *vscroll );
  437. redraw_overlay();
  438. /* Rectangle &r = _selection; */
  439. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  440. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  441. /* bi.color_average( FL_BLACK, 0.50f ); */
  442. /* bi.draw( r.x, r.y ); */
  443. /* delete[] data; */
  444. /* if ( r.w && r.h ) */
  445. /* { */
  446. /* const unsigned char data[] = { 0, 127, 0, 96, */
  447. /* 0, 96, 0, 127 }; */
  448. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  449. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  450. /* bi2->draw( r.x, r.y ); */
  451. /* delete bi2; */
  452. /* } */
  453. return;
  454. }
  455. if ( damage() & FL_DAMAGE_SCROLL )
  456. {
  457. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  458. int dy = _old_yposition - _yposition;
  459. /* draw_child( *rulers ); */
  460. if ( ! dy )
  461. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  462. Y = rulers->y() + rulers->h();
  463. H = h() - rulers->h() - hscroll->h();
  464. if ( dy == 0 )
  465. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  466. else
  467. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  468. }
  469. _old_xposition = xoffset;
  470. _old_yposition = _yposition;
  471. if ( damage() & FL_DAMAGE_CHILD )
  472. {
  473. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  474. update_child( *rulers );
  475. fl_pop_clip();
  476. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  477. update_child( *tracks );
  478. fl_pop_clip();
  479. update_child( *hscroll );
  480. update_child( *vscroll );
  481. }
  482. }
  483. void
  484. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  485. {
  486. int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  487. if ( x < tracks->x() + Track::width() || x > tracks->x() + tracks->w() )
  488. return;
  489. fl_color( color );
  490. int y = rulers->y() + rulers->h();
  491. int h = this->h() - hscroll->h();
  492. fl_push_clip( Track::width(), y, tracks->w(), h );
  493. fl_line( x, y, x, h );
  494. fl_color( fl_darker( color ) );
  495. fl_line( x - 1, y, x - 1, h );
  496. fl_color( FL_BLACK );
  497. fl_line( x + 1, y, x + 1, h );
  498. /* draw cap */
  499. fl_color( color );
  500. fl_begin_polygon();
  501. fl_vertex( x - 8, y );
  502. fl_vertex( x, y + 8 );
  503. fl_vertex( x + 8, y );
  504. fl_end_polygon();
  505. /* draw cap outline */
  506. fl_color( FL_BLACK );
  507. fl_begin_line();
  508. fl_vertex( x - 8, y );
  509. fl_vertex( x, y + 8 );
  510. fl_vertex( x + 8, y );
  511. fl_end_line();
  512. fl_pop_clip();
  513. }
  514. void
  515. Timeline::draw_playhead ( void )
  516. {
  517. if ( p1 != p2 )
  518. {
  519. draw_cursor( p1, FL_BLUE );
  520. draw_cursor( p2, FL_GREEN );
  521. }
  522. draw_cursor( transport->frame, FL_RED );
  523. }
  524. void
  525. Timeline::redraw_playhead ( void )
  526. {
  527. static nframes_t last_playhead = -1;
  528. if ( last_playhead != transport->frame )
  529. {
  530. redraw_overlay();
  531. last_playhead = transport->frame;
  532. if ( follow_playhead )
  533. {
  534. if ( center_playhead && active() )
  535. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  536. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  537. xposition( ts_to_x( transport->frame ) );
  538. }
  539. }
  540. }
  541. /** called so many times a second to redraw the playhead etc. */
  542. void
  543. Timeline::update_cb ( void *arg )
  544. {
  545. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  546. Timeline *tl = (Timeline *)arg;
  547. tl->redraw_playhead();
  548. }
  549. void
  550. Timeline::draw_overlay ( void )
  551. {
  552. draw_playhead();
  553. if ( ! ( _selection.w && _selection.h ) )
  554. return;
  555. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  556. const Rectangle &r = _selection;
  557. fl_color( FL_BLACK );
  558. fl_line_style( FL_SOLID, 2 );
  559. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  560. fl_color( FL_MAGENTA );
  561. fl_line_style( FL_DASH, 2 );
  562. fl_rect( r.x, r.y, r.w, r.h );
  563. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  564. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  565. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  566. fl_line_style( FL_SOLID, 0 );
  567. /* const unsigned char data[] = { 127, 127, 127, 96, */
  568. /* 127, 96, 127, 40 }; */
  569. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  570. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  571. /* bi2->draw( r.x, r.y ); */
  572. /* delete bi2; */
  573. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  574. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  575. /* bi.color_average( FL_BLACK, 0.50f ); */
  576. /* bi.draw( r.x, r.y ); */
  577. /* delete[] rect_image; */
  578. /* rect_image = NULL; */
  579. fl_pop_clip();
  580. }
  581. // #include "Sequence_Widget.H"
  582. /** select all widgets in inside rectangle /r/ */
  583. void
  584. Timeline::select ( const Rectangle &r )
  585. {
  586. const int Y = r.y;
  587. for ( int i = tracks->children(); i-- ; )
  588. {
  589. Track *t = (Track*)tracks->child( i );
  590. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  591. t->select( r.x, r.y, r.w, r.h, true, true );
  592. }
  593. }
  594. void
  595. Timeline::delete_selected ( void )
  596. {
  597. Sequence_Widget::delete_selected();
  598. }
  599. void
  600. Timeline::select_none ( void )
  601. {
  602. Sequence_Widget::select_none();
  603. }
  604. /** An unfortunate necessity for implementing our own DND aside from
  605. * the (bogus) native FLTK system */
  606. Track *
  607. Timeline::track_under ( int Y )
  608. {
  609. for ( int i = tracks->children(); i-- ; )
  610. {
  611. Track *t = (Track*)tracks->child( i );
  612. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  613. return t;
  614. }
  615. }
  616. int
  617. Timeline::handle ( int m )
  618. {
  619. static Drag *drag = NULL;
  620. switch ( m )
  621. {
  622. case FL_FOCUS:
  623. case FL_UNFOCUS:
  624. // redraw();
  625. return 1;
  626. case FL_KEYBOARD:
  627. case FL_SHORTCUT:
  628. {
  629. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  630. /* we don't want any keys with modifiers... */
  631. return 0;
  632. switch ( Fl::event_key() )
  633. {
  634. case FL_Delete:
  635. case FL_Home:
  636. case FL_End:
  637. /* keep scrollbar from eating these. */
  638. return 0;
  639. case 'p':
  640. {
  641. int X = Fl::event_x() - Track::width();
  642. if ( X > 0 )
  643. {
  644. transport->locate( xoffset + x_to_ts( X ) );
  645. }
  646. return 1;
  647. }
  648. case '[':
  649. {
  650. int X = Fl::event_x() - Track::width();
  651. if ( X > 0 )
  652. {
  653. p1 = xoffset + x_to_ts( X );
  654. }
  655. return 1;
  656. }
  657. case ']':
  658. {
  659. int X = Fl::event_x() - Track::width();
  660. if ( X > 0 )
  661. {
  662. p2 = xoffset + x_to_ts( X );
  663. }
  664. return 1;
  665. }
  666. default:
  667. return Fl_Overlay_Window::handle( m );
  668. }
  669. return 0;
  670. }
  671. default:
  672. {
  673. if ( m == FL_PUSH )
  674. Fl::focus( this );
  675. int r = Fl_Overlay_Window::handle( m );
  676. if ( m != FL_RELEASE && r )
  677. return r;
  678. const int X = Fl::event_x();
  679. const int Y = Fl::event_y();
  680. switch ( m )
  681. {
  682. case FL_PUSH:
  683. {
  684. // take_focus();
  685. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  686. return 0;
  687. if ( Fl::event_button1() )
  688. {
  689. assert( ! drag );
  690. drag = new Drag( X - x(), Y - y() );
  691. _selection.x = drag->x;
  692. _selection.y = drag->y;
  693. }
  694. else if ( Fl::event_button3() )
  695. {
  696. Fl_Menu_Item menu[] =
  697. {
  698. { "Add Track", 0, 0, 0, FL_SUBMENU },
  699. { "Audio", 0, 0, 0 },
  700. { 0 },
  701. { 0 },
  702. };
  703. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  704. if ( r == &menu[1] )
  705. {
  706. /* FIXME: prompt for I/O config? */
  707. Loggable::block_start();
  708. /* add audio track */
  709. char *name = get_unique_track_name( "Audio" );
  710. Track *t = new Track( name );
  711. Sequence *o = new Audio_Sequence( t );
  712. // new Control_Sequence( t );
  713. add_track( t );
  714. t->track( o );
  715. Loggable::block_end();
  716. }
  717. }
  718. else
  719. return 0;
  720. break;
  721. }
  722. case FL_DRAG:
  723. {
  724. int ox = X - drag->x;
  725. int oy = Y - drag->y;
  726. if ( ox < 0 )
  727. _selection.x = X;
  728. if ( oy < 0 )
  729. _selection.y = Y;
  730. _selection.w = abs( ox );
  731. _selection.h = abs( oy );
  732. break;
  733. }
  734. case FL_RELEASE:
  735. {
  736. delete drag;
  737. drag = NULL;
  738. select( _selection );
  739. _selection.w = _selection.h = 0;
  740. break;
  741. }
  742. default:
  743. return 0;
  744. break;
  745. }
  746. redraw_overlay();
  747. return 1;
  748. }
  749. }
  750. }
  751. void
  752. Timeline::zoom_in ( void )
  753. {
  754. hscroll->zoom_in();
  755. }
  756. void
  757. Timeline::zoom_out ( void )
  758. {
  759. hscroll->zoom_out();
  760. }
  761. /** zoom the display to show /secs/ seconds per screen */
  762. void
  763. Timeline::zoom ( float secs )
  764. {
  765. const int sw = tracks->w() - Track::width();
  766. int fpp = (int)((secs * sample_rate()) / sw);
  767. int p = 0;
  768. while ( 1 << p < fpp ) p++;
  769. hscroll->zoom( p );
  770. redraw();
  771. }
  772. Track *
  773. Timeline::track_by_name ( const char *name )
  774. {
  775. for ( int i = tracks->children(); i-- ; )
  776. {
  777. Track *t = (Track*)tracks->child( i );
  778. if ( ! strcmp( name, t->name() ) )
  779. return t;
  780. }
  781. return NULL;
  782. }
  783. char *
  784. Timeline::get_unique_track_name ( const char *name )
  785. {
  786. char pat[256];
  787. strcpy( pat, name );
  788. for ( int i = 1; track_by_name( pat ); ++i )
  789. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  790. return strdup( pat );
  791. }
  792. void
  793. Timeline::add_track ( Track *track )
  794. {
  795. printf( "added new track to the timeline\n" );
  796. /* FIXME: do locking */
  797. tracks->add( track );
  798. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  799. redraw();
  800. }
  801. void
  802. Timeline::remove_track ( Track *track )
  803. {
  804. printf( "removed track from the timeline\n" );
  805. /* FIXME: do locking */
  806. /* FIXME: what to do about track contents? */
  807. tracks->remove( track );
  808. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  809. redraw();
  810. }
  811. /** Initiate recording for all armed tracks */
  812. bool
  813. Timeline::record ( void )
  814. {
  815. Loggable::block_start();
  816. for ( int i = tracks->children(); i-- ; )
  817. {
  818. Track *t = (Track*)tracks->child( i );
  819. if ( t->armed() && t->record_ds )
  820. t->record_ds->start( transport->frame );
  821. }
  822. deactivate();
  823. return true;
  824. }
  825. /** stop recording for all armed tracks */
  826. void
  827. Timeline::stop ( void )
  828. {
  829. for ( int i = tracks->children(); i-- ; )
  830. {
  831. Track *t = (Track*)tracks->child( i );
  832. if ( t->armed() && t->record_ds )
  833. t->record_ds->stop( transport->frame );
  834. }
  835. Loggable::block_end();
  836. activate();
  837. }
  838. /**********/
  839. /* Engine */
  840. /**********/
  841. /** call process() on each track header */
  842. nframes_t
  843. Timeline::process ( nframes_t nframes )
  844. {
  845. for ( int i = tracks->children(); i-- ; )
  846. {
  847. Track *t = (Track*)tracks->child( i );
  848. t->process( nframes );
  849. }
  850. /* FIXME: BOGUS */
  851. return nframes;
  852. }
  853. /* THREAD: RT */
  854. void
  855. Timeline::seek ( nframes_t frame )
  856. {
  857. for ( int i = tracks->children(); i-- ; )
  858. {
  859. Track *t = (Track*)tracks->child( i );
  860. t->seek( frame );
  861. }
  862. }
  863. /* THREAD: RT */
  864. int
  865. Timeline::seek_pending ( void )
  866. {
  867. int r = 0;
  868. for ( int i = tracks->children(); i-- ; )
  869. {
  870. Track *t = (Track*)tracks->child( i );
  871. if ( t->playback_ds )
  872. r += t->playback_ds->buffer_percent() < 50;
  873. }
  874. return r;
  875. }
  876. /* FIXME: shouldn't these belong to the engine? */
  877. int
  878. Timeline::total_input_buffer_percent ( void )
  879. {
  880. int r = 0;
  881. int cnt = 0;
  882. for ( int i = tracks->children(); i-- ; )
  883. {
  884. Track *t = (Track*)tracks->child( i );
  885. if ( t->record_ds )
  886. {
  887. ++cnt;
  888. r += t->record_ds->buffer_percent();
  889. }
  890. }
  891. if ( ! cnt )
  892. return 0;
  893. return r / cnt;
  894. }
  895. int
  896. Timeline::total_output_buffer_percent ( void )
  897. {
  898. int r = 0;
  899. int cnt = 0;
  900. for ( int i = tracks->children(); i-- ; )
  901. {
  902. Track *t = (Track*)tracks->child( i );
  903. if ( t->playback_ds )
  904. {
  905. ++cnt;
  906. r += t->playback_ds->buffer_percent();
  907. }
  908. }
  909. if ( ! cnt )
  910. return 0;
  911. return r / cnt;
  912. }
  913. int
  914. Timeline::total_playback_xruns ( void )
  915. {
  916. int r = 0;
  917. for ( int i = tracks->children(); i-- ; )
  918. {
  919. Track *t = (Track*)tracks->child( i );
  920. if ( t->playback_ds )
  921. r += t->playback_ds->xruns();
  922. }
  923. return r;
  924. }
  925. int
  926. Timeline::total_capture_xruns ( void )
  927. {
  928. int r = 0;
  929. for ( int i = tracks->children(); i-- ; )
  930. {
  931. Track *t = (Track*)tracks->child( i );
  932. if ( t->record_ds )
  933. r += t->record_ds->xruns();
  934. }
  935. return r;
  936. }