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