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.

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