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