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.

1150 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->select( r.x, r.y, r.w, r.h, true, true );
  528. }
  529. }
  530. void
  531. Timeline::delete_selected ( void )
  532. {
  533. Sequence_Widget::delete_selected();
  534. }
  535. void
  536. Timeline::select_none ( void )
  537. {
  538. Sequence_Widget::select_none();
  539. }
  540. int
  541. Timeline::handle ( int m )
  542. {
  543. static Drag *drag = NULL;
  544. switch ( m )
  545. {
  546. case FL_FOCUS:
  547. case FL_UNFOCUS:
  548. // redraw();
  549. return 1;
  550. case FL_KEYBOARD:
  551. case FL_SHORTCUT:
  552. {
  553. if ( Fl::event_state() & ( FL_ALT || FL_CTRL || FL_SHIFT ) )
  554. /* we don't want any keys with modifiers... */
  555. return 0;
  556. switch ( Fl::event_key() )
  557. {
  558. case FL_Delete:
  559. case FL_Home:
  560. case FL_End:
  561. /* keep scrollbar from eating these. */
  562. return 0;
  563. case 'p':
  564. {
  565. int X = Fl::event_x() - Track::width();
  566. if ( X > 0 )
  567. {
  568. transport->locate( xoffset + x_to_ts( X ) );
  569. }
  570. return 1;
  571. }
  572. case '[':
  573. {
  574. int X = Fl::event_x() - Track::width();
  575. if ( X > 0 )
  576. {
  577. p1 = xoffset + x_to_ts( X );
  578. }
  579. return 1;
  580. }
  581. case ']':
  582. {
  583. int X = Fl::event_x() - Track::width();
  584. if ( X > 0 )
  585. {
  586. p2 = xoffset + x_to_ts( X );
  587. }
  588. return 1;
  589. }
  590. default:
  591. return Fl_Overlay_Window::handle( m );
  592. }
  593. return 0;
  594. }
  595. default:
  596. {
  597. if ( m == FL_PUSH )
  598. Fl::focus( this );
  599. int r = Fl_Overlay_Window::handle( m );
  600. if ( m != FL_RELEASE && r )
  601. return r;
  602. const int X = Fl::event_x();
  603. const int Y = Fl::event_y();
  604. switch ( m )
  605. {
  606. case FL_PUSH:
  607. {
  608. // take_focus();
  609. if ( Fl::event_button1() )
  610. {
  611. assert( ! drag );
  612. drag = new Drag( X - x(), Y - y() );
  613. _selection.x = drag->x;
  614. _selection.y = drag->y;
  615. }
  616. else if ( Fl::event_button3() )
  617. {
  618. Fl_Menu_Item menu[] =
  619. {
  620. { "Add Track", 0, 0, 0, FL_SUBMENU },
  621. { "Audio", 0, 0, 0 },
  622. { 0 },
  623. { 0 },
  624. };
  625. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  626. if ( r == &menu[1] )
  627. {
  628. /* FIXME: prompt for I/O config? */
  629. Loggable::block_start();
  630. /* add audio track */
  631. char *name = get_unique_track_name( "Audio" );
  632. Track *t = new Track( name );
  633. Sequence *o = new Audio_Sequence( t );
  634. new Control_Sequence( t );
  635. add_track( t );
  636. t->track( o );
  637. Loggable::block_end();
  638. }
  639. }
  640. else
  641. return 0;
  642. break;
  643. }
  644. case FL_DRAG:
  645. {
  646. int ox = X - drag->x;
  647. int oy = Y - drag->y;
  648. if ( ox < 0 )
  649. _selection.x = X;
  650. if ( oy < 0 )
  651. _selection.y = Y;
  652. _selection.w = abs( ox );
  653. _selection.h = abs( oy );
  654. break;
  655. }
  656. case FL_RELEASE:
  657. {
  658. delete drag;
  659. drag = NULL;
  660. select( _selection );
  661. _selection.w = _selection.h = 0;
  662. break;
  663. }
  664. default:
  665. return 0;
  666. break;
  667. }
  668. redraw_overlay();
  669. return 1;
  670. }
  671. }
  672. }
  673. void
  674. Timeline::zoom_in ( void )
  675. {
  676. hscroll->zoom_in();
  677. }
  678. void
  679. Timeline::zoom_out ( void )
  680. {
  681. hscroll->zoom_out();
  682. }
  683. /** zoom the display to show /secs/ seconds per screen */
  684. void
  685. Timeline::zoom ( float secs )
  686. {
  687. const int sw = w() - vscroll->w() - Track::width();
  688. /* FIXME: we actually need to set this in the scalebar */
  689. // _fpp = (int)((secs * sample_rate()) / sw);
  690. redraw();
  691. }
  692. Track *
  693. Timeline::track_by_name ( const char *name )
  694. {
  695. for ( int i = tracks->children(); i-- ; )
  696. {
  697. Track *t = (Track*)tracks->child( i );
  698. if ( ! strcmp( name, t->name() ) )
  699. return t;
  700. }
  701. return NULL;
  702. }
  703. char *
  704. Timeline::get_unique_track_name ( const char *name )
  705. {
  706. char pat[256];
  707. strcpy( pat, name );
  708. for ( int i = 1; track_by_name( pat ); ++i )
  709. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  710. return strdup( pat );
  711. }
  712. void
  713. Timeline::add_track ( Track *track )
  714. {
  715. printf( "added new track to the timeline\n" );
  716. /* FIXME: do locking */
  717. tracks->add( track );
  718. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  719. redraw();
  720. }
  721. void
  722. Timeline::remove_track ( Track *track )
  723. {
  724. printf( "removed track from the timeline\n" );
  725. /* FIXME: do locking */
  726. /* FIXME: what to do about track contents? */
  727. tracks->remove( track );
  728. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  729. redraw();
  730. }
  731. /** Initiate recording for all armed tracks */
  732. bool
  733. Timeline::record ( void )
  734. {
  735. Loggable::block_start();
  736. for ( int i = tracks->children(); i-- ; )
  737. {
  738. Track *t = (Track*)tracks->child( i );
  739. if ( t->armed() && t->record_ds )
  740. t->record_ds->start( transport->frame );
  741. }
  742. deactivate();
  743. return true;
  744. }
  745. /** stop recording for all armed tracks */
  746. void
  747. Timeline::stop ( void )
  748. {
  749. for ( int i = tracks->children(); i-- ; )
  750. {
  751. Track *t = (Track*)tracks->child( i );
  752. if ( t->armed() && t->record_ds )
  753. t->record_ds->stop( transport->frame );
  754. }
  755. Loggable::block_end();
  756. activate();
  757. }
  758. /**********/
  759. /* Engine */
  760. /**********/
  761. /** call process() on each track header */
  762. nframes_t
  763. Timeline::process ( nframes_t nframes )
  764. {
  765. for ( int i = tracks->children(); i-- ; )
  766. {
  767. Track *t = (Track*)tracks->child( i );
  768. t->process( nframes );
  769. }
  770. /* FIXME: BOGUS */
  771. return nframes;
  772. }
  773. /* THREAD: RT */
  774. void
  775. Timeline::seek ( nframes_t frame )
  776. {
  777. for ( int i = tracks->children(); i-- ; )
  778. {
  779. Track *t = (Track*)tracks->child( i );
  780. t->seek( frame );
  781. }
  782. }
  783. /* THREAD: RT */
  784. int
  785. Timeline::seek_pending ( void )
  786. {
  787. int r = 0;
  788. for ( int i = tracks->children(); i-- ; )
  789. {
  790. Track *t = (Track*)tracks->child( i );
  791. if ( t->playback_ds )
  792. r += t->playback_ds->buffer_percent() < 50;
  793. }
  794. return r;
  795. }
  796. /* FIXME: shouldn't these belong to the engine? */
  797. int
  798. Timeline::total_input_buffer_percent ( void )
  799. {
  800. int r = 0;
  801. int cnt = 0;
  802. for ( int i = tracks->children(); i-- ; )
  803. {
  804. Track *t = (Track*)tracks->child( i );
  805. if ( t->record_ds )
  806. {
  807. ++cnt;
  808. r += t->record_ds->buffer_percent();
  809. }
  810. }
  811. if ( ! cnt )
  812. return 0;
  813. return r / cnt;
  814. }
  815. int
  816. Timeline::total_output_buffer_percent ( void )
  817. {
  818. int r = 0;
  819. int cnt = 0;
  820. for ( int i = tracks->children(); i-- ; )
  821. {
  822. Track *t = (Track*)tracks->child( i );
  823. if ( t->playback_ds )
  824. {
  825. ++cnt;
  826. r += t->playback_ds->buffer_percent();
  827. }
  828. }
  829. if ( ! cnt )
  830. return 0;
  831. return r / cnt;
  832. }