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.

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