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.

1341 lines
30KB

  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 <FL/Fl_Scroll.H>
  19. #include <FL/Fl_Pack.H>
  20. #include <FL/Fl_Scrollbar.H>
  21. #include <FL/Fl_Widget.H>
  22. #include <FL/fl_draw.H>
  23. #include <FL/Fl_Scrollbar.H>
  24. #include "Timeline.H"
  25. #include "Tempo_Sequence.H"
  26. #include "Time_Sequence.H"
  27. #include "Audio_Sequence.H"
  28. #include "Control_Sequence.H"
  29. #include "Scalebar.H"
  30. #include "Sequence.H"
  31. #include "Annotation_Sequence.H"
  32. #include "Track.H"
  33. #include "Playback_DS.H"
  34. #include "Record_DS.H"
  35. #include "Transport.H"
  36. bool Timeline::draw_with_measure_lines = true;
  37. Timeline::snap_e Timeline::snap_to = Bars;
  38. bool Timeline::snap_magnetic = true;
  39. bool Timeline::follow_playhead = true;
  40. bool Timeline::center_playhead = true;
  41. const float UPDATE_FREQ = 0.02f;
  42. /** return the combined height of all visible children of (veritcal)
  43. pack, /p/. This is necessary because pack sizes are adjusted only
  44. when the relevant areas are exposes. */
  45. static int
  46. pack_visible_height ( const Fl_Pack *p )
  47. {
  48. int th = 0;
  49. const Fl_Widget* const *w = p->array();
  50. for ( int i = p->children(); i--; ++w )
  51. if ( (*w)->visible() )
  52. th += (*w)->h() + p->spacing();
  53. return th;
  54. }
  55. #define BP fl_begin_polygon()
  56. #define EP fl_end_polygon()
  57. #define vv(x,y) fl_vertex( x, y )
  58. #define BL fl_begin_line()
  59. #define EL fl_end_line()
  60. static void
  61. draw_full_arrow_symbol ( Fl_Color color )
  62. {
  63. /* draw cap */
  64. fl_color( color );
  65. BP;
  66. vv( -1, -1 );
  67. vv( 0, 1 );
  68. vv( 1, -1 );
  69. EP;
  70. /* draw cap outline */
  71. fl_color( FL_BLACK );
  72. BL;
  73. vv( -1, -1 );
  74. vv( 0, 1 );
  75. vv( 1, -1 );
  76. EL;
  77. }
  78. /** recalculate the size of vertical scrolling area and inform scrollbar */
  79. void
  80. Timeline::adjust_vscroll ( void )
  81. {
  82. vscroll->value( _yposition, h() - rulers->h() - hscroll->h(), 0, pack_visible_height( tracks ) );
  83. }
  84. void
  85. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  86. {
  87. ((Timeline*)v)->cb_scroll( w );
  88. }
  89. void
  90. Timeline::cb_scroll ( Fl_Widget *w )
  91. {
  92. if ( w == vscroll )
  93. {
  94. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  95. yposition( vscroll->value() );
  96. adjust_vscroll();
  97. }
  98. else
  99. {
  100. if ( hscroll->zoom_changed() )
  101. {
  102. nframes_t under_mouse = x_to_offset( Fl::event_x() );
  103. _fpp = hscroll->zoom();
  104. const int tw = tracks->w() - Track::width();
  105. // hscroll->value( ts_to_x( xoffset ), tw, 0, ts_to_x( _length ) );
  106. hscroll->value( max( 0, ts_to_x( under_mouse ) - ( Fl::event_x() - tracks->x() - Track::width() ) ),
  107. tw, 0, ts_to_x( _length ) );
  108. redraw();
  109. }
  110. xposition( hscroll->value() );
  111. }
  112. }
  113. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  114. {
  115. box( FL_FLAT_BOX );
  116. xoffset = 0;
  117. _yposition = 0;
  118. _old_yposition = 0;
  119. _old_xposition = 0;
  120. X = Y = 0;
  121. p1 = p2 = 0;
  122. {
  123. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  124. o->range( 0, 48000 * 300 );
  125. // o->zoom_range( 1, 16384 );
  126. // o->zoom_range( 1, 65536 << 4 );
  127. o->zoom_range( 1, 20 );
  128. o->zoom( 8 );
  129. o->type( FL_HORIZONTAL );
  130. o->callback( cb_scroll, this );
  131. hscroll = o;
  132. }
  133. {
  134. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  135. o->type( FL_VERTICAL );
  136. o->callback( cb_scroll, this );
  137. vscroll = o;
  138. }
  139. {
  140. Fl_Pack *o = new Fl_Pack( X + Track::width(), Y, (W - Track::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  141. o->type( Fl_Pack::VERTICAL );
  142. {
  143. Tempo_Sequence *o = new Tempo_Sequence( 0, 0, 800, 24 );
  144. o->color( fl_gray_ramp( 18 ) );
  145. o->label( "Tempo" );
  146. o->align( FL_ALIGN_LEFT );
  147. tempo_track = o;
  148. }
  149. {
  150. Time_Sequence *o = new Time_Sequence( 0, 24, 800, 24 );
  151. o->color( fl_gray_ramp( 16 ) );
  152. o->label( "Time" );
  153. o->align( FL_ALIGN_LEFT );
  154. time_track = o;
  155. }
  156. /* { */
  157. /* Annotation_Sequence *o = new Annotation_Sequence( 0, 24, 800, 24 ); */
  158. /* o->color( fl_gray_ramp( 'F' ) ); */
  159. /* o->label( "Ruler" ); */
  160. /* o->align( FL_ALIGN_LEFT ); */
  161. /* ruler_track = o; */
  162. /* } */
  163. o->size( o->w(), o->child( 0 )->h() * o->children() );
  164. rulers = o;
  165. o->end();
  166. }
  167. {
  168. // sample_rate() = engine->sample_rate();
  169. _fpp = 8;
  170. // _length = sample_rate() * 60 * 2;
  171. /* FIXME: hack */
  172. _length = -1;
  173. {
  174. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 1 );
  175. o->type( Fl_Pack::VERTICAL );
  176. o->spacing( 1 );
  177. tracks = o;
  178. o->end();
  179. resizable( o );
  180. }
  181. }
  182. /* rulers go above tracks... */
  183. add( rulers );
  184. /* make sure scrollbars are on top */
  185. add( vscroll );
  186. add( hscroll );
  187. vscroll->range( 0, tracks->h() );
  188. redraw();
  189. end();
  190. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  191. }
  192. /* float */
  193. /* Timeline::beats_per_minute ( nframes_t when ) const */
  194. /* { */
  195. /* /\* return tempo_track->beats_per_minute( when ); *\/ */
  196. /* } */
  197. /* int */
  198. /* Timeline::beats_per_bar ( nframes_t when ) const */
  199. /* { */
  200. /* time_sig t = time_track->time( when ); */
  201. /* return t.beats_per_bar; */
  202. /* } */
  203. void
  204. Timeline::beats_per_minute ( nframes_t when, float bpm )
  205. {
  206. tempo_track->add( new Tempo_Point( when, bpm ) );
  207. }
  208. void
  209. Timeline::time ( nframes_t when, int bpb, int note_type )
  210. {
  211. time_track->add( new Time_Point( when, bpb, note_type ) );
  212. }
  213. /************/
  214. /* Snapping */
  215. /************/
  216. struct nearest_line_arg
  217. {
  218. nframes_t original;
  219. nframes_t closest;
  220. };
  221. const int snap_pixel = 10;
  222. static nframes_t
  223. abs_diff ( nframes_t n1, nframes_t n2 )
  224. {
  225. return n1 > n2 ? n1 - n2 : n2 - n1;
  226. }
  227. void
  228. nearest_line_cb ( nframes_t frame, const BBT &bbt, void *arg )
  229. {
  230. nearest_line_arg *n = (nearest_line_arg *)arg;
  231. if ( Timeline::snap_to == Timeline::Bars && bbt.beat )
  232. return;
  233. if ( Timeline::snap_magnetic &&
  234. abs_diff( frame, n->original ) > timeline->x_to_ts( snap_pixel ) )
  235. return;
  236. if ( abs_diff( frame, n->original ) < abs_diff( n->original, n->closest ) )
  237. n->closest = frame;
  238. }
  239. /** Set the value pointed to by /frame/ to the frame number of the of
  240. the nearest measure line to /when/. Returns true if the new value of
  241. *frame is valid, false otherwise. */
  242. bool
  243. Timeline::nearest_line ( nframes_t when, nframes_t *frame ) const
  244. {
  245. if ( snap_to == None )
  246. return false;
  247. nearest_line_arg n = { when, -1 };
  248. render_tempomap( when - x_to_ts( w() >> 1 ), x_to_ts( w() ), nearest_line_cb, &n );
  249. *frame = n.closest;
  250. return *frame != (nframes_t)-1;
  251. }
  252. nframes_t
  253. Timeline::x_to_offset ( int x ) const
  254. {
  255. return x_to_ts( max( 0, x - Track::width() ) ) + xoffset;
  256. }
  257. /** draws a single measure line */
  258. static void
  259. draw_measure_cb ( nframes_t frame, const BBT &bbt, void *arg )
  260. {
  261. Fl_Color *color = (Fl_Color*)arg;
  262. fl_color( FL_BLACK );
  263. fl_line_style( FL_DASH, 0 );
  264. if ( bbt.beat )
  265. ++color;
  266. fl_color( *color );
  267. const int x = timeline->ts_to_x( frame - timeline->xoffset ) + Track::width();
  268. fl_line( x, 0, x, 5000 );
  269. fl_line_style( FL_SOLID, 0 );
  270. }
  271. /* FIXME: wrong place for this */
  272. const float ticks_per_beat = 1920.0;
  273. void
  274. Timeline::update_tempomap ( void )
  275. {
  276. /* FIXME: we need some type of locking! */
  277. _tempomap.clear();
  278. for ( list <Sequence_Widget *>::const_iterator i = time_track->_widgets.begin();
  279. i != time_track->_widgets.end(); ++i )
  280. _tempomap.push_back( *i );
  281. for ( list <Sequence_Widget *>::const_iterator i = tempo_track->_widgets.begin();
  282. i != tempo_track->_widgets.end(); ++i )
  283. _tempomap.push_back( *i );
  284. /* FIXME: shouldn't we ensure that time points always precede
  285. tempo points at the same position? */
  286. _tempomap.sort( Sequence_Widget::sort_func );
  287. }
  288. position_info
  289. Timeline::solve_tempomap ( nframes_t frame ) const
  290. {
  291. return render_tempomap( frame, 0, 0, 0 );
  292. }
  293. /* THREAD: UI and RT */
  294. /** draw appropriate measure lines inside the given bounding box */
  295. position_info
  296. Timeline::render_tempomap( nframes_t start, nframes_t length, measure_line_callback * cb, void *arg ) const
  297. {
  298. const nframes_t end = start + length;
  299. position_info pos;
  300. memset( &pos, 0, sizeof( pos ) );
  301. BBT &bbt = pos.bbt;
  302. const nframes_t samples_per_minute = sample_rate() * 60;
  303. float bpm = 120.0f;
  304. time_sig sig;
  305. sig.beats_per_bar = 4;
  306. sig.beat_type = 4;
  307. nframes_t f = 0;
  308. nframes_t next = 0;
  309. nframes_t frames_per_beat = samples_per_minute / bpm;
  310. /* FIXME: don't we need to sort so that Time_Points always preceed Tempo_Points? */
  311. if ( ! _tempomap.size() )
  312. return pos;
  313. for ( list <const Sequence_Widget *>::const_iterator i = _tempomap.begin();
  314. i != _tempomap.end(); ++i )
  315. {
  316. if ( ! strcmp( (*i)->class_name(), "Tempo_Point" ) )
  317. {
  318. const Tempo_Point *p = (Tempo_Point*)(*i);
  319. bpm = p->tempo();
  320. frames_per_beat = samples_per_minute / bpm;
  321. }
  322. else
  323. {
  324. const Time_Point *p = (Time_Point*)(*i);
  325. sig = p->time();
  326. /* Time point resets beat */
  327. bbt.beat = 0;
  328. }
  329. {
  330. list <const Sequence_Widget *>::const_iterator n = i;
  331. ++n;
  332. if ( n == _tempomap.end() )
  333. next = end;
  334. else
  335. // next = min( (*n)->start(), end );
  336. /* points may not always be aligned with beat boundaries, so we must align here */
  337. next = (*n)->start() - ( ( (*n)->start() - (*i)->start() ) % frames_per_beat );
  338. }
  339. for ( ; f < next; ++bbt.beat, f += frames_per_beat )
  340. {
  341. if ( bbt.beat == sig.beats_per_bar )
  342. {
  343. bbt.beat = 0;
  344. ++bbt.bar;
  345. }
  346. if ( f >= start )
  347. {
  348. /* in the zone */
  349. if ( cb )
  350. cb( f, bbt, arg );
  351. }
  352. /* ugliness to avoid failing out at -1 */
  353. if ( end >= frames_per_beat )
  354. {
  355. if ( f >= end - frames_per_beat )
  356. goto done;
  357. }
  358. else if ( f + frames_per_beat >= end )
  359. goto done;
  360. }
  361. }
  362. done:
  363. pos.frame = f;
  364. pos.tempo = bpm;
  365. pos.beats_per_bar = sig.beats_per_bar;
  366. pos.beat_type = sig.beat_type;
  367. assert( f <= end );
  368. assert( end - f <= frames_per_beat );
  369. /* FIXME: this this right? */
  370. const double frames_per_tick = frames_per_beat / ticks_per_beat;
  371. bbt.tick = ( end - f ) / frames_per_tick;
  372. return pos;
  373. }
  374. void
  375. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  376. {
  377. if ( ! draw_with_measure_lines )
  378. return;
  379. Fl_Color colors[2];
  380. colors[1] = fl_color_average( FL_BLACK, color, 0.65f );
  381. colors[0] = fl_color_average( FL_RED, colors[1], 0.65f );
  382. const nframes_t start = x_to_offset( X );
  383. const nframes_t length = x_to_ts( W );
  384. fl_push_clip( X, Y, W, H );
  385. render_tempomap( start, length, draw_measure_cb, &colors );
  386. fl_pop_clip();
  387. }
  388. /* /\** just like draw mesure lines except that it also draws the BBT values. *\/ */
  389. /* void */
  390. /* Timeline::draw_measure_BBT ( int X, int Y, int W, int H, Fl_Color color ) */
  391. /* { */
  392. /* // render_tempomap( X, Y, W, H, color, true ); */
  393. /* } */
  394. void
  395. Timeline::xposition ( int X )
  396. {
  397. // _old_xposition = xoffset;
  398. /* FIXME: shouldn't have to do this... */
  399. X = min( X, ts_to_x( _length ) - tracks->w() - Track::width() );
  400. xoffset = x_to_ts( X );
  401. damage( FL_DAMAGE_SCROLL );
  402. }
  403. void
  404. Timeline::yposition ( int Y )
  405. {
  406. // _old_yposition = _yposition;
  407. _yposition = Y;
  408. damage( FL_DAMAGE_SCROLL );
  409. }
  410. void
  411. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  412. {
  413. Timeline *tl = (Timeline *)v;
  414. fl_push_clip( X, Y, W, H );
  415. /* fl_color( rand() ); */
  416. /* fl_rectf( X, Y, X + W, Y + H ); */
  417. tl->draw_box();
  418. tl->draw_child( *tl->rulers );
  419. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  420. tl->draw_child( *tl->tracks );
  421. fl_pop_clip();
  422. fl_pop_clip();
  423. }
  424. void
  425. Timeline::resize ( int X, int Y, int W, int H )
  426. {
  427. Fl_Overlay_Window::resize( X, Y, W, H );
  428. /* why is this necessary? */
  429. rulers->resize( Track::width(), 0, W - Track::width() - vscroll->w(), rulers->h() );
  430. /* why is THIS necessary? */
  431. hscroll->resize( 0, H - 18, hscroll->w(), 18 );
  432. vscroll->size( vscroll->w(), H - 18 );
  433. }
  434. void
  435. Timeline::draw ( void )
  436. {
  437. int X, Y, W, H;
  438. int bdx = 0;
  439. int bdw = 0;
  440. X = tracks->x() + bdx + 1;
  441. Y = tracks->y();
  442. W = tracks->w() - bdw - 1;
  443. H = tracks->h();
  444. adjust_vscroll();
  445. if ( ( damage() & FL_DAMAGE_ALL ) || ( damage() & FL_DAMAGE_EXPOSE ) )
  446. {
  447. DMESSAGE( "complete redraw" );
  448. draw_box( box(), 0, 0, w(), h(), color() );
  449. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  450. draw_child( *rulers );
  451. fl_pop_clip();
  452. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  453. draw_child( *tracks );
  454. fl_pop_clip();
  455. draw_child( *hscroll );
  456. draw_child( *vscroll );
  457. if ( p1 != p2 )
  458. {
  459. draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
  460. draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
  461. }
  462. redraw_overlay();
  463. goto done;
  464. }
  465. if ( damage() & FL_DAMAGE_SCROLL )
  466. {
  467. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  468. int dy = _old_yposition - _yposition;
  469. /* draw_child( *rulers ); */
  470. if ( ! dy )
  471. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  472. Y = rulers->y() + rulers->h();
  473. H = h() - rulers->h() - hscroll->h();
  474. if ( dy == 0 )
  475. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  476. else
  477. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  478. }
  479. if ( damage() & FL_DAMAGE_CHILD )
  480. {
  481. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  482. update_child( *rulers );
  483. fl_pop_clip();
  484. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  485. update_child( *tracks );
  486. fl_pop_clip();
  487. update_child( *hscroll );
  488. update_child( *vscroll );
  489. if ( p1 != p2 )
  490. {
  491. draw_cursor( p1, FL_BLUE, draw_full_arrow_symbol );
  492. draw_cursor( p2, FL_GREEN, draw_full_arrow_symbol );
  493. }
  494. }
  495. done:
  496. _old_xposition = xoffset;
  497. _old_yposition = _yposition;
  498. }
  499. void
  500. Timeline::draw_cursor ( nframes_t frame, Fl_Color color, void (*symbol)(Fl_Color) )
  501. {
  502. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  503. if ( frame < xoffset )
  504. return;
  505. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  506. if ( x > tracks->x() + tracks->w() )
  507. return;
  508. fl_color( color );
  509. const int y = rulers->y() + rulers->h();
  510. const int h = this->h() - hscroll->h() - 1;
  511. fl_push_clip( tracks->x() + Track::width(), y, tracks->w(), h );
  512. fl_line( x, y, x, h );
  513. fl_color( fl_darker( color ) );
  514. fl_line( x - 1, y, x - 1, h );
  515. fl_color( FL_BLACK );
  516. fl_line( x + 1, y, x + 1, h );
  517. fl_push_matrix();
  518. fl_translate( x, y );
  519. fl_scale( 16, 8 );
  520. symbol( color );
  521. fl_pop_matrix();
  522. fl_pop_clip();
  523. }
  524. void
  525. Timeline::draw_playhead ( void )
  526. {
  527. draw_cursor( transport->frame, FL_RED, draw_full_arrow_symbol );
  528. }
  529. void
  530. Timeline::redraw_playhead ( void )
  531. {
  532. static nframes_t last_playhead = -1;
  533. if ( last_playhead != transport->frame )
  534. {
  535. redraw_overlay();
  536. last_playhead = transport->frame;
  537. if ( follow_playhead )
  538. {
  539. if ( center_playhead && active() )
  540. xposition( max( 0, ts_to_x( transport->frame ) - ( ( tracks->w() - Track::width() ) >> 1 ) ) );
  541. else if ( ts_to_x( transport->frame ) > ts_to_x( xoffset ) + ( tracks->w() - Track::width() ) )
  542. xposition( ts_to_x( transport->frame ) );
  543. }
  544. }
  545. }
  546. /** called so many times a second to redraw the playhead etc. */
  547. void
  548. Timeline::update_cb ( void *arg )
  549. {
  550. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  551. Timeline *tl = (Timeline *)arg;
  552. tl->redraw_playhead();
  553. }
  554. void
  555. Timeline::draw_overlay ( void )
  556. {
  557. draw_playhead();
  558. if ( ! ( _selection.w && _selection.h ) )
  559. return;
  560. fl_push_clip( tracks->x() + Track::width(), rulers->y() + rulers->h(), tracks->w() - Track::width(), h() - rulers->h() - hscroll->h() );
  561. const Rectangle &r = _selection;
  562. fl_color( FL_BLACK );
  563. fl_line_style( FL_SOLID, 2 );
  564. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  565. fl_color( FL_MAGENTA );
  566. fl_line_style( FL_DASH, 2 );
  567. fl_rect( r.x, r.y, r.w, r.h );
  568. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  569. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  570. fl_line_style( FL_SOLID, 0 );
  571. fl_pop_clip();
  572. }
  573. // #include "Sequence_Widget.H"
  574. /** select all widgets in inside rectangle /r/ */
  575. void
  576. Timeline::select ( const Rectangle &r )
  577. {
  578. const int Y = r.y;
  579. for ( int i = tracks->children(); i-- ; )
  580. {
  581. Track *t = (Track*)tracks->child( i );
  582. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  583. t->select( r.x, r.y, r.w, r.h, true, true );
  584. }
  585. }
  586. void
  587. Timeline::delete_selected ( void )
  588. {
  589. Sequence_Widget::delete_selected();
  590. }
  591. void
  592. Timeline::select_none ( void )
  593. {
  594. Sequence_Widget::select_none();
  595. }
  596. /** An unfortunate necessity for implementing our own DND aside from
  597. * the (bogus) native FLTK system */
  598. Track *
  599. Timeline::track_under ( int Y )
  600. {
  601. for ( int i = tracks->children(); i-- ; )
  602. {
  603. Track *t = (Track*)tracks->child( i );
  604. if ( ! ( t->y() > Y || t->y() + t->h() < Y ) )
  605. return t;
  606. }
  607. return NULL;
  608. }
  609. int
  610. Timeline::handle ( int m )
  611. {
  612. static Drag *drag = NULL;
  613. static bool range = false;
  614. switch ( m )
  615. {
  616. case FL_FOCUS:
  617. case FL_UNFOCUS:
  618. // redraw();
  619. return 1;
  620. case FL_KEYDOWN:
  621. if ( Fl::event_key() == 'r' )
  622. {
  623. range = true;
  624. return 1;
  625. }
  626. return 0;
  627. case FL_KEYUP:
  628. if ( Fl::event_key() == 'r' )
  629. {
  630. range = false;
  631. return 1;
  632. }
  633. return 0;
  634. // case FL_KEYBOARD:
  635. case FL_SHORTCUT:
  636. {
  637. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  638. /* we don't want any keys with modifiers... */
  639. return 0;
  640. switch ( Fl::event_key() )
  641. {
  642. case FL_Delete:
  643. case FL_Home:
  644. case FL_End:
  645. /* keep scrollbar from eating these. */
  646. return 0;
  647. case 'p':
  648. {
  649. int X = Fl::event_x() - Track::width();
  650. if ( X > 0 )
  651. {
  652. transport->locate( xoffset + x_to_ts( X ) );
  653. }
  654. return 1;
  655. }
  656. case '[':
  657. {
  658. int X = Fl::event_x() - Track::width();
  659. if ( X > 0 )
  660. {
  661. p1 = xoffset + x_to_ts( X );
  662. }
  663. /* FIXME: only needs to damage the location of the old cursor! */
  664. redraw();
  665. return 1;
  666. }
  667. case ']':
  668. {
  669. int X = Fl::event_x() - Track::width();
  670. if ( X > 0 )
  671. {
  672. p2 = xoffset + x_to_ts( X );
  673. }
  674. /* FIXME: only needs to damage the location of the old cursor! */
  675. redraw();
  676. return 1;
  677. }
  678. default:
  679. return Fl_Overlay_Window::handle( m );
  680. }
  681. return 0;
  682. }
  683. default:
  684. {
  685. if ( m == FL_PUSH )
  686. Fl::focus( this );
  687. int r = Fl_Overlay_Window::handle( m );
  688. if ( m != FL_RELEASE && r )
  689. return r;
  690. const int X = Fl::event_x();
  691. const int Y = Fl::event_y();
  692. switch ( m )
  693. {
  694. case FL_PUSH:
  695. {
  696. // take_focus();
  697. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  698. return 0;
  699. if ( Fl::event_button1() )
  700. {
  701. assert( ! drag );
  702. drag = new Drag( X - x(), Y - y() );
  703. _selection.x = drag->x;
  704. _selection.y = drag->y;
  705. }
  706. else if ( Fl::test_shortcut( FL_BUTTON3 ) && ! Fl::event_shift() )
  707. {
  708. Fl_Menu_Item menu[] =
  709. {
  710. { "Add Track", 0, 0, 0, FL_SUBMENU },
  711. { "Audio", 0, 0, 0 },
  712. { 0 },
  713. { 0 },
  714. };
  715. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  716. if ( r == &menu[1] )
  717. {
  718. /* FIXME: prompt for I/O config? */
  719. Loggable::block_start();
  720. /* add audio track */
  721. char *name = get_unique_track_name( "Audio" );
  722. Track *t = new Track( name );
  723. Audio_Sequence *o = new Audio_Sequence( t );
  724. // new Control_Sequence( t );
  725. add_track( t );
  726. t->sequence( o );
  727. Loggable::block_end();
  728. }
  729. }
  730. else
  731. return 0;
  732. break;
  733. }
  734. case FL_DRAG:
  735. {
  736. int ox = X - drag->x;
  737. int oy = Y - drag->y;
  738. if ( ox < 0 )
  739. _selection.x = X;
  740. if ( oy < 0 )
  741. _selection.y = Y;
  742. _selection.w = abs( ox );
  743. _selection.h = abs( oy );
  744. if ( range )
  745. {
  746. p1 = x_to_offset( _selection.x );
  747. p2 = x_to_offset( _selection.x + _selection.w );
  748. redraw();
  749. }
  750. break;
  751. }
  752. case FL_RELEASE:
  753. {
  754. delete drag;
  755. drag = NULL;
  756. if ( range )
  757. {
  758. p1 = x_to_offset( _selection.x );
  759. p2 = x_to_offset( _selection.x + _selection.w );
  760. redraw();
  761. }
  762. else
  763. select( _selection );
  764. _selection.w = _selection.h = 0;
  765. break;
  766. }
  767. default:
  768. return 0;
  769. break;
  770. }
  771. redraw_overlay();
  772. return 1;
  773. }
  774. }
  775. }
  776. void
  777. Timeline::zoom_in ( void )
  778. {
  779. hscroll->zoom_in();
  780. }
  781. void
  782. Timeline::zoom_out ( void )
  783. {
  784. hscroll->zoom_out();
  785. }
  786. /** zoom the display to show /secs/ seconds per screen */
  787. void
  788. Timeline::zoom ( float secs )
  789. {
  790. const int sw = tracks->w() - Track::width();
  791. int fpp = (int)((secs * sample_rate()) / sw);
  792. int p = 0;
  793. while ( 1 << p < fpp ) p++;
  794. hscroll->zoom( p );
  795. redraw();
  796. }
  797. void
  798. Timeline::zoom_fit ( void )
  799. {
  800. zoom( _length / (float)sample_rate() );
  801. }
  802. Track *
  803. Timeline::track_by_name ( const char *name )
  804. {
  805. for ( int i = tracks->children(); i-- ; )
  806. {
  807. Track *t = (Track*)tracks->child( i );
  808. if ( ! strcmp( name, t->name() ) )
  809. return t;
  810. }
  811. return NULL;
  812. }
  813. char *
  814. Timeline::get_unique_track_name ( const char *name )
  815. {
  816. char pat[256];
  817. strcpy( pat, name );
  818. for ( int i = 1; track_by_name( pat ); ++i )
  819. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  820. return strdup( pat );
  821. }
  822. void
  823. Timeline::add_track ( Track *track )
  824. {
  825. DMESSAGE( "added new track to the timeline" );
  826. /* FIXME: do locking */
  827. tracks->add( track );
  828. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  829. redraw();
  830. }
  831. void
  832. Timeline::remove_track ( Track *track )
  833. {
  834. DMESSAGE( "removed track from the timeline" );
  835. /* FIXME: do locking */
  836. /* FIXME: what to do about track contents? */
  837. tracks->remove( track );
  838. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  839. redraw();
  840. }
  841. /** Initiate recording for all armed tracks */
  842. bool
  843. Timeline::record ( void )
  844. {
  845. /* FIXME: right place for this? */
  846. transport->recording = true;
  847. Loggable::block_start();
  848. for ( int i = tracks->children(); i-- ; )
  849. {
  850. Track *t = (Track*)tracks->child( i );
  851. if ( t->armed() && t->record_ds )
  852. t->record_ds->start( transport->frame );
  853. }
  854. deactivate();
  855. return true;
  856. }
  857. /** stop recording for all armed tracks */
  858. void
  859. Timeline::stop ( void )
  860. {
  861. nframes_t frame = transport->frame;
  862. for ( int i = tracks->children(); i-- ; )
  863. {
  864. Track *t = (Track*)tracks->child( i );
  865. if ( t->armed() && t->record_ds )
  866. t->record_ds->stop( frame );
  867. }
  868. Loggable::block_end();
  869. activate();
  870. transport->recording = false;
  871. }
  872. /**********/
  873. /* Engine */
  874. /**********/
  875. /** call process() on each track header */
  876. nframes_t
  877. Timeline::process ( nframes_t nframes )
  878. {
  879. for ( int i = tracks->children(); i-- ; )
  880. {
  881. Track *t = (Track*)tracks->child( i );
  882. t->process( nframes );
  883. }
  884. /* FIXME: BOGUS */
  885. return nframes;
  886. }
  887. /* THREAD: RT */
  888. void
  889. Timeline::seek ( nframes_t frame )
  890. {
  891. for ( int i = tracks->children(); i-- ; )
  892. {
  893. Track *t = (Track*)tracks->child( i );
  894. t->seek( frame );
  895. }
  896. }
  897. /* THREAD: RT (non-RT) */
  898. void
  899. Timeline::resize_buffers ( nframes_t nframes )
  900. {
  901. for ( int i = tracks->children(); i-- ; )
  902. {
  903. Track *t = (Track*)tracks->child( i );
  904. t->resize_buffers( nframes );
  905. }
  906. }
  907. /* THREAD: RT */
  908. int
  909. Timeline::seek_pending ( void )
  910. {
  911. int r = 0;
  912. for ( int i = tracks->children(); i-- ; )
  913. {
  914. Track *t = (Track*)tracks->child( i );
  915. if ( t->playback_ds )
  916. r += t->playback_ds->buffer_percent() < 50;
  917. }
  918. return r;
  919. }
  920. /* FIXME: shouldn't these belong to the engine? */
  921. int
  922. Timeline::total_input_buffer_percent ( void )
  923. {
  924. int r = 0;
  925. int cnt = 0;
  926. for ( int i = tracks->children(); i-- ; )
  927. {
  928. Track *t = (Track*)tracks->child( i );
  929. if ( t->record_ds )
  930. {
  931. ++cnt;
  932. r += t->record_ds->buffer_percent();
  933. }
  934. }
  935. if ( ! cnt )
  936. return 0;
  937. return r / cnt;
  938. }
  939. int
  940. Timeline::total_output_buffer_percent ( void )
  941. {
  942. int r = 0;
  943. int cnt = 0;
  944. for ( int i = tracks->children(); i-- ; )
  945. {
  946. Track *t = (Track*)tracks->child( i );
  947. if ( t->playback_ds )
  948. {
  949. ++cnt;
  950. r += t->playback_ds->buffer_percent();
  951. }
  952. }
  953. if ( ! cnt )
  954. return 0;
  955. return r / cnt;
  956. }
  957. /** wait for I/O threads to fill their buffers */
  958. void
  959. Timeline::wait_for_buffers ( void )
  960. {
  961. while ( total_output_buffer_percent() + total_input_buffer_percent() < 200 )
  962. usleep( 5000 );
  963. }
  964. int
  965. Timeline::total_playback_xruns ( void )
  966. {
  967. int r = 0;
  968. for ( int i = tracks->children(); i-- ; )
  969. {
  970. Track *t = (Track*)tracks->child( i );
  971. if ( t->playback_ds )
  972. r += t->playback_ds->xruns();
  973. }
  974. return r;
  975. }
  976. int
  977. Timeline::total_capture_xruns ( void )
  978. {
  979. int r = 0;
  980. for ( int i = tracks->children(); i-- ; )
  981. {
  982. Track *t = (Track*)tracks->child( i );
  983. if ( t->record_ds )
  984. r += t->record_ds->xruns();
  985. }
  986. return r;
  987. }