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.

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