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.

344 lines
8.5KB

  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 "Control_Sequence.H"
  19. #include "Track.H"
  20. #include "Transport.H" // for transport->frame
  21. bool Control_Sequence::draw_with_gradient = true;
  22. bool Control_Sequence::draw_with_polygon = true;
  23. bool Control_Sequence::draw_with_grid = true;
  24. Control_Sequence::Control_Sequence ( Track *track ) : Sequence( 0, 0, 0, 0 )
  25. {
  26. init();
  27. _track = track;
  28. if ( track )
  29. track->add( this );
  30. // output.activate( track->name(),
  31. log_create();
  32. }
  33. Control_Sequence::~Control_Sequence ( )
  34. {
  35. log_destroy();
  36. }
  37. void
  38. Control_Sequence::init ( void )
  39. {
  40. _track = NULL;
  41. _highlighted = false;
  42. color( fl_darker( FL_YELLOW ) );
  43. }
  44. void
  45. Control_Sequence::get ( Log_Entry &e ) const
  46. {
  47. Sequence::get( e );
  48. e.add( ":t", _track );
  49. }
  50. void
  51. Control_Sequence::set ( Log_Entry &e )
  52. {
  53. Sequence::set( e );
  54. for ( int i = 0; i < e.size(); ++i )
  55. {
  56. const char *s, *v;
  57. e.get( i, &s, &v );
  58. if ( ! strcmp( ":t", s ) )
  59. {
  60. int i;
  61. sscanf( v, "%X", &i );
  62. Track *t = (Track*)Loggable::find( i );
  63. assert( t );
  64. t->add( this );
  65. }
  66. }
  67. }
  68. void
  69. Control_Sequence::draw_curve ( bool flip, bool filled )
  70. {
  71. const int bx = x();
  72. const int by = y() + Fl::box_dy( box() );
  73. const int bw = w();
  74. const int bh = h() - Fl::box_dh( box() );
  75. list <Sequence_Widget *>::const_iterator e = _widgets.end();
  76. e--;
  77. if ( _widgets.size() )
  78. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); ; r++ )
  79. {
  80. const int ry = (*r)->y();
  81. if ( r == _widgets.begin() )
  82. {
  83. if ( flip )
  84. {
  85. if ( filled )
  86. fl_vertex( bx, by );
  87. fl_vertex( bx, ry );
  88. }
  89. else
  90. {
  91. if ( filled )
  92. fl_vertex( bx, bh + by );
  93. fl_vertex( bx, ry );
  94. }
  95. }
  96. fl_vertex( (*r)->line_x(), ry );
  97. if ( r == e )
  98. {
  99. if ( flip )
  100. {
  101. fl_vertex( bx + bw, ry );
  102. if ( filled )
  103. fl_vertex( bx + bw, by );
  104. }
  105. else
  106. {
  107. fl_vertex( bx + bw, ry );
  108. if ( filled )
  109. fl_vertex( bx + bw, bh + by );
  110. }
  111. break;
  112. }
  113. }
  114. }
  115. void
  116. Control_Sequence::draw ( void )
  117. {
  118. if ( ! fl_not_clipped( x(), y(), w(), h() ) )
  119. return;
  120. fl_push_clip( x(), y(), w(), h() );
  121. /* draw the box with the ends cut off. */
  122. draw_box( box(), x() - Fl::box_dx( box() ), y(), w() + Fl::box_dw( box() ) + 1, h(), color() );
  123. const int bx = x();
  124. const int by = y() + Fl::box_dy( box() );
  125. const int bw = w();
  126. const int bh = h() - Fl::box_dh( box() );
  127. int X, Y, W, H;
  128. fl_clip_box( bx, by, bw, bh, X, Y, W, H );
  129. bool active = active_r();
  130. const Fl_Color color = active ? this->color() : fl_inactive( this->color() );
  131. const Fl_Color selection_color = active ? this->selection_color() : fl_inactive( this->selection_color() );
  132. if ( draw_with_gradient )
  133. {
  134. /* const Fl_Color c2 = fl_color_average( selection_color, FL_WHITE, 0.90f ); */
  135. /* const Fl_Color c1 = fl_color_average( color, c2, 0.60f ); */
  136. const Fl_Color c1 = fl_color_average( selection_color, FL_WHITE, 0.90f );
  137. const Fl_Color c2 = fl_color_average( color, c1, 0.60f );
  138. for ( int gy = 0; gy < bh; gy++ )
  139. {
  140. fl_color( fl_color_average( c1, c2, gy / (float)bh) );
  141. fl_line( X, by + gy, X + W, by + gy );
  142. }
  143. }
  144. if ( draw_with_grid )
  145. {
  146. fl_color( fl_darker( color ) );
  147. const int inc = bh / 10;
  148. if ( inc )
  149. for ( int gy = 0; gy < bh; gy += inc )
  150. fl_line( X, by + gy, X + W, by + gy );
  151. }
  152. if ( draw_with_polygon )
  153. {
  154. fl_color( draw_with_gradient ? color : fl_color_average( color, selection_color, 0.45f ) );
  155. fl_begin_complex_polygon();
  156. draw_curve( draw_with_gradient, true );
  157. fl_end_complex_polygon();
  158. fl_color( selection_color );
  159. fl_line_style( FL_SOLID, 2 );
  160. fl_begin_line();
  161. draw_curve( draw_with_gradient, false );
  162. fl_end_line();
  163. }
  164. else
  165. {
  166. // fl_color( fl_color_average( selection_color, color, 0.70f ) );
  167. fl_color( selection_color );
  168. fl_line_style( FL_SOLID, 2 );
  169. fl_begin_line();
  170. draw_curve( draw_with_gradient, false );
  171. fl_end_line();
  172. }
  173. fl_line_style( FL_SOLID, 0 );
  174. timeline->draw_measure_lines( x(), y(), w(), h(), color );
  175. if ( _highlighted )
  176. for ( list <Sequence_Widget *>::const_iterator r = _widgets.begin(); r != _widgets.end(); r++ )
  177. (*r)->draw_box();
  178. fl_pop_clip();
  179. }
  180. int
  181. Control_Sequence::handle ( int m )
  182. {
  183. switch ( m )
  184. {
  185. case FL_ENTER:
  186. _highlighted = true;
  187. redraw();
  188. return 1;
  189. case FL_LEAVE:
  190. _highlighted = false;
  191. redraw();
  192. return 1;
  193. default:
  194. break;
  195. }
  196. int r = Sequence::handle( m );
  197. if ( r )
  198. return r;
  199. switch ( m )
  200. {
  201. case FL_PUSH:
  202. {
  203. if ( Fl::event_button1() )
  204. {
  205. Control_Point *r = new Control_Point( this, timeline->xoffset + timeline->x_to_ts( Fl::event_x() - x() ), (float)(Fl::event_y() - y()) / h() );
  206. add( r );
  207. }
  208. return 1;
  209. }
  210. default:
  211. return 0;
  212. }
  213. }
  214. /**********/
  215. /* Engine */
  216. /**********/
  217. static inline float
  218. linear_interpolate ( float y1, float y2, float mu )
  219. {
  220. return y1 * (1 - mu) + y2 * mu;
  221. }
  222. static inline float
  223. sigmoid_interpolate ( float y1, float y2, float mu )
  224. {
  225. return linear_interpolate( y1, y2, ( 1 - cos( mu * M_PI ) ) / 2 );
  226. }
  227. /* static inline float */
  228. /* exponential_interpolate ( float y1, float y2, float mu ) */
  229. /* { */
  230. /* } */
  231. /* THREAD: ?? */
  232. /** fill buf with /nframes/ of interpolated control curve values
  233. * starting at /frame/ */
  234. nframes_t
  235. Control_Sequence::play ( sample_t *buf, nframes_t frame, nframes_t nframes )
  236. {
  237. Control_Point *p2, *p1 = (Control_Point*)&_widgets.front();
  238. nframes_t n = nframes;
  239. for ( list <Sequence_Widget *>::const_iterator i = _widgets.begin();
  240. i != _widgets.end(); ++i, p1 = p2 )
  241. {
  242. p2 = (Control_Point*)(*i);
  243. if ( p2->when() < frame )
  244. continue;
  245. nframes_t d = p2->when() - p1->when();
  246. for ( nframes_t i = frame - p1->when(); i < d; ++i )
  247. {
  248. *(buf++) = 1.0f - ( 2 * sigmoid_interpolate( p1->control(), p2->control(), i / (float)d ) );
  249. if ( ! n-- )
  250. return nframes;
  251. frame++;
  252. }
  253. }
  254. return frame;
  255. }
  256. /* THREAD: RT */
  257. nframes_t
  258. Control_Sequence::process ( nframes_t nframes )
  259. {
  260. void *buf = _output->buffer( nframes );
  261. return play( (sample_t*)buf, transport->frame, nframes );
  262. }