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.

394 lines
10KB

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