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.

1127 lines
25KB

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