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.

1327 lines
29KB

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