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.

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