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.

430 lines
9.9KB

  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_Menu_Item.H>
  25. #include <FL/Fl_Menu_Button.H>
  26. #include <FL/Fl_Menu_.H>
  27. #include "FL/Fl_Light_Button.H"
  28. #include "FL/Boxtypes.H"
  29. #include <FL/fl_draw.H>
  30. #include "FL/Fl_Labelpad_Group.H"
  31. #include <stdio.h>
  32. #include "Engine/Engine.H"
  33. #include "Chain.H"
  34. const float CONTROL_UPDATE_FREQ = 0.1f;
  35. void
  36. Controller_Module::get ( Log_Entry &e ) const
  37. {
  38. Module::get( e );
  39. Port *p = control_output[0].connected_port();
  40. Module *m = p->module();
  41. e.add( ":module", m );
  42. e.add( ":port", m->control_input_port_index( p ) );
  43. e.add( ":mode", mode() );
  44. }
  45. void
  46. Controller_Module::set ( Log_Entry &e )
  47. {
  48. Module::set( e );
  49. int port = -1;
  50. Module *module = NULL;
  51. for ( int i = 0; i < e.size(); ++i )
  52. {
  53. const char *s, *v;
  54. e.get( i, &s, &v );
  55. if ( ! strcmp( s, ":port" ) )
  56. {
  57. port = atoi( v );
  58. }
  59. else if ( ! strcmp( s, ":module" ) )
  60. {
  61. int i;
  62. sscanf( v, "%X", &i );
  63. Module *t = (Module*)Loggable::find( i );
  64. assert( t );
  65. module = t;
  66. }
  67. }
  68. if ( port >= 0 && module )
  69. {
  70. connect_to( &module->control_input[port] );
  71. module->chain()->add_control( this );
  72. label( module->control_input[port].name() );
  73. }
  74. for ( int i = 0; i < e.size(); ++i )
  75. {
  76. const char *s, *v;
  77. e.get( i, &s, &v );
  78. if ( ! strcmp( s, ":mode" ) )
  79. {
  80. mode( (Mode)atoi( v ) );
  81. }
  82. }
  83. }
  84. Controller_Module::Controller_Module ( bool is_default ) : Module( is_default, 50, 100, name() )
  85. {
  86. // label( "" );
  87. box( FL_NO_BOX );
  88. _pad = true;
  89. control = 0;
  90. control_value =0.0f;
  91. add_port( Port( this, Port::OUTPUT, Port::CONTROL ) );
  92. mode( GUI );
  93. // mode( CV );
  94. // configure_inputs( 1 );
  95. end();
  96. Fl::add_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  97. log_create();
  98. }
  99. Controller_Module::~Controller_Module ( )
  100. {
  101. Fl::remove_timeout( update_cb, this );
  102. log_destroy();
  103. }
  104. void
  105. Controller_Module::update_cb ( void *v )
  106. {
  107. ((Controller_Module*)v)->update_cb();
  108. }
  109. void
  110. Controller_Module::update_cb ( void )
  111. {
  112. Fl::repeat_timeout( CONTROL_UPDATE_FREQ, update_cb, this );
  113. if ( control && control_output[0].connected() )
  114. control->value(control_value);
  115. }
  116. void
  117. Controller_Module::cb_handle ( Fl_Widget *w, void *v )
  118. {
  119. ((Controller_Module*)v)->cb_handle( w );
  120. }
  121. void
  122. Controller_Module::cb_handle ( Fl_Widget *w )
  123. {
  124. control_value = ((Fl_Valuator*)w)->value();
  125. if ( control_output[0].connected() )
  126. {
  127. control_output[0].control_value( control_value );
  128. Port *p = control_output[0].connected_port();
  129. Module *m = p->module();
  130. m->handle_control_changed( p );
  131. }
  132. }
  133. void
  134. Controller_Module::connect_to ( Port *p )
  135. {
  136. control_output[0].connect_to( p );
  137. Fl_Widget *w;
  138. if ( p->hints.type == Module::Port::Hints::BOOLEAN )
  139. {
  140. Fl_Light_Button *o = new Fl_Light_Button( 0, 0, 40, 40, p->name() );
  141. w = o;
  142. o->value( p->control_value() );
  143. }
  144. else if ( p->hints.type == Module::Port::Hints::INTEGER )
  145. {
  146. Fl_Counter *o = new Fl_Counter(0, 0, 58, 24, p->name() );
  147. control = o;
  148. w = o;
  149. o->type(1);
  150. o->step(1);
  151. if ( p->hints.ranged )
  152. {
  153. o->minimum( p->hints.minimum );
  154. o->maximum( p->hints.maximum );
  155. }
  156. o->value( p->control_value() );
  157. }
  158. else if ( p->hints.type == Module::Port::Hints::LOGARITHMIC )
  159. {
  160. Fl_Value_SliderX *o = new Fl_Value_SliderX(0, 0, 30, 250, p->name() );
  161. control = o;
  162. w = o;
  163. o->type(4);
  164. o->color(FL_GRAY0);
  165. o->selection_color((Fl_Color)1);
  166. o->minimum(1.5);
  167. o->maximum(0);
  168. o->value(1);
  169. o->textsize(14);
  170. // o->type( FL_VERTICAL );
  171. // o->type(1);
  172. if ( p->hints.ranged )
  173. {
  174. o->minimum( p->hints.maximum );
  175. o->maximum( p->hints.minimum );
  176. }
  177. o->value( p->control_value() );
  178. }
  179. else
  180. {
  181. { Fl_Arc_Dial *o = new Fl_Arc_Dial( 0, 0, 40, 40, p->name() );
  182. w = o;
  183. control = o;
  184. if ( p->hints.ranged )
  185. {
  186. o->minimum( p->hints.minimum );
  187. o->maximum( p->hints.maximum );
  188. }
  189. o->box( FL_BURNISHED_OVAL_BOX );
  190. // o->box( FL_OVAL_BOX );
  191. // o->type( FL_FILL_DIAL );
  192. o->color( fl_darker( fl_darker( FL_GRAY ) ) );
  193. o->selection_color( FL_WHITE );
  194. o->value( p->control_value() );
  195. }
  196. }
  197. control_value = p->control_value();
  198. w->align(FL_ALIGN_TOP);
  199. w->labelsize( 10 );
  200. w->callback( cb_handle, this );
  201. if ( _pad )
  202. {
  203. Fl_Labelpad_Group *flg = new Fl_Labelpad_Group( w );
  204. size( flg->w(), flg->h() );
  205. add( flg );
  206. }
  207. else
  208. {
  209. w->resize( x(), y(), this->w(), h() );
  210. add( w );
  211. resizable( w );
  212. }
  213. }
  214. void
  215. Controller_Module::resize ( int X, int Y, int W, int H )
  216. {
  217. Module::resize( X, Y, W, H );
  218. if ( ! _pad && children() )
  219. {
  220. child( 0 )->resize( X, Y, W, H );
  221. }
  222. }
  223. void
  224. Controller_Module::menu_cb ( Fl_Widget *w, void *v )
  225. {
  226. ((Controller_Module*)v)->menu_cb( (Fl_Menu_*) w );
  227. }
  228. void
  229. Controller_Module::menu_cb ( const Fl_Menu_ *m )
  230. {
  231. char picked[256];
  232. m->item_pathname( picked, sizeof( picked ) );
  233. Logger log( this );
  234. if ( ! strcmp( picked, "Mode/Manual" ) )
  235. mode( GUI );
  236. else if ( ! strcmp( picked, "Mode/Control Voltage" ) )
  237. mode( CV );
  238. }
  239. #include "FL/test_press.H"
  240. #include "FL/menu_popup.H"
  241. /** build the context menu for this control */
  242. Fl_Menu_Button &
  243. Controller_Module::menu ( void )
  244. {
  245. static Fl_Menu_Button m( 0, 0, 0, 0, "Controller" );
  246. Fl_Menu_Item items[] =
  247. {
  248. { "Mode", 0, 0, 0, FL_SUBMENU },
  249. { "Manual", 0, 0, 0, FL_MENU_RADIO | ( mode() == GUI ? FL_MENU_VALUE : 0 ) },
  250. { "Control Voltage", 0, 0, 0, FL_MENU_RADIO | ( mode() == CV ? FL_MENU_VALUE : 0 ) },
  251. // { "Open Sound Control (OSC)", 0, 0, 0, FL_MENU_RADIO | ( mode() == OSC ? FL_MENU_VALUE : 0 ) },
  252. { 0 },
  253. { 0 },
  254. };
  255. menu_set_callback( items, &Controller_Module::menu_cb, (void*)this );
  256. m.copy( items, (void*)this );
  257. return m;
  258. }
  259. int
  260. Controller_Module::handle ( int m )
  261. {
  262. switch ( m )
  263. {
  264. case FL_PUSH:
  265. {
  266. if ( test_press( FL_BUTTON3 ) )
  267. {
  268. /* context menu */
  269. menu_popup( &menu() );
  270. return 1;
  271. }
  272. else
  273. return Fl_Group::handle( m );
  274. }
  275. }
  276. return Fl_Group::handle( m );
  277. }
  278. void
  279. Controller_Module::mode ( Mode m )
  280. {
  281. if( mode() != CV && m == CV )
  282. {
  283. if ( control_output[0].connected() )
  284. {
  285. chain()->engine()->lock();
  286. // char name[256];
  287. // snprintf( name, sizeof( name ), "%s-CV", p->name() );
  288. Port *p = control_output[0].connected_port();
  289. JACK::Port po( chain()->engine(), JACK::Port::Input, p->name(), 0, "CV" );
  290. if ( po.valid() )
  291. {
  292. jack_input.push_back( po );
  293. }
  294. chain()->engine()->unlock();
  295. }
  296. }
  297. else if ( mode() == CV && m == GUI )
  298. {
  299. chain()->engine()->lock();
  300. jack_input.back().shutdown();
  301. jack_input.pop_back();
  302. chain()->engine()->unlock();
  303. }
  304. _mode = m ;
  305. }
  306. void
  307. Controller_Module::process ( void )
  308. {
  309. THREAD_ASSERT( RT );
  310. if ( control_output[0].connected() )
  311. {
  312. float f = control_value;
  313. if ( mode() == CV )
  314. {
  315. f = *((float*)jack_input[0].buffer( chain()->engine()->nframes() ));
  316. const Port *p = control_output[0].connected_port();
  317. if (p->hints.ranged )
  318. {
  319. // scale value to range.
  320. // we assume that CV values are between 0 and 1
  321. float scale = p->hints.maximum - p->hints.minimum;
  322. float offset = p->hints.minimum;
  323. f = ( f * scale ) + offset;
  324. }
  325. }
  326. // else
  327. // f = *((float*)control_output[0].buffer());
  328. *((float*)control_output[0].buffer()) = f;
  329. control_value = f;
  330. }
  331. }