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.

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