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.

434 lines
11KB

  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 <FL/Fl_Scrollbar.H>
  23. #include "Track_Header.H"
  24. void
  25. cb_hscroll ( Fl_Widget *w, void *v )
  26. {
  27. Scalebar *sb = (Scalebar*)w;
  28. if ( sb->zoom_changed() )
  29. {
  30. timeline->fpp = sb->zoom() * 1;
  31. int maxx = timeline->ts_to_x( timeline->length );
  32. sb->range( 0, maxx );
  33. timeline->redraw();
  34. }
  35. else
  36. {
  37. timeline->position( sb->value() );
  38. }
  39. printf( "%lu\n", timeline->xoffset );
  40. }
  41. void
  42. cb_vscroll ( Fl_Widget *w, void *v )
  43. {
  44. Fl_Scrollbar *sb = (Fl_Scrollbar*)w;
  45. timeline->tracks->position( timeline->tracks->x(), (timeline->rulers->y() + timeline->rulers->h()) - sb->value() );
  46. timeline->yposition = sb->value();
  47. // timeline->vscroll->range( 0, timeline->tracks->h() - timeline->h() - timeline->rulers->h() );
  48. sb->value( sb->value(), 30, 0, min( timeline->tracks->h(), timeline->tracks->h() - timeline->h() - timeline->rulers->h() ) );
  49. timeline->damage( FL_DAMAGE_SCROLL );
  50. }
  51. Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : Fl_Overlay_Window( X, Y, W, H, L )
  52. {
  53. box( FL_FLAT_BOX );
  54. xoffset = 0;
  55. _enable_measure_lines = true;
  56. X = Y = 0;
  57. {
  58. Scalebar *o = new Scalebar( X, Y + H - 18, W - 18, 18 );
  59. o->range( 0, 48000 * 2 );
  60. o->zoom_range( 2, 8192 );
  61. o->zoom( 256 );
  62. o->type( FL_HORIZONTAL );
  63. o->callback( cb_hscroll, 0 );
  64. hscroll = o;
  65. }
  66. {
  67. Fl_Scrollbar *o = new Fl_Scrollbar( X + W - 18, Y, 18, H - 18 );
  68. o->type( FL_VERTICAL );
  69. // o->step( 10 );
  70. o->callback( cb_vscroll, 0 );
  71. vscroll = o;
  72. }
  73. {
  74. Fl_Pack *o = new Fl_Pack( X + Track_Header::width(), Y, (W - Track_Header::width()) - vscroll->w(), H - hscroll->h(), "rulers" );
  75. o->type( Fl_Pack::VERTICAL );
  76. {
  77. Tempo_Track *o = new Tempo_Track( 0, 0, 800, 24 );
  78. o->color( FL_RED );
  79. o->add( new Tempo_Point( 0, 120 ) );
  80. o->add( new Tempo_Point( 56000, 250 ) );
  81. o->label( "Tempo" );
  82. o->align( FL_ALIGN_LEFT );
  83. tempo_track = o;
  84. o->end();
  85. }
  86. {
  87. Time_Track *o = new Time_Track( 0, 24, 800, 24 );
  88. o->color( fl_color_average( FL_RED, FL_WHITE, 0.50f ) );
  89. o->add( new Time_Point( 0, 4, 4 ) );
  90. o->add( new Time_Point( 345344, 6, 8 ) );
  91. o->label( "Time" );
  92. o->align( FL_ALIGN_LEFT );
  93. time_track = o;
  94. o->end();
  95. }
  96. o->size( o->w(), o->child( 0 )->h() * o->children() );
  97. rulers = o;
  98. o->end();
  99. }
  100. {
  101. /* Fl_Scroll *o = new Fl_Scroll( 0, 24 * 2, 800, 600 - (24 * 3) ); */
  102. /* o->type( Fl_Scroll::VERTICAL_ALWAYS ); */
  103. sample_rate = 44100;
  104. fpp = 256;
  105. _beats_per_minute = 120;
  106. length = sample_rate * 60 * 2;
  107. {
  108. Fl_Pack *o = new Fl_Pack( X, rulers->y() + rulers->h(), W - vscroll->w(), 5000 );
  109. o->type( Fl_Pack::VERTICAL );
  110. o->spacing( 5 );
  111. Track *l = NULL;
  112. for ( int i = 16; i--; )
  113. {
  114. // Track_Header *t = new Track_Header( 0, 0, W, 75 );
  115. Track_Header *t = new Track_Header( 0, 0, W, 30 );
  116. Track *o = new Audio_Track( 0, 0, 1, 100 );
  117. o->prev( l );
  118. if ( l )
  119. l->next( o );
  120. l = o;
  121. // o->end();
  122. t->track( o );
  123. t->color( (Fl_Color)rand() );
  124. }
  125. tracks = o;
  126. o->end();
  127. }
  128. /* scroll = o; */
  129. /* o->end(); */
  130. }
  131. /* make sure scrollbars are on top */
  132. add( vscroll );
  133. add( hscroll );
  134. vscroll->range( 0, tracks->h() );
  135. redraw();
  136. end();
  137. }
  138. float
  139. Timeline::beats_per_minute ( nframes_t when ) const
  140. {
  141. return tempo_track->beats_per_minute( when );
  142. }
  143. int
  144. Timeline::beats_per_bar ( nframes_t when ) const
  145. {
  146. time_sig t = time_track->time( when );
  147. return t.beats_per_bar;
  148. }
  149. void
  150. Timeline::beats_per_minute ( nframes_t when, float bpm )
  151. {
  152. tempo_track->add( new Tempo_Point( when, bpm ) );
  153. }
  154. /** return the absolute pixel of the nearest measure line to /x/ */
  155. int
  156. Timeline::nearest_line ( int ix )
  157. {
  158. for ( int x = ix - 10; x < ix + 10; ++x )
  159. {
  160. const int measure = ts_to_x( (double)(sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  161. // const int abs_x = ts_to_x( xoffset ) + x;
  162. if ( 0 == x % measure )
  163. return x;
  164. }
  165. return -1;
  166. }
  167. /** draw appropriate measure lines inside the given bounding box */
  168. /* FIXME: this function *really* needs to be optimized. Currently it
  169. searched both the time and tempo lists once for every horiontal
  170. pixel and performs a number of calculations--this is slow. */
  171. void
  172. Timeline::draw_measure_lines ( int X, int Y, int W, int H, Fl_Color color )
  173. {
  174. if ( ! _enable_measure_lines )
  175. return;
  176. fl_line_style( FL_DASH, 2 );
  177. Fl_Color beat = fl_color_average( FL_BLACK, color, 0.65f );
  178. Fl_Color bar = fl_color_average( FL_RED, color, 0.65f );
  179. int measure;
  180. for ( int x = X; x < X + W; ++x )
  181. {
  182. measure = ts_to_x( (double)(sample_rate * 60) / beats_per_minute( x_to_ts( x - Track_Header::width() ) + xoffset ));
  183. const int abs_x = ts_to_x( xoffset ) + x - Track_Header::width();
  184. if ( 0 == abs_x % measure )
  185. {
  186. int bpb = beats_per_bar( x_to_ts( x -Track_Header::width() ) + xoffset );
  187. if ( 0 == (abs_x / measure) % bpb )
  188. {
  189. if ( measure * bpb < 8 )
  190. break;
  191. fl_color( bar );
  192. }
  193. else
  194. {
  195. if ( measure < 8 )
  196. continue;
  197. fl_color( beat );
  198. }
  199. fl_line( x, Y, x, Y + H );
  200. }
  201. }
  202. fl_line_style( FL_SOLID, 0 );
  203. }
  204. void
  205. Timeline::position ( int X )
  206. {
  207. _old_xposition = xoffset;
  208. xoffset = x_to_ts( X );
  209. damage( FL_DAMAGE_SCROLL );
  210. }
  211. void
  212. Timeline::draw_clip ( void * v, int X, int Y, int W, int H )
  213. {
  214. Timeline *tl = (Timeline *)v;
  215. // printf( "draw_clip: %d,%d %dx%d\n", X, Y, W, H );
  216. fl_push_clip( X, Y, W, H );
  217. fl_color( rand() );
  218. fl_rectf( X, Y, X + W, Y + H );
  219. tl->draw_child( *tl->rulers );
  220. /* headers */
  221. fl_push_clip( tl->tracks->x(), tl->rulers->y() + tl->rulers->h(), Track_Header::width(), tl->h() - tl->rulers->h() - tl->hscroll->h() );
  222. tl->draw_child( *tl->tracks );
  223. fl_pop_clip();
  224. /* track bodies */
  225. 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() );
  226. tl->draw_child( *tl->tracks );
  227. fl_pop_clip();
  228. // tl->draw_child( *tl->tracks );
  229. fl_pop_clip();
  230. }
  231. void
  232. Timeline::draw ( void )
  233. {
  234. int X, Y, W, H;
  235. X = tracks->x() + Fl::box_dx( tracks->child( 0 )->box() ) + 1;
  236. Y = tracks->y();
  237. W = tracks->w() - Fl::box_dw( tracks->child( 0 )->box() ) - 1;
  238. H = tracks->h();
  239. if ( damage() & FL_DAMAGE_ALL )
  240. {
  241. draw_box( box(), 0, 0, w(), h(), color() );
  242. fl_push_clip( 0, rulers->y(), w(), rulers->h() );
  243. draw_child( *rulers );
  244. fl_pop_clip();
  245. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), h() - hscroll->h() );
  246. draw_child( *tracks );
  247. fl_pop_clip();
  248. draw_child( *hscroll );
  249. draw_child( *vscroll );
  250. redraw_overlay();
  251. return;
  252. }
  253. if ( damage() & FL_DAMAGE_CHILD )
  254. {
  255. fl_push_clip( rulers->x(), rulers->y(), rulers->w() - vscroll->w(), rulers->h() );
  256. update_child( *rulers );
  257. fl_pop_clip();
  258. fl_push_clip( tracks->x(), rulers->y() + rulers->h(), tracks->w(), hscroll->y() - (rulers->y() + rulers->h()) );
  259. update_child( *tracks );
  260. fl_pop_clip();
  261. update_child( *hscroll );
  262. update_child( *vscroll );
  263. }
  264. if ( damage() & FL_DAMAGE_SCROLL )
  265. {
  266. int dx = ts_to_x( _old_xposition ) - ts_to_x( xoffset );
  267. int dy = _old_yposition - yposition;
  268. if ( ! dy )
  269. fl_scroll( X + Track_Header::width(), rulers->y(), rulers->w() - Fl::box_dw( rulers->child(0)->box() ), rulers->h(), dx, 0, draw_clip, this );
  270. Y = rulers->y() + rulers->h();
  271. H = h() - rulers->h() - hscroll->h();
  272. if ( dy == 0 )
  273. fl_scroll( X + Track_Header::width(), Y, W - Track_Header::width(), H, dx, dy, draw_clip, this );
  274. else
  275. fl_scroll( X, Y, W, H, dx, dy, draw_clip, this );
  276. _old_xposition = xoffset;
  277. _old_yposition = yposition;
  278. }
  279. }
  280. void
  281. Timeline::draw_overlay ( void )
  282. {
  283. // TODO: draw selection rectangle here!
  284. /* fl_color( FL_BLUE ); */
  285. /* fl_line_style( FL_DOT, 4 ); */
  286. /* fl_rect( 300, 400, 200, 300 ); */
  287. /* fl_line_style( FL_SOLID, 0 ); */
  288. }
  289. int
  290. Timeline::handle ( int m )
  291. {
  292. switch ( m )
  293. {
  294. /* case FL_MOUSEWHEEL: */
  295. /* { */
  296. /* // vscroll->deactivate(); */
  297. /* int r = Fl_Overlay_Window::handle( m ); */
  298. /* /\* vscroll->activate(); *\/ */
  299. /* if ( r ) */
  300. /* return r; */
  301. /* /\* if ( hscroll->handle( m ) ) *\/ */
  302. /* /\* return 1; *\/ */
  303. /* /\* return vscroll->handle( m ); *\/ */
  304. /* } */
  305. default:
  306. return Fl_Overlay_Window::handle( m );
  307. }
  308. }