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.

380 lines
9.2KB

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