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.

691 lines
16KB

  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 "Disk_Stream.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 = 2; 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 < 0 || x > tracks->w() )
  326. return;
  327. fl_color( FL_RED );
  328. fl_line( x, rulers->y() + rulers->h(), x, rulers->y() + rulers->h() + tracks->h() );
  329. }
  330. void
  331. Timeline::redraw_playhead ( void )
  332. {
  333. redraw_overlay();
  334. }
  335. /** called so many times a second to redraw the playhead etc. */
  336. void
  337. Timeline::update_cb ( void *arg )
  338. {
  339. Fl::repeat_timeout( UPDATE_FREQ, update_cb, arg );
  340. Timeline *tl = (Timeline *)arg;
  341. tl->redraw_playhead();
  342. }
  343. void
  344. Timeline::draw_overlay ( void )
  345. {
  346. draw_playhead();
  347. if ( ! ( _selection.w && _selection.h ) )
  348. return;
  349. fl_push_clip( tracks->x() + Track_Header::width(), rulers->y() + rulers->h(), tracks->w() - Track_Header::width(), h() - rulers->h() - hscroll->h() );
  350. const Rectangle &r = _selection;
  351. fl_color( FL_BLACK );
  352. fl_line_style( FL_SOLID, 2 );
  353. fl_rect( r.x + 2, r.y + 2, r.w, r.h );
  354. fl_color( FL_MAGENTA );
  355. fl_line_style( FL_DASH, 2 );
  356. fl_rect( r.x, r.y, r.w, r.h );
  357. fl_line( r.x, r.y, r.x + r.w, r.y + r.h );
  358. fl_line( r.x + r.w, r.y, r.x, r.y + r.h );
  359. /* fl_overlay_rect( r.x, r.y, r.w, r.h ); */
  360. fl_line_style( FL_SOLID, 0 );
  361. /* const unsigned char data[] = { 127, 127, 127, 96, */
  362. /* 127, 96, 127, 40 }; */
  363. /* Fl_RGB_Image bi( data, 2, 2, 2 ); */
  364. /* Fl_Image *bi2 = bi.copy( r.w, r.h ); */
  365. /* bi2->draw( r.x, r.y ); */
  366. /* delete bi2; */
  367. /* unsigned char *data = fl_read_image( NULL, r.x, r.y, r.w, r.h, 0 ); */
  368. /* Fl_RGB_Image bi( rect_image, r.w, r.h, 3 ); */
  369. /* bi.color_average( FL_BLACK, 0.50f ); */
  370. /* bi.draw( r.x, r.y ); */
  371. /* delete[] rect_image; */
  372. /* rect_image = NULL; */
  373. fl_pop_clip();
  374. }
  375. // #include "Track_Widget.H"
  376. /** select all widgets in inside rectangle /r/ */
  377. void
  378. Timeline::select( const Rectangle &r )
  379. {
  380. const int Y = r.y;
  381. for ( int i = tracks->children(); i-- ; )
  382. {
  383. Track_Header *t = (Track_Header*)tracks->child( i );
  384. if ( ! ( t->y() > Y + r.h || t->y() + t->h() < Y ) )
  385. t->track()->select_range( r.x, r.w );
  386. }
  387. }
  388. int
  389. Timeline::handle ( int m )
  390. {
  391. static Drag *drag = NULL;
  392. switch ( m )
  393. {
  394. case FL_KEYBOARD:
  395. {
  396. switch ( Fl::event_key() )
  397. {
  398. case FL_Delete:
  399. {
  400. Track_Widget::delete_selected();
  401. return 1;
  402. }
  403. }
  404. return 0;
  405. }
  406. default:
  407. {
  408. int r = Fl_Overlay_Window::handle( m );
  409. if ( m != FL_RELEASE && r )
  410. return r;
  411. const int X = Fl::event_x();
  412. const int Y = Fl::event_y();
  413. switch ( m )
  414. {
  415. case FL_PUSH:
  416. {
  417. if ( ! Fl::event_button1() )
  418. return 0;
  419. assert( ! drag );
  420. drag = new Drag( X - x(), Y - y() );
  421. _selection.x = drag->x;
  422. _selection.y = drag->y;
  423. break;
  424. }
  425. case FL_DRAG:
  426. {
  427. int ox = X - drag->x;
  428. int oy = Y - drag->y;
  429. if ( ox < 0 )
  430. _selection.x = X;
  431. if ( oy < 0 )
  432. _selection.y = Y;
  433. _selection.w = abs( ox );
  434. _selection.h = abs( oy );
  435. break;
  436. }
  437. case FL_RELEASE:
  438. {
  439. delete drag;
  440. drag = NULL;
  441. select( _selection );
  442. _selection.w = _selection.h = 0;
  443. break;
  444. }
  445. default:
  446. return 0;
  447. break;
  448. }
  449. redraw_overlay();
  450. return 1;
  451. }
  452. }
  453. }
  454. /**********/
  455. /* Engine */
  456. /**********/
  457. /** call process() on each track header */
  458. nframes_t
  459. Timeline::process ( nframes_t nframes )
  460. {
  461. for ( int i = tracks->children(); i-- ; )
  462. {
  463. Track_Header *t = (Track_Header*)tracks->child( i );
  464. t->process( nframes );
  465. }
  466. /* FIXME: BOGUS */
  467. return nframes;
  468. }
  469. /* THREAD: RT */
  470. void
  471. Timeline::seek ( nframes_t frame )
  472. {
  473. for ( int i = tracks->children(); i-- ; )
  474. {
  475. Track_Header *t = (Track_Header*)tracks->child( i );
  476. t->seek( frame );
  477. }
  478. }
  479. /* THREAD: RT */
  480. int
  481. Timeline::seek_pending ( void )
  482. {
  483. int r = 0;
  484. for ( int i = tracks->children(); i-- ; )
  485. {
  486. Track_Header *t = (Track_Header*)tracks->child( i );
  487. if ( t->diskstream )
  488. r += t->diskstream->output_buffer_percent() < 50;
  489. }
  490. }