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.

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