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.

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