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.

706 lines
17KB

  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_Track.H"
  20. #include "Time_Track.H"
  21. #include "Audio_Track.H"
  22. #include "Control_Track.H"
  23. #include <FL/Fl_Scrollbar.H>
  24. // #include <FL/Fl_Image.H>
  25. // #include <FL/Fl_RGB_Image.H> // needed for alpha blending
  26. #include "Track_Header.H"
  27. const float UPDATE_FREQ = 0.02f;
  28. #include "Playback_DS.H"
  29. #include "Transport.H"
  30. void
  31. Timeline::cb_scroll ( Fl_Widget *w, void *v )
  32. {
  33. ((Timeline*)v)->cb_scroll( w );
  34. }
  35. void
  36. Timeline::cb_scroll ( Fl_Widget *w )
  37. {
  38. if ( w == vscroll )
  39. {
  40. tracks->position( tracks->x(), (rulers->y() + rulers->h()) - vscroll->value() );
  41. yposition( vscroll->value() );
  42. int rh = h() - rulers->h();
  43. vscroll->value( vscroll->value(), 30, 0, max( tracks->h() - rh, rh) );
  44. }
  45. else
  46. {
  47. if ( hscroll->zoom_changed() )
  48. {
  49. _fpp = hscroll->zoom() * 1;
  50. int maxx = ts_to_x( _length );
  51. hscroll->range( 0, maxx );
  52. redraw();
  53. }
  54. else
  55. {
  56. xposition( hscroll->value() );
  57. }
  58. }
  59. }
  60. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  61. {
  62. box( FL_FLAT_BOX );
  63. xoffset = 0;
  64. _enable_measure_lines = true;
  65. X = Y = 0;
  66. {
  67. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  68. o->range( 0, 48000 * 300 );
  69. o->zoom_range( 2, 8192 );
  70. o->zoom( 256 );
  71. o->type( FL_HORIZONTAL );
  72. o->callback( cb_scroll, this );
  73. hscroll = o;
  74. }
  75. {
  76. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  77. o->type( FL_VERTICAL );
  78. // o->step( 10 );
  79. o->callback( cb_scroll, this );
  80. vscroll = o;
  81. }
  82. {
  83. Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  84. o->type( Fl_Pack::VERTICAL );
  85. {
  86. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  87. o->color( FL_RED );
  88. o->add( new Tempo_Point( 0, 120 ) );
  89. o->add( new Tempo_Point( 56000, 250 ) );
  90. o->label( "Tempo" );
  91. o->align( FL_ALIGN_LEFT );
  92. tempo_track = o;
  93. // o->end();
  94. }
  95. {
  96. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  97. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  98. o->add( new Time_Point( 0, 4, 4 ) );
  99. o->add( new Time_Point( 345344, 6, 8 ) );
  100. o->label( "Time" );
  101. o->align( FL_ALIGN_LEFT );
  102. time_track = o;
  103. // o->end();
  104. }
  105. o->size( o->w(), o->child( 0 )->h() * o->children() );
  106. rulers = o;
  107. o->end();
  108. }
  109. {
  110. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  111. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  112. _sample_rate = 44100;
  113. _fpp = 256;
  114. _length = _sample_rate * 60 * 2;
  115. {
  116. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  117. o->type( Fl_Pack::VERTICAL );
  118. o->spacing( 0 );
  119. for ( int i = 1; i--; )
  120. {
  121. // Track_Header *t = new Track_Header( 0, 0, W, 75 );
  122. Track_Header *t = new Track_Header( 0, 0, W, 30 );
  123. Track *o = new Audio_Track( 0, 0, 1, 100 );
  124. t->track( o );
  125. t->add( new Audio_Track( 0, 0, 1, 100 ) );
  126. t->add( new Audio_Track( 0, 0, 1, 100 ) );
  127. t->add_control( new Control_Track( 0, 0, 1, 100 ) );
  128. t->color( (Fl_Color)rand() );
  129. }
  130. tracks = o;
  131. o->end();
  132. }
  133. /* scroll = o; */
  134. /* o->end(); */
  135. }
  136. /* make sure scrollbars are on top */
  137. add( vscroll );
  138. add( hscroll );
  139. vscroll->range( 0, tracks->h() );
  140. redraw();
  141. end();
  142. Fl::add_timeout( UPDATE_FREQ, update_cb, this );
  143. }
  144. float
  145. Timeline::beats_per_minute ( nframes_t when ) const
  146. {
  147. return tempo_track->beats_per_minute( when );
  148. }
  149. int
  150. Timeline::beats_per_bar ( nframes_t when ) const
  151. {
  152. time_sig t = time_track->time( when );
  153. return t.beats_per_bar;
  154. }
  155. void
  156. Timeline::beats_per_minute ( nframes_t when, float bpm )
  157. {
  158. tempo_track->add( new Tempo_Point( when, bpm ) );
  159. }
  160. /** return the absolute pixel of the nearest measure line to /x/ */
  161. int
  162. Timeline::nearest_line ( int ix )
  163. {
  164. for ( int x = ix - 10; x < ix + 10; ++x )
  165. {
  166. const int measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  167. // const int abs_x = ts_to_x( xoffset ) + x;
  168. if ( 0 == x % measure )
  169. return x;
  170. }
  171. return -1;
  172. }
  173. /** draw appropriate measure lines inside the given bounding box */
  174. /* FIXME: this function *really* needs to be optimized. Currently it
  175. searched both the time and tempo lists once for every horiontal
  176. pixel and performs a number of calculations--this is slow. */
  177. void
  178. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  179. {
  180. if ( ! _enable_measure_lines )
  181. return;
  182. // fl_line_style( FL_DASH, 2 );
  183. fl_line_style( FL_DASH, 0 );
  184. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  185. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  186. int measure;
  187. for ( int x = X; x < X + W; ++x )
  188. {
  189. measure = ts_to_x( (double)(_sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  190. const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
  191. if ( 0 == abs_x % measure )
  192. {
  193. int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
  194. if ( 0 == (abs_x / measure) % bpb )
  195. {
  196. if ( measure * bpb < 8 )
  197. break;
  198. fl_color( bar );
  199. }
  200. else
  201. {
  202. if ( measure < 8 )
  203. continue;
  204. fl_color( beat );
  205. }
  206. fl_line( x, Y, x, Y + H );
  207. }
  208. }
  209. fl_line_style( FL_SOLID, 0 );
  210. }
  211. void
  212. Timeline::xposition ( int X )
  213. {
  214. // _old_xposition = xoffset;
  215. xoffset = x_to_ts( X );
  216. damage( FL_DAMAGE_SCROLL );
  217. }
  218. void
  219. Timeline::yposition ( int Y )
  220. {
  221. // _old_yposition = _yposition;
  222. _yposition = Y;
  223. damage( FL_DAMAGE_SCROLL );
  224. }
  225. void
  226. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  227. {
  228. Timeline *tl = (Timeline *)v;
  229. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  230. fl_push_clip( X, Y, W, H );
  231. fl_color( rand() );
  232. fl_rectf( X, Y, X + W, Y + H );
  233. tl->draw_child( *tl->rulers );
  234. /* headers */
  235. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  236. tl->draw_child( *tl->tracks );
  237. fl_pop_clip();
  238. /* track bodies */
  239. fl_push_clip( tl->tracks->x() + Track_Header::width(), tl->rulers->y() + tl->rulers->h(), tl->tracks->w() - Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  240. tl->draw_child( *tl->tracks );
  241. fl_pop_clip();
  242. // tl->draw_child( *tl->tracks );
  243. fl_pop_clip();
  244. }
  245. static unsigned char *rect_image;
  246. void
  247. Timeline::draw ( void )
  248. {
  249. int X, Y, W, H;
  250. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  251. Y = tracks->y();
  252. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  253. H = tracks->h();
  254. /* if ( damage() & FL_DAMAGE_USER1 ) */
  255. /* { */
  256. /* /\* save the rectangle so we can draw it (darkened) in the overlay *\/ */
  257. /* Rectangle &r = _selection; */
  258. /* make_current(); */
  259. /* rect_image = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  260. /* return; */
  261. /* } */
  262. if ( (damage() & FL_DAMAGE_ALL)
  263. ||
  264. damage() & FL_DAMAGE_EXPOSE )
  265. {
  266. draw_box( box(), 0, 0, w(), h(), color() );
  267. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  268. draw_child( *rulers );
  269. fl_pop_clip();
  270. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  271. draw_child( *tracks );
  272. fl_pop_clip();
  273. draw_child( *hscroll );
  274. draw_child( *vscroll );
  275. redraw_overlay();
  276. /* Rectangle &r = _selection; */
  277. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  278. /* Fl_RGB_Image bi( data, r.w, r.h, 3 ); */
  279. /* bi.color_average( FL_BLACK, 0.50f ); */
  280. /* bi.draw( r.x, r.y ); */
  281. /* delete[] data; */
  282. /* if ( r.w && r.h ) */
  283. /* { */
  284. /* const unsigned char data[] = { 0, 127, 0, 96, */
  285. /* 0, 96, 0, 127 }; */
  286. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  287. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  288. /* bi2->draw( r.x, r.y ); */
  289. /* delete bi2; */
  290. /* } */
  291. return;
  292. }
  293. if ( damage() & FL_DAMAGE_CHILD )
  294. {
  295. // draw_box( box(), 0, 0, w(), h(), color() );
  296. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  297. update_child( *rulers );
  298. fl_pop_clip();
  299. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  300. update_child( *tracks );
  301. fl_pop_clip();
  302. update_child( *hscroll );
  303. update_child( *vscroll );
  304. }
  305. if ( damage() & FL_DAMAGE_SCROLL )
  306. {
  307. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  308. int dy = _old_yposition - _yposition;
  309. if ( ! dy )
  310. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  311. Y = rulers->y() + rulers->h();
  312. H = h() - rulers->h() - hscroll->h();
  313. if ( dy == 0 )
  314. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  315. else
  316. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  317. _old_xposition = xoffset;
  318. _old_yposition = _yposition;
  319. }
  320. }
  321. void
  322. Timeline::draw_playhead ( void )
  323. {
  324. int x = ( ts_to_x( transport.frame ) - ts_to_x( xoffset ) ) + tracks->x() + Track_Header::width();
  325. if ( x < tracks->x() + Track_Header::width() || x > tracks->x() + tracks->w() )
  326. return;
  327. fl_color( FL_RED );
  328. int y = rulers->y() + rulers->h();
  329. int h = this->h() - hscroll->h();
  330. fl_rectf( x - 2, y, 5, 2 );
  331. fl_line( x, y, x, h );
  332. }
  333. void
  334. Timeline::redraw_playhead ( void )
  335. {
  336. static nframes_t last_playhead = -1;
  337. if ( last_playhead != transport.frame )
  338. {
  339. redraw_overlay();
  340. last_playhead = transport.frame;
  341. }
  342. }
  343. /** called so many times a second to redraw the playhead etc. */
  344. void
  345. Timeline::update_cb ( void *arg )
  346. {
  347. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  348. Timeline *tl = (Timeline *)arg;
  349. tl->redraw_playhead();
  350. }
  351. void
  352. Timeline::draw_overlay ( void )
  353. {
  354. draw_playhead();
  355. if ( ! ( _selection.w && _selection.h ) )
  356. return;
  357. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  358. const Rectangle &r = _selection;
  359. fl_color( FL_BLACK );
  360. fl_line_style( FL_SOLID, 2 );
  361. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  362. fl_color( FL_MAGENTA );
  363. fl_line_style( FL_DASH, 2 );
  364. fl_rect( r.x, r.y, r.w, r.h );
  365. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  366. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  367. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  368. fl_line_style( FL_SOLID, 0 );
  369. /* const unsigned char data[] = { 127, 127, 127, 96, */
  370. /* 127, 96, 127, 40 }; */
  371. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  372. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  373. /* bi2->draw( r.x, r.y ); */
  374. /* delete bi2; */
  375. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  376. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  377. /* bi.color_average( FL_BLACK, 0.50f ); */
  378. /* bi.draw( r.x, r.y ); */
  379. /* delete[] rect_image; */
  380. /* rect_image = NULL; */
  381. fl_pop_clip();
  382. }
  383. // #include "Track_Widget.H"
  384. /** select all widgets in inside rectangle /r/ */
  385. void
  386. Timeline::select( const Rectangle &r )
  387. {
  388. const int Y = r.y;
  389. for ( int i = tracks->children(); i-- ; )
  390. {
  391. Track_Header *t = (Track_Header*)tracks->child( i );
  392. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  393. t->track()->select_range( r.x, r.w );
  394. }
  395. }
  396. int
  397. Timeline::handle ( int m )
  398. {
  399. static Drag *drag = NULL;
  400. switch ( m )
  401. {
  402. case FL_KEYBOARD:
  403. {
  404. switch ( Fl::event_key() )
  405. {
  406. case FL_Delete:
  407. {
  408. Track_Widget::delete_selected();
  409. return 1;
  410. }
  411. default:
  412. return Fl_Overlay_Window::handle( m );
  413. }
  414. return 0;
  415. }
  416. default:
  417. {
  418. int r = Fl_Overlay_Window::handle( m );
  419. if ( m != FL_RELEASE && r )
  420. return r;
  421. const int X = Fl::event_x();
  422. const int Y = Fl::event_y();
  423. switch ( m )
  424. {
  425. case FL_PUSH:
  426. {
  427. take_focus();
  428. if ( ! Fl::event_button1() )
  429. return 0;
  430. assert( ! drag );
  431. drag = new Drag( X - x(), Y - y() );
  432. _selection.x = drag->x;
  433. _selection.y = drag->y;
  434. break;
  435. }
  436. case FL_DRAG:
  437. {
  438. int ox = X - drag->x;
  439. int oy = Y - drag->y;
  440. if ( ox < 0 )
  441. _selection.x = X;
  442. if ( oy < 0 )
  443. _selection.y = Y;
  444. _selection.w = abs( ox );
  445. _selection.h = abs( oy );
  446. break;
  447. }
  448. case FL_RELEASE:
  449. {
  450. delete drag;
  451. drag = NULL;
  452. select( _selection );
  453. _selection.w = _selection.h = 0;
  454. break;
  455. }
  456. default:
  457. return 0;
  458. break;
  459. }
  460. redraw_overlay();
  461. return 1;
  462. }
  463. }
  464. }
  465. /**********/
  466. /* Engine */
  467. /**********/
  468. /** call process() on each track header */
  469. nframes_t
  470. Timeline::process ( nframes_t nframes )
  471. {
  472. for ( int i = tracks->children(); i-- ; )
  473. {
  474. Track_Header *t = (Track_Header*)tracks->child( i );
  475. t->process( nframes );
  476. }
  477. /* FIXME: BOGUS */
  478. return nframes;
  479. }
  480. /* THREAD: RT */
  481. void
  482. Timeline::seek ( nframes_t frame )
  483. {
  484. for ( int i = tracks->children(); i-- ; )
  485. {
  486. Track_Header *t = (Track_Header*)tracks->child( i );
  487. t->seek( frame );
  488. }
  489. }
  490. /* THREAD: RT */
  491. int
  492. Timeline::seek_pending ( void )
  493. {
  494. int r = 0;
  495. for ( int i = tracks->children(); i-- ; )
  496. {
  497. Track_Header *t = (Track_Header*)tracks->child( i );
  498. if ( t->playback_ds )
  499. r += t->playback_ds->buffer_percent() < 50;
  500. }
  501. }