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.

328 lines
7.7KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2009 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 "Controller_Module.H"
  19. #include <FL/Fl.H>
  20. #include "FL/Fl_Value_SliderX.H"
  21. #include <FL/Fl_Box.H>
  22. #include <FL/Fl_Counter.H>
  23. #include "FL/Fl_Arc_Dial.H"
  24. #include "FL/Fl_Light_Button.H"
  25. #include "FL/Boxtypes.H"
  26. #include <FL/fl_draw.H>
  27. #include "FL/Fl_Labelpad_Group.H"
  28. #include <stdio.h>
  29. #include "Engine/Engine.H"
  30. #include "Chain.H"
  31. const float CONTROL_UPDATE_FREQ = 0.1f;
  32. void
  33. Controller_Module::get ( Log_Entry &e ) const
  34. {
  35. Port *p = control_output[0].connected_port();
  36. Module *m = p->module();
  37. e.add( ":module", m );
  38. e.add( ":port", m->control_input_port_index( p ) );
  39. Module::get( e );
  40. }
  41. void
  42. Controller_Module::set ( Log_Entry &e )
  43. {
  44. Module::set( e );
  45. int port = -1;
  46. Module *module = NULL;
  47. for ( int i = 0; i < e.size(); ++i )
  48. {
  49. const char *s, *v;
  50. e.get( i, &s, &v );
  51. if ( ! strcmp( s, ":port" ) )
  52. {
  53. port = atoi( v );
  54. }
  55. else if ( ! strcmp( s, ":module" ) )
  56. {
  57. int i;
  58. sscanf( v, "%X", &i );
  59. Module *t = (Module*)Loggable::find( i );
  60. assert( t );
  61. module = t;
  62. }
  63. }
  64. if ( port >= 0 && module )
  65. {
  66. control_output[0].connect_to( &module->control_input[port] );
  67. module->chain()->add_control( this );
  68. label( module->control_input[port].name() );
  69. }
  70. }
  71. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  72. {
  73. // label( "" );
  74. box( FL_NO_BOX );
  75. _pad = true;
  76. control = 0;
  77. control_value =0.0f;
  78. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  79. mode( GUI );
  80. // mode( CV );
  81. // configure_inputs( 1 );
  82. end();
  83. Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  84. log_create();
  85. }
  86. Controller_Module::~Controller_Module ( )
  87. {
  88. Fl::remove_timeout( update_cb, this );
  89. log_destroy();
  90. }
  91. void
  92. Controller_Module::update_cb ( void *v )
  93. {
  94. ((Controller_Module*)v)->update_cb();
  95. }
  96. void
  97. Controller_Module::update_cb ( void )
  98. {
  99. Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  100. if ( control && control_output[0].connected() )
  101. control->value(control_value);
  102. }
  103. void
  104. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  105. {
  106. ((Controller_Module*)v)->cb_handle( w );
  107. }
  108. void
  109. Controller_Module::cb_handle ( Fl_Widget *w )
  110. {
  111. control_value = ((Fl_Valuator*)w)->value();
  112. if ( control_output[0].connected() )
  113. {
  114. control_output[0].control_value( control_value );
  115. Port *p = control_output[0].connected_port();
  116. Module *m = p->module();
  117. m->handle_control_changed( p );
  118. }
  119. }
  120. void
  121. Controller_Module::connect_to ( Port *p )
  122. {
  123. control_output[0].connect_to( p );
  124. if( mode() == CV )
  125. {
  126. chain()->engine()->lock();
  127. char name[256];
  128. snprintf( name, sizeof( name ), "%s-CV", p->name() );
  129. JACK::Port po( chain()->engine()->client(), JACK::Port::Input, chain()->name(), 0, name );
  130. if ( po.valid() )
  131. {
  132. jack_input.push_back( po );
  133. }
  134. chain()->engine()->unlock();
  135. }
  136. Fl_Widget *w;
  137. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  138. {
  139. Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
  140. w = o;
  141. o->value( p->control_value() );
  142. }
  143. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  144. {
  145. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  146. control = o;
  147. w = o;
  148. o->type(1);
  149. o->step(1);
  150. if ( p->hints.ranged )
  151. {
  152. o->minimum( p->hints.minimum );
  153. o->maximum( p->hints.maximum );
  154. }
  155. o->value( p->control_value() );
  156. }
  157. else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  158. {
  159. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  160. control = o;
  161. w = o;
  162. o->type(4);
  163. o->color(FL_GRAY0);
  164. o->selection_color((Fl_Color)1);
  165. o->minimum(1.5);
  166. o->maximum(0);
  167. o->value(1);
  168. o->textsize(14);
  169. // o->type( FL_VERTICAL );
  170. // o->type(1);
  171. if ( p->hints.ranged )
  172. {
  173. o->minimum( p->hints.maximum );
  174. o->maximum( p->hints.minimum );
  175. }
  176. o->value( p->control_value() );
  177. }
  178. else
  179. {
  180. { Fl_Arc_Dial *o = new Fl_Arc_Dial( 0, 0, 40, 40, p->name() );
  181. w = o;
  182. control = o;
  183. if ( p->hints.ranged )
  184. {
  185. o->minimum( p->hints.minimum );
  186. o->maximum( p->hints.maximum );
  187. }
  188. o->box( FL_BURNISHED_OVAL_BOX );
  189. // o->box( FL_OVAL_BOX );
  190. // o->type( FL_FILL_DIAL );
  191. o->color( fl_darker( fl_darker( FL_GRAY ) ) );
  192. o->selection_color( FL_WHITE );
  193. o->value( p->control_value() );
  194. }
  195. }
  196. control_value = p->control_value();
  197. w->align(FL_ALIGN_TOP);
  198. w->labelsize( 10 );
  199. w->callback( cb_handle, this );
  200. if ( _pad )
  201. {
  202. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  203. size( flg->w(), flg->h() );
  204. add( flg );
  205. }
  206. else
  207. {
  208. w->resize( x(), y(), this->w(), h() );
  209. add( w );
  210. resizable( w );
  211. }
  212. }
  213. void
  214. Controller_Module::resize ( int X, int Y, int W, int H )
  215. {
  216. Module::resize( X, Y, W, H );
  217. if ( ! _pad && children() )
  218. {
  219. child( 0 )->resize( X, Y, W, H );
  220. }
  221. }
  222. int
  223. Controller_Module::handle ( int m )
  224. {
  225. return Fl_Group::handle( m );
  226. }
  227. void
  228. Controller_Module::process ( void )
  229. {
  230. THREAD_ASSERT( RT );
  231. if ( control_output[0].connected() )
  232. {
  233. float f = control_value;
  234. if ( mode() == CV )
  235. {
  236. f = *((float*)jack_input[0].buffer( chain()->engine()->nframes() ));
  237. const Port *p = control_output[0].connected_port();
  238. if (p->hints.ranged )
  239. {
  240. // scale value to range.
  241. // we assume that CV values are between 0 and 1
  242. float scale = p->hints.maximum - p->hints.minimum;
  243. float offset = p->hints.minimum;
  244. f = ( f * scale ) + offset;
  245. }
  246. }
  247. // else
  248. // f = *((float*)control_output[0].buffer());
  249. *((float*)control_output[0].buffer()) = f;
  250. control_value = f;
  251. }
  252. }