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.

1238 lines
28KB

  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. int
  603. Timeline::handle ( int m )
  604. {
  605. static Drag *drag = NULL;
  606. switch ( m )
  607. {
  608. case FL_FOCUS:
  609. case FL_UNFOCUS:
  610. // redraw();
  611. return 1;
  612. case FL_KEYBOARD:
  613. case FL_SHORTCUT:
  614. {
  615. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  616. /* we don't want any keys with modifiers... */
  617. return 0;
  618. switch ( Fl::event_key() )
  619. {
  620. case FL_Delete:
  621. case FL_Home:
  622. case FL_End:
  623. /* keep scrollbar from eating these. */
  624. return 0;
  625. case 'p':
  626. {
  627. int X = Fl::event_x() - Track::width();
  628. if ( X > 0 )
  629. {
  630. transport->locate( xoffset + x_to_ts( X ) );
  631. }
  632. return 1;
  633. }
  634. case '[':
  635. {
  636. int X = Fl::event_x() - Track::width();
  637. if ( X > 0 )
  638. {
  639. p1 = xoffset + x_to_ts( X );
  640. }
  641. return 1;
  642. }
  643. case ']':
  644. {
  645. int X = Fl::event_x() - Track::width();
  646. if ( X > 0 )
  647. {
  648. p2 = xoffset + x_to_ts( X );
  649. }
  650. return 1;
  651. }
  652. default:
  653. return Fl_Overlay_Window::handle( m );
  654. }
  655. return 0;
  656. }
  657. default:
  658. {
  659. if ( m == FL_PUSH )
  660. Fl::focus( this );
  661. int r = Fl_Overlay_Window::handle( m );
  662. if ( m != FL_RELEASE && r )
  663. return r;
  664. const int X = Fl::event_x();
  665. const int Y = Fl::event_y();
  666. switch ( m )
  667. {
  668. case FL_PUSH:
  669. {
  670. // take_focus();
  671. if ( Fl::event_button1() )
  672. {
  673. assert( ! drag );
  674. drag = new Drag( X - x(), Y - y() );
  675. _selection.x = drag->x;
  676. _selection.y = drag->y;
  677. }
  678. else if ( Fl::event_button3() )
  679. {
  680. Fl_Menu_Item menu[] =
  681. {
  682. { "Add Track", 0, 0, 0, FL_SUBMENU },
  683. { "Audio", 0, 0, 0 },
  684. { 0 },
  685. { 0 },
  686. };
  687. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  688. if ( r == &menu[1] )
  689. {
  690. /* FIXME: prompt for I/O config? */
  691. Loggable::block_start();
  692. /* add audio track */
  693. char *name = get_unique_track_name( "Audio" );
  694. Track *t = new Track( name );
  695. Sequence *o = new Audio_Sequence( t );
  696. // new Control_Sequence( t );
  697. add_track( t );
  698. t->track( o );
  699. Loggable::block_end();
  700. }
  701. }
  702. else
  703. return 0;
  704. break;
  705. }
  706. case FL_DRAG:
  707. {
  708. int ox = X - drag->x;
  709. int oy = Y - drag->y;
  710. if ( ox < 0 )
  711. _selection.x = X;
  712. if ( oy < 0 )
  713. _selection.y = Y;
  714. _selection.w = abs( ox );
  715. _selection.h = abs( oy );
  716. break;
  717. }
  718. case FL_RELEASE:
  719. {
  720. delete drag;
  721. drag = NULL;
  722. select( _selection );
  723. _selection.w = _selection.h = 0;
  724. break;
  725. }
  726. default:
  727. return 0;
  728. break;
  729. }
  730. redraw_overlay();
  731. return 1;
  732. }
  733. }
  734. }
  735. void
  736. Timeline::zoom_in ( void )
  737. {
  738. hscroll->zoom_in();
  739. }
  740. void
  741. Timeline::zoom_out ( void )
  742. {
  743. hscroll->zoom_out();
  744. }
  745. /** zoom the display to show /secs/ seconds per screen */
  746. void
  747. Timeline::zoom ( float secs )
  748. {
  749. const int sw = w() - vscroll->w() - Track::width();
  750. /* FIXME: we actually need to set this in the scalebar */
  751. // _fpp = (int)((secs * sample_rate()) / sw);
  752. redraw();
  753. }
  754. Track *
  755. Timeline::track_by_name ( const char *name )
  756. {
  757. for ( int i = tracks->children(); i-- ; )
  758. {
  759. Track *t = (Track*)tracks->child( i );
  760. if ( ! strcmp( name, t->name() ) )
  761. return t;
  762. }
  763. return NULL;
  764. }
  765. char *
  766. Timeline::get_unique_track_name ( const char *name )
  767. {
  768. char pat[256];
  769. strcpy( pat, name );
  770. for ( int i = 1; track_by_name( pat ); ++i )
  771. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  772. return strdup( pat );
  773. }
  774. void
  775. Timeline::add_track ( Track *track )
  776. {
  777. printf( "added new track to the timeline\n" );
  778. /* FIXME: do locking */
  779. tracks->add( track );
  780. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  781. redraw();
  782. }
  783. void
  784. Timeline::remove_track ( Track *track )
  785. {
  786. printf( "removed track from the timeline\n" );
  787. /* FIXME: do locking */
  788. /* FIXME: what to do about track contents? */
  789. tracks->remove( track );
  790. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  791. redraw();
  792. }
  793. /** Initiate recording for all armed tracks */
  794. bool
  795. Timeline::record ( void )
  796. {
  797. Loggable::block_start();
  798. for ( int i = tracks->children(); i-- ; )
  799. {
  800. Track *t = (Track*)tracks->child( i );
  801. if ( t->armed() && t->record_ds )
  802. t->record_ds->start( transport->frame );
  803. }
  804. deactivate();
  805. return true;
  806. }
  807. /** stop recording for all armed tracks */
  808. void
  809. Timeline::stop ( void )
  810. {
  811. for ( int i = tracks->children(); i-- ; )
  812. {
  813. Track *t = (Track*)tracks->child( i );
  814. if ( t->armed() && t->record_ds )
  815. t->record_ds->stop( transport->frame );
  816. }
  817. Loggable::block_end();
  818. activate();
  819. }
  820. /**********/
  821. /* Engine */
  822. /**********/
  823. /** call process() on each track header */
  824. nframes_t
  825. Timeline::process ( nframes_t nframes )
  826. {
  827. for ( int i = tracks->children(); i-- ; )
  828. {
  829. Track *t = (Track*)tracks->child( i );
  830. t->process( nframes );
  831. }
  832. /* FIXME: BOGUS */
  833. return nframes;
  834. }
  835. /* THREAD: RT */
  836. void
  837. Timeline::seek ( nframes_t frame )
  838. {
  839. for ( int i = tracks->children(); i-- ; )
  840. {
  841. Track *t = (Track*)tracks->child( i );
  842. t->seek( frame );
  843. }
  844. }
  845. /* THREAD: RT */
  846. int
  847. Timeline::seek_pending ( void )
  848. {
  849. int r = 0;
  850. for ( int i = tracks->children(); i-- ; )
  851. {
  852. Track *t = (Track*)tracks->child( i );
  853. if ( t->playback_ds )
  854. r += t->playback_ds->buffer_percent() < 50;
  855. }
  856. return r;
  857. }
  858. /* FIXME: shouldn't these belong to the engine? */
  859. int
  860. Timeline::total_input_buffer_percent ( void )
  861. {
  862. int r = 0;
  863. int cnt = 0;
  864. for ( int i = tracks->children(); i-- ; )
  865. {
  866. Track *t = (Track*)tracks->child( i );
  867. if ( t->record_ds )
  868. {
  869. ++cnt;
  870. r += t->record_ds->buffer_percent();
  871. }
  872. }
  873. if ( ! cnt )
  874. return 0;
  875. return r / cnt;
  876. }
  877. int
  878. Timeline::total_output_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->playback_ds )
  886. {
  887. ++cnt;
  888. r += t->playback_ds->buffer_percent();
  889. }
  890. }
  891. if ( ! cnt )
  892. return 0;
  893. return r / cnt;
  894. }