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.

342 lines
8.4KB

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