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.

311 lines
7.3KB

  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. control_output[0].connect_to( &module->control_input[port] );
  66. }
  67. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  68. {
  69. // label( "" );
  70. box( FL_NO_BOX );
  71. _pad = true;
  72. control = 0;
  73. control_value =0.0f;
  74. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  75. mode( GUI );
  76. // mode( CV );
  77. // configure_inputs( 1 );
  78. end();
  79. Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  80. log_create();
  81. }
  82. Controller_Module::~Controller_Module ( )
  83. {
  84. Fl::remove_timeout( update_cb, this );
  85. log_destroy();
  86. }
  87. void
  88. Controller_Module::update_cb ( void *v )
  89. {
  90. ((Controller_Module*)v)->update_cb();
  91. }
  92. void
  93. Controller_Module::update_cb ( void )
  94. {
  95. Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  96. if ( control && control_output[0].connected() )
  97. control->value(control_value);
  98. }
  99. void
  100. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  101. {
  102. ((Controller_Module*)v)->cb_handle( w );
  103. }
  104. void
  105. Controller_Module::cb_handle ( Fl_Widget *w )
  106. {
  107. control_value = ((Fl_Valuator*)w)->value();
  108. if ( control_output[0].connected() )
  109. {
  110. control_output[0].control_value( control_value );
  111. Port *p = control_output[0].connected_port();
  112. Module *m = p->module();
  113. m->handle_control_changed( p );
  114. }
  115. }
  116. void
  117. Controller_Module::connect_to ( Port *p )
  118. {
  119. control_output[0].connect_to( p );
  120. if( mode() == CV )
  121. {
  122. engine->lock();
  123. {
  124. char name[256];
  125. snprintf( name, sizeof( name ), "%s-CV", p->name() );
  126. JACK::Port po( engine->client(), JACK::Port::Input, chain()->name(), 0, name );
  127. if ( po.valid() )
  128. {
  129. jack_input.push_back( po );
  130. }
  131. }
  132. engine->unlock();
  133. }
  134. Fl_Widget *w;
  135. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  136. {
  137. Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
  138. w = o;
  139. o->value( p->control_value() );
  140. }
  141. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  142. {
  143. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  144. control = o;
  145. w = o;
  146. o->type(1);
  147. o->step(1);
  148. if ( p->hints.ranged )
  149. {
  150. o->minimum( p->hints.minimum );
  151. o->maximum( p->hints.maximum );
  152. }
  153. o->value( p->control_value() );
  154. }
  155. else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  156. {
  157. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  158. control = o;
  159. w = o;
  160. o->type(4);
  161. o->color(FL_GRAY0);
  162. o->selection_color((Fl_Color)1);
  163. o->minimum(1.5);
  164. o->maximum(0);
  165. o->value(1);
  166. o->textsize(14);
  167. // o->type( FL_VERTICAL );
  168. // o->type(1);
  169. if ( p->hints.ranged )
  170. {
  171. o->minimum( p->hints.maximum );
  172. o->maximum( p->hints.minimum );
  173. }
  174. o->value( p->control_value() );
  175. }
  176. else
  177. {
  178. { Fl_Arc_Dial *o = new Fl_Arc_Dial( 0, 0, 40, 40, p->name() );
  179. w = o;
  180. control = o;
  181. if ( p->hints.ranged )
  182. {
  183. o->minimum( p->hints.minimum );
  184. o->maximum( p->hints.maximum );
  185. }
  186. o->box( FL_BURNISHED_OVAL_BOX );
  187. // o->box( FL_OVAL_BOX );
  188. // o->type( FL_FILL_DIAL );
  189. o->color( fl_darker( fl_darker( FL_GRAY ) ) );
  190. o->selection_color( FL_WHITE );
  191. o->value( p->control_value() );
  192. }
  193. }
  194. control_value = p->control_value();
  195. w->align(FL_ALIGN_TOP);
  196. w->labelsize( 10 );
  197. w->callback( cb_handle, this );
  198. if ( _pad )
  199. {
  200. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  201. size( flg->w(), flg->h() );
  202. add( flg );
  203. }
  204. else
  205. {
  206. w->resize( x(), y(), this->w(), h() );
  207. add( w );
  208. resizable( w );
  209. }
  210. }
  211. int
  212. Controller_Module::handle ( int m )
  213. {
  214. return Fl_Group::handle( m );
  215. }
  216. void
  217. Controller_Module::process ( void )
  218. {
  219. if ( control_output[0].connected() )
  220. {
  221. float f = control_value;
  222. if ( mode() == CV )
  223. {
  224. f = *((float*)jack_input[0].buffer( engine->nframes() ));
  225. const Port *p = control_output[0].connected_port();
  226. if (p->hints.ranged )
  227. {
  228. // scale value to range.
  229. // we assume that CV values are between 0 and 1
  230. float scale = p->hints.maximum - p->hints.minimum;
  231. float offset = p->hints.minimum;
  232. f = ( f * scale ) + offset;
  233. }
  234. }
  235. // else
  236. // f = *((float*)control_output[0].buffer());
  237. *((float*)control_output[0].buffer()) = f;
  238. control_value = f;
  239. }
  240. }