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.

1336 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. redraw_overlay();
  446. /* Rectangle &r = _selection; */
  447. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  448. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  449. /* bi.color_average( FL_BLACK, 0.50f ); */
  450. /* bi.draw( r.x, r.y ); */
  451. /* delete[] data; */
  452. /* if ( r.w && r.h ) */
  453. /* { */
  454. /* const unsigned char data[] = { 0, 127, 0, 96, */
  455. /* 0, 96, 0, 127 }; */
  456. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  457. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  458. /* bi2->draw( r.x, r.y ); */
  459. /* delete bi2; */
  460. /* } */
  461. goto done;
  462. }
  463. if ( damage() & FL_DAMAGE_SCROLL )
  464. {
  465. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  466. int dy = _old_yposition - _yposition;
  467. /* draw_child( *rulers ); */
  468. if ( ! dy )
  469. fl_scroll( rulers->x(), rulers->y(), rulers->w(), rulers->h(), dx, 0, draw_clip, this );
  470. Y = rulers->y() + rulers->h();
  471. H = h() - rulers->h() - hscroll->h();
  472. if ( dy == 0 )
  473. fl_scroll( X + Track::width(), Y, W - Track::width(), H, dx, dy, draw_clip, this );
  474. else
  475. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  476. }
  477. if ( damage() & FL_DAMAGE_CHILD )
  478. {
  479. fl_push_clip( rulers->x(), rulers->y(), rulers->w(), rulers->h() );
  480. update_child( *rulers );
  481. fl_pop_clip();
  482. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - rulers->h() - hscroll->h() );
  483. update_child( *tracks );
  484. fl_pop_clip();
  485. update_child( *hscroll );
  486. update_child( *vscroll );
  487. }
  488. done:
  489. _old_xposition = xoffset;
  490. _old_yposition = _yposition;
  491. }
  492. void
  493. Timeline::draw_cursor ( nframes_t frame, Fl_Color color )
  494. {
  495. // int x = ( ts_to_x( frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track::width();
  496. if ( frame < xoffset )
  497. return;
  498. const int x = ts_to_x( frame - xoffset ) + tracks->x() + Track::width();
  499. if ( x > tracks->x() + tracks->w() )
  500. return;
  501. fl_color( color );
  502. const int y = rulers->y() + rulers->h();
  503. const int h = this->h() - hscroll->h();
  504. fl_push_clip( Track::width(), y, tracks->w(), h );
  505. fl_line( x, y, x, h );
  506. fl_color( fl_darker( color ) );
  507. fl_line( x - 1, y, x - 1, h );
  508. fl_color( FL_BLACK );
  509. fl_line( x + 1, y, x + 1, h );
  510. /* draw cap */
  511. fl_color( color );
  512. fl_begin_polygon();
  513. fl_vertex( x - 8, y );
  514. fl_vertex( x, y + 8 );
  515. fl_vertex( x + 8, y );
  516. fl_end_polygon();
  517. /* draw cap outline */
  518. fl_color( FL_BLACK );
  519. fl_begin_line();
  520. fl_vertex( x - 8, y );
  521. fl_vertex( x, y + 8 );
  522. fl_vertex( x + 8, y );
  523. fl_end_line();
  524. fl_pop_clip();
  525. }
  526. void
  527. Timeline::draw_playhead ( void )
  528. {
  529. if ( p1 != p2 )
  530. {
  531. draw_cursor( p1, FL_BLUE );
  532. draw_cursor( p2, FL_GREEN );
  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. return 1;
  669. }
  670. case ']':
  671. {
  672. int X = Fl::event_x() - Track::width();
  673. if ( X > 0 )
  674. {
  675. p2 = xoffset + x_to_ts( X );
  676. }
  677. return 1;
  678. }
  679. default:
  680. return Fl_Overlay_Window::handle( m );
  681. }
  682. return 0;
  683. }
  684. default:
  685. {
  686. if ( m == FL_PUSH )
  687. Fl::focus( this );
  688. int r = Fl_Overlay_Window::handle( m );
  689. if ( m != FL_RELEASE && r )
  690. return r;
  691. const int X = Fl::event_x();
  692. const int Y = Fl::event_y();
  693. switch ( m )
  694. {
  695. case FL_PUSH:
  696. {
  697. // take_focus();
  698. if ( Fl::event_state() & ( FL_ALT | FL_CTRL | FL_SHIFT ) )
  699. return 0;
  700. if ( Fl::event_button1() )
  701. {
  702. assert( ! drag );
  703. drag = new Drag( X - x(), Y - y() );
  704. _selection.x = drag->x;
  705. _selection.y = drag->y;
  706. }
  707. else if ( Fl::test_shortcut( FL_BUTTON3 ) && ! Fl::event_shift() )
  708. {
  709. Fl_Menu_Item menu[] =
  710. {
  711. { "Add Track", 0, 0, 0, FL_SUBMENU },
  712. { "Audio", 0, 0, 0 },
  713. { 0 },
  714. { 0 },
  715. };
  716. const Fl_Menu_Item *r = menu->popup( X, Y, "Timeline" );
  717. if ( r == &menu[1] )
  718. {
  719. /* FIXME: prompt for I/O config? */
  720. Loggable::block_start();
  721. /* add audio track */
  722. char *name = get_unique_track_name( "Audio" );
  723. Track *t = new Track( name );
  724. Audio_Sequence *o = new Audio_Sequence( t );
  725. // new Control_Sequence( t );
  726. add_track( t );
  727. t->sequence( o );
  728. Loggable::block_end();
  729. }
  730. }
  731. else
  732. return 0;
  733. break;
  734. }
  735. case FL_DRAG:
  736. {
  737. int ox = X - drag->x;
  738. int oy = Y - drag->y;
  739. if ( ox < 0 )
  740. _selection.x = X;
  741. if ( oy < 0 )
  742. _selection.y = Y;
  743. _selection.w = abs( ox );
  744. _selection.h = abs( oy );
  745. break;
  746. }
  747. case FL_RELEASE:
  748. {
  749. delete drag;
  750. drag = NULL;
  751. select( _selection );
  752. _selection.w = _selection.h = 0;
  753. break;
  754. }
  755. default:
  756. return 0;
  757. break;
  758. }
  759. redraw_overlay();
  760. return 1;
  761. }
  762. }
  763. }
  764. void
  765. Timeline::zoom_in ( void )
  766. {
  767. hscroll->zoom_in();
  768. }
  769. void
  770. Timeline::zoom_out ( void )
  771. {
  772. hscroll->zoom_out();
  773. }
  774. /** zoom the display to show /secs/ seconds per screen */
  775. void
  776. Timeline::zoom ( float secs )
  777. {
  778. const int sw = tracks->w() - Track::width();
  779. int fpp = (int)((secs * sample_rate()) / sw);
  780. int p = 0;
  781. while ( 1 << p < fpp ) p++;
  782. hscroll->zoom( p );
  783. redraw();
  784. }
  785. void
  786. Timeline::zoom_fit ( void )
  787. {
  788. zoom( _length / (float)sample_rate() );
  789. }
  790. Track *
  791. Timeline::track_by_name ( const char *name )
  792. {
  793. for ( int i = tracks->children(); i-- ; )
  794. {
  795. Track *t = (Track*)tracks->child( i );
  796. if ( ! strcmp( name, t->name() ) )
  797. return t;
  798. }
  799. return NULL;
  800. }
  801. char *
  802. Timeline::get_unique_track_name ( const char *name )
  803. {
  804. char pat[256];
  805. strcpy( pat, name );
  806. for ( int i = 1; track_by_name( pat ); ++i )
  807. snprintf( pat, sizeof( pat ), "%s.%d", name, i );
  808. return strdup( pat );
  809. }
  810. void
  811. Timeline::add_track ( Track *track )
  812. {
  813. DMESSAGE( "added new track to the timeline" );
  814. /* FIXME: do locking */
  815. tracks->add( track );
  816. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  817. redraw();
  818. }
  819. void
  820. Timeline::remove_track ( Track *track )
  821. {
  822. DMESSAGE( "removed track from the timeline" );
  823. /* FIXME: do locking */
  824. /* FIXME: what to do about track contents? */
  825. tracks->remove( track );
  826. /* FIXME: why is this necessary? doesn't the above add do DAMAGE_CHILD? */
  827. redraw();
  828. }
  829. /** Initiate recording for all armed tracks */
  830. bool
  831. Timeline::record ( void )
  832. {
  833. /* FIXME: right place for this? */
  834. transport->recording = true;
  835. Loggable::block_start();
  836. for ( int i = tracks->children(); i-- ; )
  837. {
  838. Track *t = (Track*)tracks->child( i );
  839. if ( t->armed() && t->record_ds )
  840. t->record_ds->start( transport->frame );
  841. }
  842. deactivate();
  843. return true;
  844. }
  845. /** stop recording for all armed tracks */
  846. void
  847. Timeline::stop ( void )
  848. {
  849. nframes_t frame = transport->frame;
  850. for ( int i = tracks->children(); i-- ; )
  851. {
  852. Track *t = (Track*)tracks->child( i );
  853. if ( t->armed() && t->record_ds )
  854. t->record_ds->stop( frame );
  855. }
  856. Loggable::block_end();
  857. activate();
  858. transport->recording = false;
  859. }
  860. /**********/
  861. /* Engine */
  862. /**********/
  863. /** call process() on each track header */
  864. nframes_t
  865. Timeline::process ( nframes_t nframes )
  866. {
  867. for ( int i = tracks->children(); i-- ; )
  868. {
  869. Track *t = (Track*)tracks->child( i );
  870. t->process( nframes );
  871. }
  872. /* FIXME: BOGUS */
  873. return nframes;
  874. }
  875. /* THREAD: RT */
  876. void
  877. Timeline::seek ( nframes_t frame )
  878. {
  879. for ( int i = tracks->children(); i-- ; )
  880. {
  881. Track *t = (Track*)tracks->child( i );
  882. t->seek( frame );
  883. }
  884. }
  885. /* THREAD: RT */
  886. int
  887. Timeline::seek_pending ( void )
  888. {
  889. int r = 0;
  890. for ( int i = tracks->children(); i-- ; )
  891. {
  892. Track *t = (Track*)tracks->child( i );
  893. if ( t->playback_ds )
  894. r += t->playback_ds->buffer_percent() < 50;
  895. }
  896. return r;
  897. }
  898. /* FIXME: shouldn't these belong to the engine? */
  899. int
  900. Timeline::total_input_buffer_percent ( void )
  901. {
  902. int r = 0;
  903. int cnt = 0;
  904. for ( int i = tracks->children(); i-- ; )
  905. {
  906. Track *t = (Track*)tracks->child( i );
  907. if ( t->record_ds )
  908. {
  909. ++cnt;
  910. r += t->record_ds->buffer_percent();
  911. }
  912. }
  913. if ( ! cnt )
  914. return 0;
  915. return r / cnt;
  916. }
  917. int
  918. Timeline::total_output_buffer_percent ( void )
  919. {
  920. int r = 0;
  921. int cnt = 0;
  922. for ( int i = tracks->children(); i-- ; )
  923. {
  924. Track *t = (Track*)tracks->child( i );
  925. if ( t->playback_ds )
  926. {
  927. ++cnt;
  928. r += t->playback_ds->buffer_percent();
  929. }
  930. }
  931. if ( ! cnt )
  932. return 0;
  933. return r / cnt;
  934. }
  935. /** wait for I/O threads to fill their buffers */
  936. void
  937. Timeline::wait_for_buffers ( void )
  938. {
  939. while ( total_output_buffer_percent() + total_input_buffer_percent() < 200 )
  940. usleep( 5000 );
  941. }
  942. int
  943. Timeline::total_playback_xruns ( void )
  944. {
  945. int r = 0;
  946. for ( int i = tracks->children(); i-- ; )
  947. {
  948. Track *t = (Track*)tracks->child( i );
  949. if ( t->playback_ds )
  950. r += t->playback_ds->xruns();
  951. }
  952. return r;
  953. }
  954. int
  955. Timeline::total_capture_xruns ( void )
  956. {
  957. int r = 0;
  958. for ( int i = tracks->children(); i-- ; )
  959. {
  960. Track *t = (Track*)tracks->child( i );
  961. if ( t->record_ds )
  962. r += t->record_ds->xruns();
  963. }
  964. return r;
  965. }