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.

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