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.

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