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.

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