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.

390 lines
10.0KB

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