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.

1143 lines
26KB

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